Skip to content

Commit

Permalink
fix #1531 and introduce the 'Configuration.ResetOnFireErrorCode' (rea…
Browse files Browse the repository at this point in the history
…d HISTORY.md)
  • Loading branch information
kataras committed Jun 8, 2020
1 parent 02699ed commit d57b502
Show file tree
Hide file tree
Showing 20 changed files with 333 additions and 260 deletions.
2 changes: 2 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,8 @@ Other Improvements:

![DBUG routes](https://iris-go.com/images/v12.2.0-dbug2.png?v=0)

- Fixed handler's error response not be respected when response recorder or gzip writer was used instead of the common writer. Fixes [#1531](https://github.com/kataras/iris/issues/1531). It contains a **BREAKING CHANGE** of: the new `Configuration.ResetOnFireErrorCode` field should be set **to true** in order to behave as it used before this update (to reset the contents on recorder or gzip writer).

- New builtin [requestid](https://github.com/kataras/iris/tree/master/middleware/requestid) middleware.

- New builtin [JWT](https://github.com/kataras/iris/tree/master/middleware/jwt) middleware based on [square/go-jose](https://github.com/square/go-jose) featured with optional encryption to set claims with sensitive data when necessary.
Expand Down
2 changes: 1 addition & 1 deletion _examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
* [Route State](routing/route-state/main.go)
* [Reverse Routing](routing/reverse/main.go)
* [Router Wrapper](routing/custom-wrapper/main.go)
* [Custom Router](routing/custom-high-level-router/main.go)
* [Custom Router](routing/custom-router/main.go)
* Custom Context
* [Method Overriding](routing/custom-context/method-overriding/main.go)
* [New Implementation](routing/custom-context/new-implementation/main.go)
Expand Down
2 changes: 1 addition & 1 deletion _examples/bootstrap/bootstrap/bootstrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func (b *Bootstrapper) SetupWebsockets(endpoint string, handler websocket.ConnHa
}

// SetupErrorHandlers prepares the http error handlers
// `(context.StatusCodeNotSuccessful`, which defaults to < 200 || >= 400 but you can change it).
// `(context.StatusCodeNotSuccessful`, which defaults to >=400 (but you can change it).
func (b *Bootstrapper) SetupErrorHandlers() {
b.OnAnyErrorCode(func(ctx iris.Context) {
err := iris.Map{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func (r *customRouter) RouteExists(ctx iris.Context, method, path string) bool {
return false
}

func (r *customRouter) FireErrorCode(ctx iris.Context) {
func (r *customRouter) FireErrorCode(ctx iris.Context, reset bool) {
// responseStatusCode := ctx.GetStatusCode() // set by prior ctx.StatusCode calls
// [...]
}
Expand Down
2 changes: 1 addition & 1 deletion _examples/view/herotemplate/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package main
import (
"bytes"

"github.com/kataras/iris/v12/_examples/response-writer/herotemplate/template"
"github.com/kataras/iris/v12/_examples/view/herotemplate/template"

"github.com/kataras/iris/v12"
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package controllers

import (
"github.com/kataras/iris/v12/_examples/response-writer/quicktemplate/templates"
"github.com/kataras/iris/v12/_examples/view/quicktemplate/templates"

"github.com/kataras/iris/v12"
)
Expand Down
2 changes: 1 addition & 1 deletion _examples/view/quicktemplate/controllers/hello.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package controllers

import (
"github.com/kataras/iris/v12/_examples/response-writer/quicktemplate/templates"
"github.com/kataras/iris/v12/_examples/view/quicktemplate/templates"

"github.com/kataras/iris/v12"
)
Expand Down
2 changes: 1 addition & 1 deletion _examples/view/quicktemplate/controllers/index.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package controllers

import (
"github.com/kataras/iris/v12/_examples/response-writer/quicktemplate/templates"
"github.com/kataras/iris/v12/_examples/view/quicktemplate/templates"

"github.com/kataras/iris/v12"
)
Expand Down
2 changes: 1 addition & 1 deletion _examples/view/quicktemplate/main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package main

import (
"github.com/kataras/iris/v12/_examples/response-writer/quicktemplate/controllers"
"github.com/kataras/iris/v12/_examples/view/quicktemplate/controllers"

"github.com/kataras/iris/v12"
)
Expand Down
77 changes: 49 additions & 28 deletions configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,13 +269,6 @@ var WithEmptyFormError = func(app *Application) {
app.config.FireEmptyFormError = true
}

// WithoutAutoFireStatusCode disables the AutoFireStatusCode setting.
//
// See `Configuration`.
var WithoutAutoFireStatusCode = func(app *Application) {
app.config.DisableAutoFireStatusCode = true
}

// WithPathEscape sets the EnablePathEscape setting to true.
//
// See `Configuration`.
Expand Down Expand Up @@ -305,6 +298,20 @@ var WithFireMethodNotAllowed = func(app *Application) {
app.config.FireMethodNotAllowed = true
}

// WithoutAutoFireStatusCode sets the DisableAutoFireStatusCode setting to true.
//
// See `Configuration`.
var WithoutAutoFireStatusCode = func(app *Application) {
app.config.DisableAutoFireStatusCode = true
}

// WithResetOnFireErrorCode sets the ResetOnFireErrorCode setting to true.
//
// See `Configuration`.
var WithResetOnFireErrorCode = func(app *Application) {
app.config.ResetOnFireErrorCode = true
}

// WithTimeFormat sets the TimeFormat setting.
//
// See `Configuration`.
Expand Down Expand Up @@ -829,6 +836,21 @@ type Configuration struct {
// fires the 405 error instead of 404
// Defaults to false.
FireMethodNotAllowed bool `json:"fireMethodNotAllowed,omitempty" yaml:"FireMethodNotAllowed" toml:"FireMethodNotAllowed"`
// DisableAutoFireStatusCode if true then it turns off the http error status code
// handler automatic execution on error code from a `Context.StatusCode` call.
// By-default a custom http error handler will be fired when "Context.StatusCode(errorCode)" called.
//
// Defaults to false.
DisableAutoFireStatusCode bool `json:"disableAutoFireStatusCode,omitempty" yaml:"DisableAutoFireStatusCode" toml:"DisableAutoFireStatusCode"`
// ResetOnFireErrorCode if true then any previously response body or headers through
// response recorder or gzip writer will be ignored and the router
// will fire the registered (or default) HTTP error handler instead.
// See `core/router/handler#FireErrorCode` and `Context.EndRequest` for more details.
//
// Read more at: https://github.com/kataras/iris/issues/1531
//
// Defaults to false.
ResetOnFireErrorCode bool `json:"resetOnFireErrorCode,omitempty" yaml:"ResetOnFireErrorCode" toml:"ResetOnFireErrorCode"`

// EnableOptimization when this field is true
// then the application tries to optimize for the best performance where is possible.
Expand All @@ -848,20 +870,6 @@ type Configuration struct {
// will return an `iris.ErrEmptyForm` on empty request form data.
FireEmptyFormError bool `json:"fireEmptyFormError,omitempty" yaml:"FireEmptyFormError" yaml:"FireEmptyFormError"`

// DisableAutoFireStatusCode if true then it turns off the http error status code handler automatic execution
// from (`context.StatusCodeNotSuccessful`, defaults to < 200 || >= 400).
// If that is false then for a direct error firing, then call the "context#FireStatusCode(statusCode)" manually.
//
// By-default a custom http error handler will be fired when "context.StatusCode(code)" called,
// code should be equal with the result of the the `context.StatusCodeNotSuccessful` in order to be received as an "http error handler".
//
// Developer may want this option to set as true in order to manually call the
// error handlers when needed via "context#FireStatusCode(< 200 || >= 400)".
// HTTP Custom error handlers are being registered via app.OnErrorCode(code, handler)".
//
// Defaults to false.
DisableAutoFireStatusCode bool `json:"disableAutoFireStatusCode,omitempty" yaml:"DisableAutoFireStatusCode" toml:"DisableAutoFireStatusCode"`

// TimeFormat time format for any kind of datetime parsing
// Defaults to "Mon, 02 Jan 2006 15:04:05 GMT".
TimeFormat string `json:"timeFormat,omitempty" yaml:"TimeFormat" toml:"TimeFormat"`
Expand Down Expand Up @@ -1038,19 +1046,28 @@ func (c Configuration) GetFireEmptyFormError() bool {
return c.DisableBodyConsumptionOnUnmarshal
}

// GetDisableAutoFireStatusCode returns the Configuration#DisableAutoFireStatusCode.
// GetDisableAutoFireStatusCode returns the Configuration.DisableAutoFireStatusCode.
// Returns true when the http error status code handler automatic execution turned off.
func (c Configuration) GetDisableAutoFireStatusCode() bool {
return c.DisableAutoFireStatusCode
}

// GetTimeFormat returns the Configuration#TimeFormat,
// GetResetOnFireErrorCode returns the Configuration.ResetOnFireErrorCode.
// Returns true when the router should not respect the handler's error response and
// fire the registered error handler instead.
//
// See https://github.com/kataras/iris/issues/1531
func (c Configuration) GetResetOnFireErrorCode() bool {
return c.ResetOnFireErrorCode
}

// GetTimeFormat returns the Configuration.TimeFormat,
// format for any kind of datetime parsing.
func (c Configuration) GetTimeFormat() string {
return c.TimeFormat
}

// GetCharset returns the Configuration#Charset,
// GetCharset returns the Configuration.Charset,
// the character encoding for various rendering
// used for templates and the rest of the responses.
func (c Configuration) GetCharset() string {
Expand Down Expand Up @@ -1203,6 +1220,14 @@ func WithConfiguration(c Configuration) Configurator {
main.FireMethodNotAllowed = v
}

if v := c.DisableAutoFireStatusCode; v {
main.DisableAutoFireStatusCode = v
}

if v := c.ResetOnFireErrorCode; v {
main.ResetOnFireErrorCode = v
}

if v := c.DisableBodyConsumptionOnUnmarshal; v {
main.DisableBodyConsumptionOnUnmarshal = v
}
Expand All @@ -1211,10 +1236,6 @@ func WithConfiguration(c Configuration) Configurator {
main.FireEmptyFormError = v
}

if v := c.DisableAutoFireStatusCode; v {
main.DisableAutoFireStatusCode = v
}

if v := c.TimeFormat; v != "" {
main.TimeFormat = v
}
Expand Down
8 changes: 6 additions & 2 deletions context/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,12 @@ type Application interface {
// Look core/router/APIBuilder#GetRoutes for more.
GetRoutesReadOnly() []RouteReadOnly

// FireErrorCode executes an error http status code handler
// based on the context's status code.
// FireErrorCode handles the response's error response.
// If `Configuration.ResetOnFireErrorCode()` is true
// and the response writer was a recorder or a gzip writer 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` method.
FireErrorCode(ctx Context)

// RouteExists reports whether a particular route exists
Expand Down
15 changes: 12 additions & 3 deletions context/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,18 @@ type ConfigurationReadOnly interface {
GetForceLowercaseRouting() bool
// GetFireMethodNotAllowed returns the configuration.FireMethodNotAllowed.
GetFireMethodNotAllowed() bool
// GetDisableAutoFireStatusCode returns the configuration.DisableAutoFireStatusCode.
// Returns true when the http error status code handler automatic execution turned off.
GetDisableAutoFireStatusCode() bool
// ResetOnFireErrorCode if true then any previously response body or headers through
// response recorder or gzip writer will be ignored and the router
// will fire the registered (or default) HTTP error handler instead.
// See `core/router/handler#FireErrorCode` and `Context.EndRequest` for more details.
//
// Read more at: https://github.com/kataras/iris/issues/1531
//
// Defaults to false.
GetResetOnFireErrorCode() bool

// GetEnableOptimizations returns whether
// the application has performance optimizations enabled.
Expand All @@ -57,9 +69,6 @@ type ConfigurationReadOnly interface {
// If true then the `context.ReadBody/ReadForm` will return an `iris.ErrEmptyForm`
// on empty request form data.
GetFireEmptyFormError() bool
// GetDisableAutoFireStatusCode returns the configuration.DisableAutoFireStatusCode.
// Returns true when the http error status code handler automatic execution turned off.
GetDisableAutoFireStatusCode() bool

// GetTimeFormat returns the configuration.TimeFormat,
// format for any kind of datetime parsing.
Expand Down
Loading

0 comments on commit d57b502

Please sign in to comment.