38 lines
963 B
Go
38 lines
963 B
Go
|
package errors
|
||
|
|
||
|
import (
|
||
|
"net/http"
|
||
|
|
||
|
"github.com/go-chi/render"
|
||
|
)
|
||
|
|
||
|
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 ErrRender(err error) render.Renderer {
|
||
|
return &ErrResponse{
|
||
|
Err: err,
|
||
|
HTTPStatusCode: http.StatusUnprocessableEntity,
|
||
|
StatusText: "Error rendering response.",
|
||
|
ErrorText: err.Error(),
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func ErrUnauthorized() render.Renderer {
|
||
|
return &ErrResponse{
|
||
|
HTTPStatusCode: http.StatusUnauthorized,
|
||
|
StatusText: "Unauthorized",
|
||
|
}
|
||
|
}
|