forked from mirrors/homebox
update error handlers
This commit is contained in:
parent
93a4a816f9
commit
9501eb398a
4 changed files with 25 additions and 16 deletions
|
@ -11,6 +11,10 @@ import (
|
||||||
|
|
||||||
type Level int8
|
type Level int8
|
||||||
|
|
||||||
|
var (
|
||||||
|
IncludeTrace = false
|
||||||
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
LevelDebug Level = iota
|
LevelDebug Level = iota
|
||||||
LevelInfo
|
LevelInfo
|
||||||
|
@ -88,8 +92,12 @@ func (l *Logger) print(level Level, message string, properties map[string]string
|
||||||
}
|
}
|
||||||
|
|
||||||
// Include a stack trace for entries at the ERROR and FATAL levels.
|
// Include a stack trace for entries at the ERROR and FATAL levels.
|
||||||
|
dumpTrace := false
|
||||||
if level >= LevelError {
|
if level >= LevelError {
|
||||||
aux.Trace = string(debug.Stack())
|
dumpTrace = true
|
||||||
|
if IncludeTrace {
|
||||||
|
aux.Trace = string(debug.Stack())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Declare a line variable for holding the actual log entry text.
|
// Declare a line variable for holding the actual log entry text.
|
||||||
|
@ -109,8 +117,11 @@ func (l *Logger) print(level Level, message string, properties map[string]string
|
||||||
l.mu.Lock()
|
l.mu.Lock()
|
||||||
defer l.mu.Unlock()
|
defer l.mu.Unlock()
|
||||||
|
|
||||||
// Write the log entry followed by a newline.
|
n, err := l.out.Write(line)
|
||||||
return l.out.Write(append(line, '\n'))
|
if dumpTrace {
|
||||||
|
l.out.Write(debug.Stack())
|
||||||
|
}
|
||||||
|
return n, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// We also implement a Write() method on our Logger type so that it satisfies the
|
// We also implement a Write() method on our Logger type so that it satisfies the
|
||||||
|
|
|
@ -1,7 +1,8 @@
|
||||||
package server
|
package server
|
||||||
|
|
||||||
const (
|
const (
|
||||||
ContentType = "Content-Type"
|
ContentType = "Content-Type"
|
||||||
ContentJSON = "application/json"
|
ContentJSON = "application/json"
|
||||||
ContentXML = "application/xml"
|
ContentXML = "application/xml"
|
||||||
|
ContentFormUrlEncoded = "application/x-www-form-urlencoded"
|
||||||
)
|
)
|
||||||
|
|
|
@ -8,17 +8,16 @@ import (
|
||||||
|
|
||||||
// Respond converts a Go value to JSON and sends it to the client.
|
// Respond converts a Go value to JSON and sends it to the client.
|
||||||
// Adapted from https://github.com/ardanlabs/service/tree/master/foundation/web
|
// Adapted from https://github.com/ardanlabs/service/tree/master/foundation/web
|
||||||
func Respond(w http.ResponseWriter, statusCode int, data interface{}) error {
|
func Respond(w http.ResponseWriter, statusCode int, data interface{}) {
|
||||||
// If there is nothing to marshal then set status code and return.
|
|
||||||
if statusCode == http.StatusNoContent {
|
if statusCode == http.StatusNoContent {
|
||||||
w.WriteHeader(statusCode)
|
w.WriteHeader(statusCode)
|
||||||
return nil
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Convert the response value to JSON.
|
// Convert the response value to JSON.
|
||||||
jsonData, err := json.Marshal(data)
|
jsonData, err := json.Marshal(data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set the content type and headers once we know marshaling has succeeded.
|
// Set the content type and headers once we know marshaling has succeeded.
|
||||||
|
@ -29,10 +28,8 @@ func Respond(w http.ResponseWriter, statusCode int, data interface{}) error {
|
||||||
|
|
||||||
// Send the result back to the client.
|
// Send the result back to the client.
|
||||||
if _, err := w.Write(jsonData); err != nil {
|
if _, err := w.Write(jsonData); err != nil {
|
||||||
return err
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ResponseError is a helper function that sends a JSON response of an error message
|
// ResponseError is a helper function that sends a JSON response of an error message
|
||||||
|
@ -42,9 +39,9 @@ func RespondError(w http.ResponseWriter, statusCode int, err error) {
|
||||||
eb.Respond(w, statusCode)
|
eb.Respond(w, statusCode)
|
||||||
}
|
}
|
||||||
|
|
||||||
// RespondInternalServerError is a wrapper around RespondError that sends a 500 internal server error. Useful for
|
// RespondServerError is a wrapper around RespondError that sends a 500 internal server error. Useful for
|
||||||
// Sending generic errors when everything went wrong.
|
// Sending generic errors when everything went wrong.
|
||||||
func RespondInternalServerError(w http.ResponseWriter) {
|
func RespondServerError(w http.ResponseWriter) {
|
||||||
RespondError(w, http.StatusInternalServerError, errors.New("internal server error"))
|
RespondError(w, http.StatusInternalServerError, errors.New("internal server error"))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -52,7 +52,7 @@ func Test_RespondError(t *testing.T) {
|
||||||
func Test_RespondInternalServerError(t *testing.T) {
|
func Test_RespondInternalServerError(t *testing.T) {
|
||||||
recorder := httptest.NewRecorder()
|
recorder := httptest.NewRecorder()
|
||||||
|
|
||||||
RespondInternalServerError(recorder)
|
RespondServerError(recorder)
|
||||||
|
|
||||||
assert.Equal(t, http.StatusInternalServerError, recorder.Code)
|
assert.Equal(t, http.StatusInternalServerError, recorder.Code)
|
||||||
assert.JSONEq(t, recorder.Body.String(), `{"details":["internal server error"], "message":"Internal Server Error", "error":true}`)
|
assert.JSONEq(t, recorder.Body.String(), `{"details":["internal server error"], "message":"Internal Server Error", "error":true}`)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue