diff --git a/_examples/experimental-handlers/cors/simple/main.go b/_examples/experimental-handlers/cors/simple/main.go index 4f6667ff..7d80caa9 100644 --- a/_examples/experimental-handlers/cors/simple/main.go +++ b/_examples/experimental-handlers/cors/simple/main.go @@ -16,6 +16,7 @@ func main() { ctx.StatusCode(iris.StatusNoContent) return } + ctx.Next() } // or "github.com/iris-contrib/middleware/cors" @@ -25,8 +26,7 @@ func main() { var any iris.Map err := ctx.ReadJSON(&any) if err != nil { - ctx.WriteString(err.Error()) - ctx.StatusCode(iris.StatusBadRequest) + ctx.StopWithError(iris.StatusBadRequest, err) return } ctx.Application().Logger().Infof("received %#+v", any) diff --git a/_examples/http_request/read-custom-per-type/main.go b/_examples/http_request/read-custom-per-type/main.go index e03d172a..54c03d1d 100644 --- a/_examples/http_request/read-custom-per-type/main.go +++ b/_examples/http_request/read-custom-per-type/main.go @@ -56,8 +56,7 @@ func handler(ctx iris.Context) { // if err := ctx.UnmarshalBody(&c, nil); err != nil { - ctx.StatusCode(iris.StatusBadRequest) - ctx.WriteString(err.Error()) + ctx.StopWithError(iris.StatusBadRequest, err) return } diff --git a/_examples/http_request/read-custom-via-unmarshaler/main.go b/_examples/http_request/read-custom-via-unmarshaler/main.go index 13e22bcc..ad0d1332 100644 --- a/_examples/http_request/read-custom-via-unmarshaler/main.go +++ b/_examples/http_request/read-custom-via-unmarshaler/main.go @@ -64,8 +64,7 @@ func handler(ctx iris.Context) { // if err := ctx.UnmarshalBody(&c, iris.UnmarshalerFunc(yaml.Unmarshal)); err != nil { - ctx.StatusCode(iris.StatusBadRequest) - ctx.WriteString(err.Error()) + ctx.StopWithError(iris.StatusBadRequest, err) return } diff --git a/_examples/http_request/read-form/main.go b/_examples/http_request/read-form/main.go index 441bb8ab..1e309326 100644 --- a/_examples/http_request/read-form/main.go +++ b/_examples/http_request/read-form/main.go @@ -19,8 +19,8 @@ func main() { app.Get("/", func(ctx iris.Context) { if err := ctx.View("form.html"); err != nil { - ctx.StatusCode(iris.StatusInternalServerError) - ctx.WriteString(err.Error()) + ctx.StopWithError(iris.StatusInternalServerError, err) + return } }) @@ -28,8 +28,8 @@ func main() { visitor := Visitor{} err := ctx.ReadForm(&visitor) if err != nil && !iris.IsErrPath(err) /* see: https://github.com/kataras/iris/issues/1157 */ { - ctx.StatusCode(iris.StatusInternalServerError) - ctx.WriteString(err.Error()) + ctx.StopWithError(iris.StatusInternalServerError, err) + return } ctx.Writef("Visitor: %#v", visitor) diff --git a/_examples/http_request/read-json/main.go b/_examples/http_request/read-json/main.go index 692ec87e..fee580f5 100644 --- a/_examples/http_request/read-json/main.go +++ b/_examples/http_request/read-json/main.go @@ -14,8 +14,7 @@ func MyHandler(ctx iris.Context) { var c Company if err := ctx.ReadJSON(&c); err != nil { - ctx.StatusCode(iris.StatusBadRequest) - ctx.WriteString(err.Error()) + ctx.StopWithError(iris.StatusBadRequest, err) return } @@ -33,8 +32,7 @@ func MyHandler2(ctx iris.Context) { var persons []Person err := ctx.ReadJSON(&persons) if err != nil { - ctx.StatusCode(iris.StatusBadRequest) - ctx.WriteString(err.Error()) + ctx.StopWithError(iris.StatusBadRequest, err) return } diff --git a/_examples/http_request/read-many/main.go b/_examples/http_request/read-many/main.go index b529fb0f..cc22d775 100644 --- a/_examples/http_request/read-many/main.go +++ b/_examples/http_request/read-many/main.go @@ -11,8 +11,7 @@ func main() { // body, err := ioutil.ReadAll(ctx.Request().Body) once or body, err := ctx.GetBody() // as many times as you need. if err != nil { - ctx.StatusCode(iris.StatusInternalServerError) - ctx.WriteString(err.Error()) + ctx.StopWithError(iris.StatusInternalServerError, err) return } diff --git a/_examples/http_request/read-msgpack/main.go b/_examples/http_request/read-msgpack/main.go index 785a9a68..c5d5c88e 100644 --- a/_examples/http_request/read-msgpack/main.go +++ b/_examples/http_request/read-msgpack/main.go @@ -15,8 +15,7 @@ func readMsgPack(ctx iris.Context) { var u User err := ctx.ReadMsgPack(&u) if err != nil { - ctx.StatusCode(iris.StatusBadRequest) - ctx.WriteString(err.Error()) + ctx.StopWithError(iris.StatusBadRequest, err) return } diff --git a/_examples/http_request/read-query/main.go b/_examples/http_request/read-query/main.go index 86386874..ec6e8f8b 100644 --- a/_examples/http_request/read-query/main.go +++ b/_examples/http_request/read-query/main.go @@ -17,8 +17,8 @@ func main() { var t MyType err := ctx.ReadQuery(&t) if err != nil && !iris.IsErrPath(err) { - ctx.StatusCode(iris.StatusInternalServerError) - ctx.WriteString(err.Error()) + ctx.StopWithError(iris.StatusInternalServerError, err) + return } ctx.Writef("MyType: %#v", t) diff --git a/_examples/http_request/read-xml/main.go b/_examples/http_request/read-xml/main.go index def65876..9b42e9ae 100644 --- a/_examples/http_request/read-xml/main.go +++ b/_examples/http_request/read-xml/main.go @@ -41,8 +41,7 @@ type person struct { func handler(ctx iris.Context) { var p person if err := ctx.ReadXML(&p); err != nil { - ctx.StatusCode(iris.StatusBadRequest) - ctx.WriteString(err.Error()) + ctx.StopWithError(iris.StatusBadRequest, err) return } diff --git a/_examples/http_request/read-yaml/main.go b/_examples/http_request/read-yaml/main.go index 27b9e965..b353e94c 100644 --- a/_examples/http_request/read-yaml/main.go +++ b/_examples/http_request/read-yaml/main.go @@ -22,8 +22,7 @@ type product struct { func handler(ctx iris.Context) { var p product if err := ctx.ReadYAML(&p); err != nil { - ctx.StatusCode(iris.StatusBadRequest) - ctx.WriteString(err.Error()) + ctx.StopWithError(iris.StatusBadRequest, err) return } diff --git a/_examples/http_request/upload-files/main.go b/_examples/http_request/upload-files/main.go index e18dff28..9545dac0 100644 --- a/_examples/http_request/upload-files/main.go +++ b/_examples/http_request/upload-files/main.go @@ -52,8 +52,7 @@ func main() { err := ctx.Request().ParseMultipartForm(maxSize) if err != nil { - ctx.StatusCode(iris.StatusInternalServerError) - ctx.WriteString(err.Error()) + ctx.StopWithError(iris.StatusInternalServerError, err) return } diff --git a/_examples/http_responsewriter/herotemplate/app.go b/_examples/http_responsewriter/herotemplate/app.go index 429e4d6a..93d9c06a 100644 --- a/_examples/http_responsewriter/herotemplate/app.go +++ b/_examples/http_responsewriter/herotemplate/app.go @@ -45,8 +45,8 @@ func main() { _, err := ctx.Write(buffer.Bytes()) if err != nil { - ctx.StatusCode(iris.StatusInternalServerError) - ctx.WriteString(err.Error()) + ctx.StopWithError(iris.StatusInternalServerError, err) + return } }) diff --git a/context/context.go b/context/context.go index 70d708b8..c7af6b47 100644 --- a/context/context.go +++ b/context/context.go @@ -1576,8 +1576,8 @@ func (ctx *context) StopWithStatus(statusCode int) { // If the status code is a failure one then // it will also fire the specified error code handler. func (ctx *context) StopWithText(statusCode int, plainText string) { - ctx.WriteString(plainText) ctx.StopWithStatus(statusCode) + ctx.WriteString(plainText) } // StopWithError stops the handlers chain and writes the "statusCode" diff --git a/hero/struct_test.go b/hero/struct_test.go index 48ece882..5401f7bd 100644 --- a/hero/struct_test.go +++ b/hero/struct_test.go @@ -56,9 +56,7 @@ func TestStruct(t *testing.T) { type testStructErrorHandler struct{} func (s *testStructErrorHandler) HandleError(ctx iris.Context, err error) { - ctx.StatusCode(httptest.StatusConflict) - ctx.WriteString(err.Error()) - ctx.StopExecution() + ctx.StopWithError(httptest.StatusConflict, err) } func (s *testStructErrorHandler) Handle(errText string) error {