replace all common error responses with the new Context.StopWithError

Former-commit-id: 99b08a0b5564ef640456db12674cb37721f73ae3
This commit is contained in:
Gerasimos (Makis) Maropoulos 2020-05-18 00:25:38 +03:00
parent 07cd03a674
commit f3745cebbd
14 changed files with 21 additions and 32 deletions

View File

@ -16,6 +16,7 @@ func main() {
ctx.StatusCode(iris.StatusNoContent) ctx.StatusCode(iris.StatusNoContent)
return return
} }
ctx.Next() ctx.Next()
} // or "github.com/iris-contrib/middleware/cors" } // or "github.com/iris-contrib/middleware/cors"
@ -25,8 +26,7 @@ func main() {
var any iris.Map var any iris.Map
err := ctx.ReadJSON(&any) err := ctx.ReadJSON(&any)
if err != nil { if err != nil {
ctx.WriteString(err.Error()) ctx.StopWithError(iris.StatusBadRequest, err)
ctx.StatusCode(iris.StatusBadRequest)
return return
} }
ctx.Application().Logger().Infof("received %#+v", any) ctx.Application().Logger().Infof("received %#+v", any)

View File

@ -56,8 +56,7 @@ func handler(ctx iris.Context) {
// //
if err := ctx.UnmarshalBody(&c, nil); err != nil { if err := ctx.UnmarshalBody(&c, nil); err != nil {
ctx.StatusCode(iris.StatusBadRequest) ctx.StopWithError(iris.StatusBadRequest, err)
ctx.WriteString(err.Error())
return return
} }

View File

@ -64,8 +64,7 @@ func handler(ctx iris.Context) {
// //
if err := ctx.UnmarshalBody(&c, iris.UnmarshalerFunc(yaml.Unmarshal)); err != nil { if err := ctx.UnmarshalBody(&c, iris.UnmarshalerFunc(yaml.Unmarshal)); err != nil {
ctx.StatusCode(iris.StatusBadRequest) ctx.StopWithError(iris.StatusBadRequest, err)
ctx.WriteString(err.Error())
return return
} }

View File

@ -19,8 +19,8 @@ func main() {
app.Get("/", func(ctx iris.Context) { app.Get("/", func(ctx iris.Context) {
if err := ctx.View("form.html"); err != nil { if err := ctx.View("form.html"); err != nil {
ctx.StatusCode(iris.StatusInternalServerError) ctx.StopWithError(iris.StatusInternalServerError, err)
ctx.WriteString(err.Error()) return
} }
}) })
@ -28,8 +28,8 @@ func main() {
visitor := Visitor{} visitor := Visitor{}
err := ctx.ReadForm(&visitor) err := ctx.ReadForm(&visitor)
if err != nil && !iris.IsErrPath(err) /* see: https://github.com/kataras/iris/issues/1157 */ { if err != nil && !iris.IsErrPath(err) /* see: https://github.com/kataras/iris/issues/1157 */ {
ctx.StatusCode(iris.StatusInternalServerError) ctx.StopWithError(iris.StatusInternalServerError, err)
ctx.WriteString(err.Error()) return
} }
ctx.Writef("Visitor: %#v", visitor) ctx.Writef("Visitor: %#v", visitor)

View File

@ -14,8 +14,7 @@ func MyHandler(ctx iris.Context) {
var c Company var c Company
if err := ctx.ReadJSON(&c); err != nil { if err := ctx.ReadJSON(&c); err != nil {
ctx.StatusCode(iris.StatusBadRequest) ctx.StopWithError(iris.StatusBadRequest, err)
ctx.WriteString(err.Error())
return return
} }
@ -33,8 +32,7 @@ func MyHandler2(ctx iris.Context) {
var persons []Person var persons []Person
err := ctx.ReadJSON(&persons) err := ctx.ReadJSON(&persons)
if err != nil { if err != nil {
ctx.StatusCode(iris.StatusBadRequest) ctx.StopWithError(iris.StatusBadRequest, err)
ctx.WriteString(err.Error())
return return
} }

View File

@ -11,8 +11,7 @@ func main() {
// body, err := ioutil.ReadAll(ctx.Request().Body) once or // body, err := ioutil.ReadAll(ctx.Request().Body) once or
body, err := ctx.GetBody() // as many times as you need. body, err := ctx.GetBody() // as many times as you need.
if err != nil { if err != nil {
ctx.StatusCode(iris.StatusInternalServerError) ctx.StopWithError(iris.StatusInternalServerError, err)
ctx.WriteString(err.Error())
return return
} }

View File

@ -15,8 +15,7 @@ func readMsgPack(ctx iris.Context) {
var u User var u User
err := ctx.ReadMsgPack(&u) err := ctx.ReadMsgPack(&u)
if err != nil { if err != nil {
ctx.StatusCode(iris.StatusBadRequest) ctx.StopWithError(iris.StatusBadRequest, err)
ctx.WriteString(err.Error())
return return
} }

View File

@ -17,8 +17,8 @@ func main() {
var t MyType var t MyType
err := ctx.ReadQuery(&t) err := ctx.ReadQuery(&t)
if err != nil && !iris.IsErrPath(err) { if err != nil && !iris.IsErrPath(err) {
ctx.StatusCode(iris.StatusInternalServerError) ctx.StopWithError(iris.StatusInternalServerError, err)
ctx.WriteString(err.Error()) return
} }
ctx.Writef("MyType: %#v", t) ctx.Writef("MyType: %#v", t)

View File

@ -41,8 +41,7 @@ type person struct {
func handler(ctx iris.Context) { func handler(ctx iris.Context) {
var p person var p person
if err := ctx.ReadXML(&p); err != nil { if err := ctx.ReadXML(&p); err != nil {
ctx.StatusCode(iris.StatusBadRequest) ctx.StopWithError(iris.StatusBadRequest, err)
ctx.WriteString(err.Error())
return return
} }

View File

@ -22,8 +22,7 @@ type product struct {
func handler(ctx iris.Context) { func handler(ctx iris.Context) {
var p product var p product
if err := ctx.ReadYAML(&p); err != nil { if err := ctx.ReadYAML(&p); err != nil {
ctx.StatusCode(iris.StatusBadRequest) ctx.StopWithError(iris.StatusBadRequest, err)
ctx.WriteString(err.Error())
return return
} }

View File

@ -52,8 +52,7 @@ func main() {
err := ctx.Request().ParseMultipartForm(maxSize) err := ctx.Request().ParseMultipartForm(maxSize)
if err != nil { if err != nil {
ctx.StatusCode(iris.StatusInternalServerError) ctx.StopWithError(iris.StatusInternalServerError, err)
ctx.WriteString(err.Error())
return return
} }

View File

@ -45,8 +45,8 @@ func main() {
_, err := ctx.Write(buffer.Bytes()) _, err := ctx.Write(buffer.Bytes())
if err != nil { if err != nil {
ctx.StatusCode(iris.StatusInternalServerError) ctx.StopWithError(iris.StatusInternalServerError, err)
ctx.WriteString(err.Error()) return
} }
}) })

View File

@ -1576,8 +1576,8 @@ func (ctx *context) StopWithStatus(statusCode int) {
// 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.
func (ctx *context) StopWithText(statusCode int, plainText string) { func (ctx *context) StopWithText(statusCode int, plainText string) {
ctx.WriteString(plainText)
ctx.StopWithStatus(statusCode) ctx.StopWithStatus(statusCode)
ctx.WriteString(plainText)
} }
// StopWithError stops the handlers chain and writes the "statusCode" // StopWithError stops the handlers chain and writes the "statusCode"

View File

@ -56,9 +56,7 @@ func TestStruct(t *testing.T) {
type testStructErrorHandler struct{} type testStructErrorHandler struct{}
func (s *testStructErrorHandler) HandleError(ctx iris.Context, err error) { func (s *testStructErrorHandler) HandleError(ctx iris.Context, err error) {
ctx.StatusCode(httptest.StatusConflict) ctx.StopWithError(httptest.StatusConflict, err)
ctx.WriteString(err.Error())
ctx.StopExecution()
} }
func (s *testStructErrorHandler) Handle(errText string) error { func (s *testStructErrorHandler) Handle(errText string) error {