mirror of
https://github.com/kataras/iris.git
synced 2025-12-17 09:57:01 +00:00
@@ -114,14 +114,23 @@ type compatibleErr interface {
|
||||
Error() string
|
||||
}
|
||||
|
||||
// dispatchErr writes the error to the response.
|
||||
// dispatchErr sets the error status code
|
||||
// and the error value to the context.
|
||||
// The APIBuilder's On(Any)ErrorCode is responsible to render this error code.
|
||||
func dispatchErr(ctx *context.Context, status int, err error) bool {
|
||||
if err == nil {
|
||||
return false
|
||||
}
|
||||
|
||||
ctx.StatusCode(status)
|
||||
DefaultErrorHandler.HandleError(ctx, err)
|
||||
if err != ErrStopExecution {
|
||||
if status == 0 || !context.StatusCodeNotSuccessful(status) {
|
||||
status = DefaultErrStatusCode
|
||||
}
|
||||
|
||||
ctx.StatusCode(status)
|
||||
}
|
||||
|
||||
ctx.SetErr(err)
|
||||
return true
|
||||
}
|
||||
|
||||
|
||||
@@ -258,3 +258,24 @@ func TestPreflightResult(t *testing.T) {
|
||||
e.POST("/alternative").WithJSON(testInput{expected4.Name}).
|
||||
Expect().Status(httptest.StatusAccepted).JSON().Equal(expected4)
|
||||
}
|
||||
|
||||
func TestResponseErr(t *testing.T) {
|
||||
app := iris.New()
|
||||
var expectedErr = errors.New("response error")
|
||||
|
||||
app.OnAnyErrorCode(func(ctx iris.Context) {
|
||||
err := ctx.GetErr()
|
||||
if err != expectedErr {
|
||||
t.Fatalf("expected error value does not match")
|
||||
}
|
||||
|
||||
ctx.WriteString(err.Error())
|
||||
})
|
||||
|
||||
app.ConfigureContainer().Get("/", func() Response {
|
||||
return Response{Code: iris.StatusBadGateway, Err: expectedErr}
|
||||
})
|
||||
|
||||
e := httptest.New(t, app)
|
||||
e.GET("/").Expect().Status(iris.StatusBadGateway).Body().Equal("response error")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user