Skip to content

Instantly share code, notes, and snippets.

@mori-dev
Last active August 4, 2017 02:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mori-dev/0ae89dc0918d2b1c644d34dbee29b408 to your computer and use it in GitHub Desktop.
Save mori-dev/0ae89dc0918d2b1c644d34dbee29b408 to your computer and use it in GitHub Desktop.
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