Config. environment (throughenv var 'ENVIRONMENT')

This commit is contained in:
Denis Arh
2020-11-04 15:13:27 +01:00
parent 7666fe2d62
commit ed8fc547f7
12 changed files with 106 additions and 76 deletions
+11 -10
View File
@@ -12,7 +12,7 @@ import (
// ServeHTTP Prepares and encodes given error for HTTP transport
//
// mask arg hides extra/debug info
func ServeHTTP(w http.ResponseWriter, r *http.Request, err error, debug bool) {
func ServeHTTP(w http.ResponseWriter, r *http.Request, err error, mask bool) {
var (
// Very naive approach on parsing accept headers
acceptsJson = strings.Contains(r.Header.Get("accept"), "application/json")
@@ -31,12 +31,7 @@ func ServeHTTP(w http.ResponseWriter, r *http.Request, err error, debug bool) {
w.WriteHeader(code)
if !debug {
// trim error details when not debugging
err = fmt.Errorf(err.Error())
}
if debug && !acceptsJson {
if !mask && !acceptsJson {
// Prettify error for plain text debug output
w.Header().Set("Content-Type", "plain/text")
writeHttpPlain(w, err)
@@ -46,7 +41,7 @@ func ServeHTTP(w http.ResponseWriter, r *http.Request, err error, debug bool) {
}
w.Header().Set("Content-Type", "application/json")
writeHttpJSON(w, err)
writeHttpJSON(w, err, mask)
}
func writeHttpPlain(w io.Writer, err error) {
@@ -97,14 +92,20 @@ func writeHttpPlain(w io.Writer, err error) {
}
func writeHttpJSON(w io.Writer, err error) {
func writeHttpJSON(w io.Writer, err error, mask bool) {
var (
wrap = struct {
Error interface{} `json:"error"`
}{}
)
if c, is := err.(*Error); is {
if se, is := err.(interface{ Safe() bool }); !is || !se.Safe() {
// trim error details when not debugging or error is not safe
err = fmt.Errorf(err.Error())
}
if c, is := err.(json.Marshaler); is {
// take advantage of JSON marshaller on error
wrap.Error = c
} else {
wrap.Error = map[string]string{"message": err.Error()}
+2 -6
View File
@@ -11,12 +11,10 @@ func ExampleSimpleErrorAsText() {
// Output:
// Error: dummy error
// --------------------------------------------------------------------------------
// Note: you are seeing this because system is running in development mode
// and HTTP request is made without "Accept: .../json" headers
}
func ExampleSimpleErrorAsJson() {
writeHttpJSON(os.Stdout, fmt.Errorf("dummy error"))
writeHttpJSON(os.Stdout, fmt.Errorf("dummy error"), true)
// Output:
// {"error":{"message":"dummy error"}}
@@ -31,14 +29,12 @@ func ExampleErrorAsText() {
// --------------------------------------------------------------------------------
// a: b
// --------------------------------------------------------------------------------
// Note: you are seeing this because system is running in development mode
// and HTTP request is made without "Accept: .../json" headers
}
func ExampleErrorAsJson() {
err := New(0, "dummy error", Meta("a", "b"), Meta(&Error{}, "nope"))
err.stack = nil // will not test the stack as file path & line numbers might change
writeHttpJSON(os.Stdout, err)
writeHttpJSON(os.Stdout, err, false)
// Output:
// {"error":{"message":"dummy error","meta":{"a":"b"}}}