|
99 | 99 |
|
100 | 100 | // HTTPError represents an error that occurred while handling a request.
|
101 | 101 | HTTPError struct {
|
102 |
| - Code int |
103 |
| - Message interface{} |
104 |
| - Internal error // Stores the error returned by an external dependency |
| 102 | + Code int `json:"code"` |
| 103 | + Message interface{} `json:"message"` |
| 104 | + Internal error `json:"-"` // Stores the error returned by an external dependency |
105 | 105 | }
|
106 | 106 |
|
107 | 107 | // MiddlewareFunc defines a function to process middleware.
|
@@ -341,32 +341,28 @@ func (e *Echo) Routers() map[string]*Router {
|
341 | 341 | // DefaultHTTPErrorHandler is the default HTTP error handler. It sends a JSON response
|
342 | 342 | // with status code.
|
343 | 343 | func (e *Echo) DefaultHTTPErrorHandler(err error, c Context) {
|
344 |
| - var ( |
345 |
| - code = http.StatusInternalServerError |
346 |
| - msg interface{} |
347 |
| - ) |
348 |
| - |
349 |
| - if he, ok := err.(*HTTPError); ok { |
350 |
| - code = he.Code |
351 |
| - msg = he.Message |
| 344 | + he, ok := err.(*HTTPError) |
| 345 | + if ok { |
352 | 346 | if he.Internal != nil {
|
353 | 347 | err = fmt.Errorf("%v, %v", err, he.Internal)
|
354 | 348 | }
|
355 |
| - } else if e.Debug { |
356 |
| - msg = err.Error() |
357 | 349 | } else {
|
358 |
| - msg = http.StatusText(code) |
| 350 | + he = &HTTPError{ |
| 351 | + Code: http.StatusInternalServerError, |
| 352 | + } |
359 | 353 | }
|
360 |
| - if _, ok := msg.(string); ok { |
361 |
| - msg = Map{"message": msg} |
| 354 | + if e.Debug { |
| 355 | + he.Message = err.Error() |
| 356 | + } else { |
| 357 | + he.Message = http.StatusText(he.Code) |
362 | 358 | }
|
363 | 359 |
|
364 | 360 | // Send response
|
365 | 361 | if !c.Response().Committed {
|
366 | 362 | if c.Request().Method == http.MethodHead { // Issue #608
|
367 |
| - err = c.NoContent(code) |
| 363 | + err = c.NoContent(he.Code) |
368 | 364 | } else {
|
369 |
| - err = c.JSON(code, msg) |
| 365 | + err = c.JSON(he.Code, he) |
370 | 366 | }
|
371 | 367 | if err != nil {
|
372 | 368 | e.Logger.Error(err)
|
@@ -749,7 +745,7 @@ func NewHTTPError(code int, message ...interface{}) *HTTPError {
|
749 | 745 |
|
750 | 746 | // Error makes it compatible with `error` interface.
|
751 | 747 | func (he *HTTPError) Error() string {
|
752 |
| - return fmt.Sprintf("code=%d, message=%v", he.Code, he.Message) |
| 748 | + return fmt.Sprintf("code=%d, message=%v, internal=%v", he.Code, he.Message, he.Internal) |
753 | 749 | }
|
754 | 750 |
|
755 | 751 | // SetInternal sets error to HTTPError.Internal
|
|
0 commit comments