Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Former-commit-id: c55f1023f4d93f6712c7fa4d299bcf1098872ecf
  • Loading branch information
kataras committed Jul 25, 2020
1 parent e70ceba commit 802348c
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 1 deletion.
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)

- Fix [#1569#issuecomment-663739177](https://github.com/kataras/iris/issues/1569#issuecomment-663739177).

- Fix [#1564](https://github.com/kataras/iris/issues/1564).

- Fix [#1553](https://github.com/kataras/iris/issues/1553).
Expand Down
42 changes: 42 additions & 0 deletions _examples/routing/http-errors/reset-body/main.go
Original file line number Diff line number Diff line change
@@ -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)
}
14 changes: 14 additions & 0 deletions _examples/routing/http-errors/reset-body/main_test.go
Original file line number Diff line number Diff line change
@@ -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")
}
6 changes: 5 additions & 1 deletion context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,7 @@ func (ctx *Context) StopWithText(statusCode int, plainText string) {

// StopWithError stops the handlers chain and writes the "statusCode"
// 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
// it will also fire the specified error code handler.
Expand All @@ -608,6 +609,7 @@ func (ctx *Context) StopWithError(statusCode int, err error) {
return
}

ctx.SetErr(err)
ctx.StopWithText(statusCode, err.Error())
}

Expand Down Expand Up @@ -4255,7 +4257,9 @@ func (ctx *Context) MaxAge() int64 {
// which can be used to reset the body, reset headers, get the body,
// get & set the status code at any time and more.
func (ctx *Context) Record() {
if w, ok := ctx.writer.(*responseWriter); ok {
switch w := ctx.writer.(type) {
case *ResponseRecorder:
default:
recorder := AcquireResponseRecorder()
recorder.BeginRecord(w)
ctx.ResetResponseWriter(recorder)
Expand Down
5 changes: 5 additions & 0 deletions core/router/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,11 @@ func (h *routerHandler) FireErrorCode(ctx *context.Context) {
// reset if previous content and it's recorder, keep the status code.
w.ClearHeaders()
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 {
// reset and disable the gzip in order to be an expected form of http error result
w.Disabled = true
Expand Down

0 comments on commit 802348c

Please sign in to comment.