You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
51 lines
1.6 KiB
Go
51 lines
1.6 KiB
Go
package api
|
|
|
|
import (
|
|
"github.com/go-chi/render"
|
|
"net/http"
|
|
)
|
|
|
|
//--------------------------------------------------------------------------
|
|
// 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 ErrInvalidRequest(err error) render.Renderer {
|
|
return &ErrResponse{
|
|
Err: err,
|
|
HTTPStatusCode: 400,
|
|
StatusText: "Invalid request.",
|
|
ErrorText: err.Error(),
|
|
}
|
|
}
|
|
|
|
func ErrRender(err error) render.Renderer {
|
|
return &ErrResponse{
|
|
Err: err,
|
|
HTTPStatusCode: 422,
|
|
StatusText: "Error rendering response.",
|
|
ErrorText: err.Error(),
|
|
}
|
|
}
|
|
|
|
var ErrNotFound = &ErrResponse{HTTPStatusCode: 404, StatusText: "Resource not found."}
|
|
var ErrNotANumber = &ErrResponse{HTTPStatusCode: 400, StatusText: "Value not a number."}
|