Skip to content

[BUG] Recorded body overwritten by error status code handler #1531

@feliciegodineau

Description

@feliciegodineau

When the request is being recorded, the body is overwritten before sending the response by the error status code handler. This doesn't happen when the request is not recorded.

package main

import (
	"fmt"

	"github.com/kataras/iris/v12"
)

func main() {
	app := iris.New()
	app.Handle("POST", "/fail", func(ctx iris.Context) {
		ctx.Record()
		ctx.StatusCode(500)
		if _, err := ctx.Recorder().Write([]byte("My error message")); err != nil {
			panic(err)
		}
		ctx.Next()
	})
	app.Run(iris.Addr(":8080"))
}

Response:
Internal Server Error

This can be hotfix by setting the option DisableAutoFireStatusCode to true
But, I think that the request should have the same behaviour when it's being recorded and when it's not.

package main

import (
	"fmt"

	"github.com/kataras/iris/v12"
)

func main() {
	app := iris.New()
	app.Handle("POST", "/fail", func(ctx iris.Context) {
		// ctx.Record()
		ctx.StatusCode(500)
		if _, err := ctx.Recorder().Write([]byte("My error message")); err != nil {
			panic(err)
		}
		ctx.Next()
	})
	app.Run(iris.Addr(":8080"))
}

Response:
My error message

Metadata

Metadata

Assignees

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions