-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy patherror.go
36 lines (30 loc) · 900 Bytes
/
error.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package fireball
import (
"net/http"
)
// HTTPError implements the Response and Error interfaces
type HTTPError struct {
*HTTPResponse
Err error
}
// NewError returns a new HTTPError
func NewError(status int, err error, headers map[string]string) *HTTPError {
return &HTTPError{
HTTPResponse: NewResponse(status, []byte(err.Error()), headers),
Err: err,
}
}
// Error calls the internal Err.Error function
func (e *HTTPError) Error() string {
return e.Err.Error()
}
// DefaultErrorHandler is the default ErrorHandler used by an App
// If the error implements the Response interface, it will call its Write function
// Otherwise, a 500 with the error message is returned
func DefaultErrorHandler(w http.ResponseWriter, r *http.Request, err error) {
if err, ok := err.(Response); ok {
err.Write(w, r)
return
}
http.Error(w, err.Error(), http.StatusInternalServerError)
}