fix(errors): errors handling interface implementation
This commit is contained in:
@@ -6,15 +6,37 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
type ErrBadRequest struct {
|
||||
Msg string `json:"message"`
|
||||
}
|
||||
|
||||
func NewErrBadRequest(format string, a ...string) RouteErrorHandler {
|
||||
return defaultErrorHandler{ErrBadRequest{Msg: fmt.Sprintf(format, a)}}
|
||||
}
|
||||
func (e ErrBadRequest) Error() string { return e.Msg }
|
||||
func (e ErrBadRequest) Status() int { return http.StatusBadRequest }
|
||||
|
||||
type ErrMissingParams struct {
|
||||
defaultErr
|
||||
Params []string `json:"params"`
|
||||
}
|
||||
|
||||
func NewErrMissingParams(params ...string) ErrMissingParams {
|
||||
return ErrMissingParams{Params: params}
|
||||
func NewErrMissingParams(params ...string) RouteErrorHandler {
|
||||
return defaultErrorHandler{ErrMissingParams{Params: params}}
|
||||
}
|
||||
func (e ErrMissingParams) Error() string {
|
||||
return fmt.Sprintf("Missing parameters: %s.", strings.Join(e.Params, ", "))
|
||||
}
|
||||
func (e ErrMissingParams) Status() int { return http.StatusBadRequest }
|
||||
|
||||
type ErrMethodNotAllowed struct {
|
||||
Method string `json:"method"`
|
||||
Allowed []string `json:"allowed"`
|
||||
}
|
||||
|
||||
func NewErrMethodNotAllowed(method string, allowed ...string) RouteErrorHandler {
|
||||
return defaultErrorHandler{ErrMethodNotAllowed{Method: method, Allowed: allowed}}
|
||||
}
|
||||
func (e ErrMethodNotAllowed) Error() string {
|
||||
return fmt.Sprintf("Method %s not allowed. Allowed methods are: %s", e.Method, strings.Join(e.Allowed, ", "))
|
||||
}
|
||||
func (e ErrMethodNotAllowed) Status() int { return http.StatusMethodNotAllowed }
|
||||
|
||||
@@ -6,10 +6,11 @@ import (
|
||||
)
|
||||
|
||||
type ErrInternal struct {
|
||||
defaultErr
|
||||
Err string `json:"error"`
|
||||
}
|
||||
|
||||
func NewErrInternal(err ...error) ErrInternal { return ErrInternal{Err: errors.Join(err...).Error()} }
|
||||
func (e ErrInternal) Error() string { return e.Err }
|
||||
func (e ErrInternal) Status() int { return http.StatusInternalServerError }
|
||||
func NewErrInternal(err ...error) RouteErrorHandler {
|
||||
return defaultErrorHandler{ErrInternal{Err: errors.Join(err...).Error()}}
|
||||
}
|
||||
func (e ErrInternal) Error() string { return e.Err }
|
||||
func (e ErrInternal) Status() int { return http.StatusInternalServerError }
|
||||
|
||||
@@ -7,25 +7,22 @@ import (
|
||||
"encoding/json"
|
||||
)
|
||||
|
||||
type Error interface {
|
||||
type RouteError interface {
|
||||
Error() string
|
||||
Status() int
|
||||
}
|
||||
|
||||
type RouteErrorHandler interface {
|
||||
ServeHTTP(w http.ResponseWriter, r *http.Request)
|
||||
Component() templ.Component
|
||||
JSON() string
|
||||
}
|
||||
|
||||
type defaultErr struct{}
|
||||
|
||||
func (e defaultErr) Error() string {
|
||||
return "Error: This method should have been overridden :')"
|
||||
type defaultErrorHandler struct {
|
||||
RouteError
|
||||
}
|
||||
|
||||
func (e defaultErr) Status() int {
|
||||
return http.StatusNotImplemented
|
||||
}
|
||||
|
||||
func (e defaultErr) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
func (e defaultErrorHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
if strings.Contains(r.Header.Get("Accept"), "text/html") {
|
||||
w.Header().Set("Content-Type", "text/html")
|
||||
|
||||
@@ -50,7 +47,7 @@ func (e defaultErr) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(e.Status())
|
||||
}
|
||||
|
||||
func (e defaultErr) JSON() string {
|
||||
func (e defaultErrorHandler) JSON() string {
|
||||
type jsonErr struct {
|
||||
Error string `json:"error"`
|
||||
Info any `json:"info"`
|
||||
@@ -69,7 +66,7 @@ func (e defaultErr) JSON() string {
|
||||
return string(js)
|
||||
}
|
||||
|
||||
templ (e defaultErr) Component() {
|
||||
templ (e defaultErrorHandler) Component() {
|
||||
@layouts.Page("Error") {
|
||||
<dialog open>
|
||||
<article>
|
||||
|
||||
Reference in New Issue
Block a user