forked from mirrors/homebox
refactor: http interfaces (#114)
* implement custom http handler interface * implement trace_id * normalize http method spacing for consistent logs * fix failing test * fix linter errors * cleanup old dead code * more route cleanup * cleanup some inconsistent errors * update and generate code * make taskfile more consistent * update task calls * run tidy * drop `@` tag for version * use relative paths * tidy * fix auto-setting variables * update build paths * add contributing guide * tidy
This commit is contained in:
parent
e2d93f8523
commit
6529549289
40 changed files with 984 additions and 808 deletions
119
backend/internal/sys/validate/errors.go
Normal file
119
backend/internal/sys/validate/errors.go
Normal file
|
@ -0,0 +1,119 @@
|
|||
package validate
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
)
|
||||
|
||||
type UnauthorizedError struct {
|
||||
}
|
||||
|
||||
func (err *UnauthorizedError) Error() string {
|
||||
return "unauthorized"
|
||||
}
|
||||
|
||||
func IsUnauthorizedError(err error) bool {
|
||||
var re *UnauthorizedError
|
||||
return errors.As(err, &re)
|
||||
}
|
||||
|
||||
func NewUnauthorizedError() error {
|
||||
return &UnauthorizedError{}
|
||||
}
|
||||
|
||||
type InvalidRouteKeyError struct {
|
||||
key string
|
||||
}
|
||||
|
||||
func (err *InvalidRouteKeyError) Error() string {
|
||||
return "invalid route key: " + err.key
|
||||
}
|
||||
|
||||
func NewInvalidRouteKeyError(key string) error {
|
||||
return &InvalidRouteKeyError{key}
|
||||
}
|
||||
|
||||
func IsInvalidRouteKeyError(err error) bool {
|
||||
var re *InvalidRouteKeyError
|
||||
return errors.As(err, &re)
|
||||
}
|
||||
|
||||
// ErrorResponse is the form used for API responses from failures in the API.
|
||||
type ErrorResponse struct {
|
||||
Error string `json:"error"`
|
||||
Fields string `json:"fields,omitempty"`
|
||||
}
|
||||
|
||||
// RequestError is used to pass an error during the request through the
|
||||
// application with web specific context.
|
||||
type RequestError struct {
|
||||
Err error
|
||||
Status int
|
||||
Fields error
|
||||
}
|
||||
|
||||
// NewRequestError wraps a provided error with an HTTP status code. This
|
||||
// function should be used when handlers encounter expected errors.
|
||||
func NewRequestError(err error, status int) error {
|
||||
return &RequestError{err, status, nil}
|
||||
}
|
||||
|
||||
func (err *RequestError) Error() string {
|
||||
return err.Err.Error()
|
||||
}
|
||||
|
||||
// IsRequestError checks if an error of type RequestError exists.
|
||||
func IsRequestError(err error) bool {
|
||||
var re *RequestError
|
||||
return errors.As(err, &re)
|
||||
}
|
||||
|
||||
// FieldError is used to indicate an error with a specific request field.
|
||||
type FieldError struct {
|
||||
Field string `json:"field"`
|
||||
Error string `json:"error"`
|
||||
}
|
||||
|
||||
// FieldErrors represents a collection of field errors.
|
||||
type FieldErrors []FieldError
|
||||
|
||||
func (fe FieldErrors) Append(field, reason string) FieldErrors {
|
||||
return append(fe, FieldError{
|
||||
Field: field,
|
||||
Error: reason,
|
||||
})
|
||||
}
|
||||
|
||||
func (fe FieldErrors) Nil() bool {
|
||||
return len(fe) == 0
|
||||
}
|
||||
|
||||
// Error implments the error interface.
|
||||
func (fe FieldErrors) Error() string {
|
||||
d, err := json.Marshal(fe)
|
||||
if err != nil {
|
||||
return err.Error()
|
||||
}
|
||||
return string(d)
|
||||
}
|
||||
|
||||
func NewFieldErrors(errs ...FieldError) FieldErrors {
|
||||
return errs
|
||||
}
|
||||
|
||||
func IsFieldError(err error) bool {
|
||||
v := FieldErrors{}
|
||||
return errors.As(err, &v)
|
||||
}
|
||||
|
||||
// Cause iterates through all the wrapped errors until the root
|
||||
// error value is reached.
|
||||
func Cause(err error) error {
|
||||
root := err
|
||||
for {
|
||||
if err = errors.Unwrap(root); err == nil {
|
||||
return root
|
||||
}
|
||||
root = err
|
||||
}
|
||||
}
|
70
backend/internal/web/mid/errors.go
Normal file
70
backend/internal/web/mid/errors.go
Normal file
|
@ -0,0 +1,70 @@
|
|||
package mid
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/hay-kot/homebox/backend/ent"
|
||||
"github.com/hay-kot/homebox/backend/internal/sys/validate"
|
||||
"github.com/hay-kot/homebox/backend/pkgs/server"
|
||||
"github.com/rs/zerolog"
|
||||
)
|
||||
|
||||
func Errors(log zerolog.Logger) server.Middleware {
|
||||
return func(h server.Handler) server.Handler {
|
||||
return server.HandlerFunc(func(w http.ResponseWriter, r *http.Request) error {
|
||||
err := h.ServeHTTP(w, r)
|
||||
|
||||
if err != nil {
|
||||
var resp server.ErrorResponse
|
||||
var code int
|
||||
|
||||
log.Err(err).
|
||||
Str("trace_id", server.GetTraceID(r.Context())).
|
||||
Msg("ERROR occurred")
|
||||
|
||||
switch {
|
||||
case validate.IsUnauthorizedError(err):
|
||||
code = http.StatusUnauthorized
|
||||
resp = server.ErrorResponse{
|
||||
Error: "unauthorized",
|
||||
}
|
||||
case validate.IsInvalidRouteKeyError(err):
|
||||
code = http.StatusBadRequest
|
||||
resp = server.ErrorResponse{
|
||||
Error: err.Error(),
|
||||
}
|
||||
case validate.IsFieldError(err):
|
||||
fieldErrors := err.(validate.FieldErrors)
|
||||
resp.Error = "Validation Error"
|
||||
resp.Fields = map[string]string{}
|
||||
|
||||
for _, fieldError := range fieldErrors {
|
||||
resp.Fields[fieldError.Field] = fieldError.Error
|
||||
}
|
||||
case validate.IsRequestError(err):
|
||||
requestError := err.(*validate.RequestError)
|
||||
resp.Error = requestError.Error()
|
||||
code = requestError.Status
|
||||
case ent.IsNotFound(err):
|
||||
resp.Error = "Not Found"
|
||||
code = http.StatusNotFound
|
||||
default:
|
||||
resp.Error = "Unknown Error"
|
||||
code = http.StatusInternalServerError
|
||||
|
||||
}
|
||||
|
||||
if err := server.Respond(w, code, resp); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// If Showdown error, return error
|
||||
if server.IsShutdownError(err) {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
}
|
||||
}
|
97
backend/internal/web/mid/logger.go
Normal file
97
backend/internal/web/mid/logger.go
Normal file
|
@ -0,0 +1,97 @@
|
|||
package mid
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/hay-kot/homebox/backend/pkgs/server"
|
||||
"github.com/rs/zerolog"
|
||||
)
|
||||
|
||||
type statusRecorder struct {
|
||||
http.ResponseWriter
|
||||
Status int
|
||||
}
|
||||
|
||||
func (r *statusRecorder) WriteHeader(status int) {
|
||||
r.Status = status
|
||||
r.ResponseWriter.WriteHeader(status)
|
||||
}
|
||||
|
||||
func Logger(log zerolog.Logger) server.Middleware {
|
||||
return func(next server.Handler) server.Handler {
|
||||
return server.HandlerFunc(func(w http.ResponseWriter, r *http.Request) error {
|
||||
traceId := server.GetTraceID(r.Context())
|
||||
|
||||
log.Info().
|
||||
Str("trace_id", traceId).
|
||||
Str("method", r.Method).
|
||||
Str("path", r.URL.Path).
|
||||
Str("remove_address", r.RemoteAddr).
|
||||
Msg("request started")
|
||||
|
||||
record := &statusRecorder{ResponseWriter: w, Status: http.StatusOK}
|
||||
|
||||
err := next.ServeHTTP(record, r)
|
||||
|
||||
log.Info().
|
||||
Str("trace_id", traceId).
|
||||
Str("method", r.Method).
|
||||
Str("url", r.URL.Path).
|
||||
Str("remote_address", r.RemoteAddr).
|
||||
Int("status_code", record.Status).
|
||||
Msg("request completed")
|
||||
|
||||
return err
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func SugarLogger(log zerolog.Logger) server.Middleware {
|
||||
orange := func(s string) string { return "\033[33m" + s + "\033[0m" }
|
||||
aqua := func(s string) string { return "\033[36m" + s + "\033[0m" }
|
||||
red := func(s string) string { return "\033[31m" + s + "\033[0m" }
|
||||
green := func(s string) string { return "\033[32m" + s + "\033[0m" }
|
||||
|
||||
fmtCode := func(code int) string {
|
||||
switch {
|
||||
case code >= 500:
|
||||
return red(fmt.Sprintf("%d", code))
|
||||
case code >= 400:
|
||||
return orange(fmt.Sprintf("%d", code))
|
||||
case code >= 300:
|
||||
return aqua(fmt.Sprintf("%d", code))
|
||||
default:
|
||||
return green(fmt.Sprintf("%d", code))
|
||||
}
|
||||
}
|
||||
bold := func(s string) string { return "\033[1m" + s + "\033[0m" }
|
||||
|
||||
atLeast6 := func(s string) string {
|
||||
for len(s) <= 6 {
|
||||
s += " "
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
return func(next server.Handler) server.Handler {
|
||||
return server.HandlerFunc(func(w http.ResponseWriter, r *http.Request) error {
|
||||
|
||||
record := &statusRecorder{ResponseWriter: w, Status: http.StatusOK}
|
||||
|
||||
err := next.ServeHTTP(record, r) // Blocks until the next handler returns.
|
||||
|
||||
url := fmt.Sprintf("%s %s", r.RequestURI, r.Proto)
|
||||
|
||||
log.Info().
|
||||
Str("trace_id", server.GetTraceID(r.Context())).
|
||||
Msgf("%s %s %s",
|
||||
bold(fmtCode(record.Status)),
|
||||
bold(orange(atLeast6(r.Method))),
|
||||
aqua(url),
|
||||
)
|
||||
|
||||
return err
|
||||
})
|
||||
}
|
||||
}
|
33
backend/internal/web/mid/panic.go
Normal file
33
backend/internal/web/mid/panic.go
Normal file
|
@ -0,0 +1,33 @@
|
|||
package mid
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"runtime/debug"
|
||||
|
||||
"github.com/hay-kot/homebox/backend/pkgs/server"
|
||||
)
|
||||
|
||||
// Panic is a middleware that recovers from panics anywhere in the chain and wraps the error.
|
||||
// and returns it up the middleware chain.
|
||||
func Panic(develop bool) server.Middleware {
|
||||
return func(h server.Handler) server.Handler {
|
||||
return server.HandlerFunc(func(w http.ResponseWriter, r *http.Request) (err error) {
|
||||
defer func() {
|
||||
if rec := recover(); rec != nil {
|
||||
trace := debug.Stack()
|
||||
|
||||
if develop {
|
||||
err = fmt.Errorf("PANIC [%v]", rec)
|
||||
fmt.Printf("%s", string(trace))
|
||||
} else {
|
||||
err = fmt.Errorf("PANIC [%v] TRACE[%s]", rec, string(trace))
|
||||
}
|
||||
|
||||
}
|
||||
}()
|
||||
|
||||
return h.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue