neko/internal/api/utils/error.go
2020-10-30 22:06:03 +01:00

80 lines
2.0 KiB
Go

package utils
import (
"net/http"
"github.com/go-chi/render"
)
//--
// Error response payloads & renderers
//--
// ErrResponse renderer type for handling all sorts of errors.
//
// In the best case scenario, the excellent github.com/pkg/errors package
// helps reveal information on the error, setting it on Err, and in the Render()
// method, using it to set the application-specific error code in AppCode.
type ErrResponse struct {
Err error `json:"-"` // low-level runtime error
HTTPStatusCode int `json:"-"` // http response status code
StatusText string `json:"status"` // user-level status message
AppCode int64 `json:"code,omitempty"` // application-specific error code
ErrorText string `json:"error,omitempty"` // application-level error message, for debugging
}
func (e *ErrResponse) Render(w http.ResponseWriter, r *http.Request) error {
render.Status(r, e.HTTPStatusCode)
return nil
}
func ErrMessage(HTTPStatusCode int, StatusText string) render.Renderer {
return &ErrResponse{
HTTPStatusCode: HTTPStatusCode,
StatusText: StatusText,
}
}
func ErrInvalidRequest(err error) render.Renderer {
return &ErrResponse{
Err: err,
HTTPStatusCode: 400,
StatusText: "Invalid request.",
ErrorText: err.Error(),
}
}
func ErrInternalServer(err error) render.Renderer {
return &ErrResponse{
Err: err,
HTTPStatusCode: 500,
StatusText: "Internal server error.",
ErrorText: err.Error(),
}
}
func ErrNot(err error) render.Renderer {
return &ErrResponse{
Err: err,
HTTPStatusCode: 422,
StatusText: "Error rendering response.",
ErrorText: err.Error(),
}
}
var ErrNotAuthenticated = &ErrResponse{
HTTPStatusCode: 401,
StatusText: "Invalid or missing access token.",
}
var ErrNotAuthorized = &ErrResponse{
HTTPStatusCode: 403,
StatusText: "Access token does not have the required scope.",
}
var ErrNotFound = &ErrResponse{
HTTPStatusCode: 404,
StatusText: "Resource not found.",
}