MCPcopy Create free account
hub / github.com/kataras/iris / FireErrorCode

Method FireErrorCode

core/router/handler.go:544–652  ·  view source on GitHub ↗

FireErrorCode handles the response's error response. If `Configuration.ResetOnFireErrorCode()` is true and the response writer was a recorder one then it will try to reset the headers and the body before calling the registered (or default) error handler for that error code set by `ctx.StatusCode` me

(ctx *context.Context)

Source from the content-addressed store, hash-verified

542// registered (or default) error handler for that error code set by
543// `ctx.StatusCode` method.
544func (h *routerHandler) FireErrorCode(ctx *context.Context) {
545 // On common response writer, always check
546 // if we can't reset the body and the body has been filled
547 // which means that the status code already sent,
548 // then do not fire this custom error code,
549 // rel: context/context.go#EndRequest.
550 //
551 // Note that, this is set to 0 on recorder because it holds the response before sent,
552 // so we check their len(Body()) instead, look below.
553 if ctx.ResponseWriter().Written() > 0 {
554 return
555 }
556
557 statusCode := ctx.GetStatusCode() // the response's cached one.
558
559 if ctx.Application().ConfigurationReadOnly().GetResetOnFireErrorCode() /* could be an argument too but we must not break the method */ {
560 // if we can reset the body, probably manual call of `Application.FireErrorCode`.
561 if w, ok := ctx.IsRecording(); ok {
562 if statusCodeSuccessful(w.StatusCode()) { // if not an error status code
563 w.WriteHeader(statusCode) // then set it manually here, otherwise it should be set via ctx.StatusCode(...)
564 }
565 // reset if previous content and it's recorder, keep the status code.
566 w.ClearHeaders()
567 w.ResetBody()
568
569 if cw, ok := w.ResponseWriter.(*context.CompressResponseWriter); ok {
570 // recorder wraps a compress writer.
571 cw.Disabled = true
572 }
573 } else if w, ok := ctx.ResponseWriter().(*context.CompressResponseWriter); ok {
574 // reset and disable the gzip in order to be an expected form of http error result
575 w.Disabled = true
576 }
577 } else {
578 // check if a body already set (the error response is handled by the handler itself,
579 // see `Context.EndRequest`)
580 if w, ok := ctx.IsRecording(); ok {
581 if len(w.Body()) > 0 {
582 return
583 }
584 }
585 }
586
587 for i := range h.errorTrees {
588 t := h.errorTrees[i]
589
590 if statusCode != t.statusCode {
591 continue
592 }
593
594 if h.errorHosts && !canHandleSubdomain(ctx, t.subdomain) {
595 continue
596 }
597
598 n := t.search(ctx.Path(), ctx.Params())
599 if n == nil {
600 // try to take the root's one.
601 n = t.root.getChild(pathSep)

Callers

nothing calls this directly

Calls 15

statusCodeSuccessfulFunction · 0.85
canHandleSubdomainFunction · 0.85
ResponseWriterMethod · 0.80
GetStatusCodeMethod · 0.80
ApplicationMethod · 0.80
IsRecordingMethod · 0.80
WriteHeaderMethod · 0.80
ClearHeadersMethod · 0.80
searchMethod · 0.80
ParamsMethod · 0.80
getChildMethod · 0.80
SetCurrentRouteMethod · 0.80

Tested by

no test coverage detected