mirror of
https://github.com/kataras/iris.git
synced 2026-01-26 05:15:56 +00:00
Update to 8.0.4 | New: transfer a message to the request logger
Former-commit-id: 2bab3c9f28f7e9edd5d85e579349f70388af871d
This commit is contained in:
@@ -21,8 +21,12 @@ type ErrorCodeHandler struct {
|
||||
func (ch *ErrorCodeHandler) Fire(ctx context.Context) {
|
||||
// if we can reset the body
|
||||
if w, ok := ctx.IsRecording(); ok {
|
||||
// reset if previous content and it's recorder
|
||||
w.Reset()
|
||||
if w.StatusCode() < 400 { // if not an error status code
|
||||
w.WriteHeader(ch.StatusCode) // then set it manually here, otherwise it should be setted via ctx.StatusCode(...)
|
||||
}
|
||||
// reset if previous content and it's recorder, keep the status code.
|
||||
w.ClearHeaders()
|
||||
w.ResetBody()
|
||||
} else if w, ok := ctx.ResponseWriter().(*context.GzipResponseWriter); ok {
|
||||
// reset and disable the gzip in order to be an expected form of http error result
|
||||
w.ResetBody()
|
||||
@@ -41,6 +45,18 @@ func (ch *ErrorCodeHandler) Fire(ctx context.Context) {
|
||||
// i.e
|
||||
// users := app.Party("/users")
|
||||
// users.Done(func(ctx context.Context){ if ctx.StatusCode() == 400 { /* custom error code for /users */ }})
|
||||
|
||||
// use .HandlerIndex
|
||||
// that sets the current handler index to zero
|
||||
// in order to:
|
||||
// ignore previous runs that may changed the handler index,
|
||||
// via ctx.Next or ctx.StopExecution, if any.
|
||||
//
|
||||
// use .Do
|
||||
// that overrides the existing handlers and sets and runs these error handlers.
|
||||
// in order to:
|
||||
// ignore the route's after-handlers, if any.
|
||||
ctx.HandlerIndex(0)
|
||||
ctx.Do(ch.Handlers)
|
||||
}
|
||||
|
||||
@@ -76,10 +92,7 @@ func defaultErrorCodeHandlers() *ErrorCodeHandlers {
|
||||
|
||||
func statusText(statusCode int) context.Handler {
|
||||
return func(ctx context.Context) {
|
||||
if _, err := ctx.WriteString(http.StatusText(statusCode)); err != nil {
|
||||
// ctx.Application().Logger().Infof("(status code: %d) %s",
|
||||
// err.Error(), statusCode)
|
||||
}
|
||||
ctx.WriteString(http.StatusText(statusCode))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -113,10 +126,12 @@ func (s *ErrorCodeHandlers) Register(statusCode int, handlers ...context.Handler
|
||||
StatusCode: statusCode,
|
||||
Handlers: handlers,
|
||||
}
|
||||
|
||||
s.handlers = append(s.handlers, ch)
|
||||
|
||||
return ch
|
||||
}
|
||||
|
||||
// otherwise update the handlers
|
||||
h.updateHandlers(handlers)
|
||||
return h
|
||||
@@ -136,6 +151,5 @@ func (s *ErrorCodeHandlers) Fire(ctx context.Context) {
|
||||
if ch == nil {
|
||||
ch = s.Register(statusCode, statusText(statusCode))
|
||||
}
|
||||
|
||||
ch.Fire(ctx)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user