Skip to content

Commit

Permalink
replace all common error responses with the new Context.StopWithError
Browse files Browse the repository at this point in the history
Former-commit-id: 99b08a0b5564ef640456db12674cb37721f73ae3
  • Loading branch information
kataras committed May 17, 2020
1 parent 07cd03a commit f3745ce
Show file tree
Hide file tree
Showing 14 changed files with 21 additions and 32 deletions.
4 changes: 2 additions & 2 deletions _examples/experimental-handlers/cors/simple/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ func main() {
ctx.StatusCode(iris.StatusNoContent)
return
}

ctx.Next()
} // or "github.com/iris-contrib/middleware/cors"

Expand All @@ -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)
Expand Down
3 changes: 1 addition & 2 deletions _examples/http_request/read-custom-per-type/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
3 changes: 1 addition & 2 deletions _examples/http_request/read-custom-via-unmarshaler/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
8 changes: 4 additions & 4 deletions _examples/http_request/read-form/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,17 @@ 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
}
})

app.Post("/form_action", func(ctx iris.Context) {
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)
Expand Down
6 changes: 2 additions & 4 deletions _examples/http_request/read-json/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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
}

Expand Down
3 changes: 1 addition & 2 deletions _examples/http_request/read-many/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
3 changes: 1 addition & 2 deletions _examples/http_request/read-msgpack/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
4 changes: 2 additions & 2 deletions _examples/http_request/read-query/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 1 addition & 2 deletions _examples/http_request/read-xml/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
3 changes: 1 addition & 2 deletions _examples/http_request/read-yaml/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
3 changes: 1 addition & 2 deletions _examples/http_request/upload-files/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
4 changes: 2 additions & 2 deletions _examples/http_responsewriter/herotemplate/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
})

Expand Down
2 changes: 1 addition & 1 deletion context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
4 changes: 1 addition & 3 deletions hero/struct_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit f3745ce

Please sign in to comment.