Last active
August 4, 2017 02:26
-
-
Save mori-dev/0ae89dc0918d2b1c644d34dbee29b408 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package app_error | |
import ( | |
"net/http" | |
"time" | |
"github.com/labstack/echo" | |
) | |
type ApiError struct { | |
StatusCode int `json:"status_code"` | |
Type string `json:"type"` | |
ResponsedAt string `json:"responsed_at"` // RFC3339 | |
Message string `json:"message"` | |
UserMessageTitle string `json:"user_message_title"` | |
UserMessages []string `json:"user_messages"` | |
Tip string `json:"tip"` | |
} | |
func CustomHTTPErrorHandler(err error, c echo.Context) { | |
code := http.StatusInternalServerError | |
message := "" | |
// https://godoc.org/github.com/labstack/echo#HTTPError | |
if ee, ok := err.(*echo.HTTPError); ok { | |
code = ee.Code | |
message = ee.Message.(string) | |
} | |
body := ApiError{ | |
StatusCode: code, | |
Type: "echo_error", | |
ResponsedAt: time.Now().Format(time.RFC3339), | |
Message: message, | |
UserMessageTitle: "", | |
UserMessages: []string{}, | |
Tip: "", | |
} | |
c.JSON(code, body) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment