Former-commit-id: c55f1023f4d93f6712c7fa4d299bcf1098872ecf
This commit is contained in:
Gerasimos (Makis) Maropoulos 2020-07-25 09:23:34 +03:00
parent e70ceba051
commit 802348cedd
5 changed files with 68 additions and 1 deletions

View File

@ -371,6 +371,8 @@ Other Improvements:
![DBUG routes](https://iris-go.com/images/v12.2.0-dbug2.png?v=0) ![DBUG routes](https://iris-go.com/images/v12.2.0-dbug2.png?v=0)
- Fix [#1569#issuecomment-663739177](https://github.com/kataras/iris/issues/1569#issuecomment-663739177).
- Fix [#1564](https://github.com/kataras/iris/issues/1564). - Fix [#1564](https://github.com/kataras/iris/issues/1564).
- Fix [#1553](https://github.com/kataras/iris/issues/1553). - Fix [#1553](https://github.com/kataras/iris/issues/1553).

View File

@ -0,0 +1,42 @@
package main
import (
"fmt"
"github.com/kataras/iris/v12"
)
func main() {
app := newApp()
app.Listen(":8080")
}
func newApp() *iris.Application {
app := iris.New()
app.Use(iris.Compression)
app.OnAnyErrorCode(onErrorCode)
app.Get("/", handler)
app.Configure(iris.WithResetOnFireErrorCode)
return app
}
// This is the default error handler Iris uses for any error codes.
func onErrorCode(ctx iris.Context) {
if err := ctx.GetErr(); err != nil {
ctx.WriteString(err.Error())
} else {
ctx.WriteString(iris.StatusText(ctx.GetStatusCode()))
}
}
func handler(ctx iris.Context) {
ctx.Record()
ctx.WriteString("This should NOT be written")
// [....something bad happened after we "write"]
err := fmt.Errorf("custom error")
ctx.StopWithError(iris.StatusBadRequest, err)
}

View File

@ -0,0 +1,14 @@
package main
import (
"testing"
"github.com/kataras/iris/v12/httptest"
)
func TestResetCompressionAndFireError(t *testing.T) { // #1569
app := newApp()
e := httptest.New(t, app)
e.GET("/").Expect().Status(httptest.StatusBadRequest).Body().Equal("custom error")
}

View File

@ -600,6 +600,7 @@ func (ctx *Context) StopWithText(statusCode int, plainText string) {
// StopWithError stops the handlers chain and writes the "statusCode" // StopWithError stops the handlers chain and writes the "statusCode"
// among with the error "err". // among with the error "err".
// It Calls the `SetErr` method so error handlers can access the given error.
// //
// If the status code is a failure one then // If the status code is a failure one then
// it will also fire the specified error code handler. // it will also fire the specified error code handler.
@ -608,6 +609,7 @@ func (ctx *Context) StopWithError(statusCode int, err error) {
return return
} }
ctx.SetErr(err)
ctx.StopWithText(statusCode, err.Error()) ctx.StopWithText(statusCode, err.Error())
} }
@ -4255,7 +4257,9 @@ func (ctx *Context) MaxAge() int64 {
// which can be used to reset the body, reset headers, get the body, // which can be used to reset the body, reset headers, get the body,
// get & set the status code at any time and more. // get & set the status code at any time and more.
func (ctx *Context) Record() { func (ctx *Context) Record() {
if w, ok := ctx.writer.(*responseWriter); ok { switch w := ctx.writer.(type) {
case *ResponseRecorder:
default:
recorder := AcquireResponseRecorder() recorder := AcquireResponseRecorder()
recorder.BeginRecord(w) recorder.BeginRecord(w)
ctx.ResetResponseWriter(recorder) ctx.ResetResponseWriter(recorder)

View File

@ -473,6 +473,11 @@ func (h *routerHandler) FireErrorCode(ctx *context.Context) {
// reset if previous content and it's recorder, keep the status code. // reset if previous content and it's recorder, keep the status code.
w.ClearHeaders() w.ClearHeaders()
w.ResetBody() w.ResetBody()
if cw, ok := w.ResponseWriter.(*context.CompressResponseWriter); ok {
// recorder wraps a compress writer.
cw.Disabled = true
}
} else if w, ok := ctx.ResponseWriter().(*context.CompressResponseWriter); ok { } else if w, ok := ctx.ResponseWriter().(*context.CompressResponseWriter); ok {
// reset and disable the gzip in order to be an expected form of http error result // reset and disable the gzip in order to be an expected form of http error result
w.Disabled = true w.Disabled = true