feat(smalltrip,exceptions): use a JSON handler if none is provided via the context

This commit is contained in:
Guz
2025-02-25 14:30:14 -03:00
parent efceb2efd6
commit ef8261752f
2 changed files with 78 additions and 0 deletions

View File

@@ -12,6 +12,11 @@ type Exception struct {
Message string `json:"message"` // User friendly message
Err error `json:"error,omitempty"` // Go error
Severity Severity `json:"severity"` // Exception level
// Handler to be used. This is normally provided by a middleware via the
// request context. Setting this field overrides any provided by the middleware
// and can be used to add a handler when using a middleware is not possible.
handler HandlerFunc `json:"-"`
}
var (
@@ -33,6 +38,8 @@ func (e Exception) ServeHTTP(w http.ResponseWriter, r *http.Request) {
e.handler(e, w, r)
}
e.handler = HandlerJSON(HandlerText)
handler, ok := r.Context().Value(handlerFuncCtxKey).(HandlerFunc)
if !ok {
e.handler(e, w, r)