Skip to content

Commit

Permalink
add x/errors.RecoveryHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
kataras committed Jan 10, 2024
1 parent 06a6e63 commit b2c37e1
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 41 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/codeql-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ jobs:
uses: actions/checkout@v4

- name: Set up Go 1.x
uses: actions/setup-go@v4
uses: actions/setup-go@v5
with:
go-version-file: './go.mod'
check-latest: true
Expand All @@ -61,7 +61,7 @@ jobs:
# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
# If this step fails, then you should remove it and run the build manually (see below)
- name: Autobuild
uses: github/codeql-action/autobuild@v2
uses: github/codeql-action/autobuild@v3

# ℹ️ Command-line programs to run using the OS shell.
# 📚 https://git.io/JvXDl
Expand All @@ -75,4 +75,4 @@ jobs:
# make release

- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v2
uses: github/codeql-action/analyze@v3
3 changes: 2 additions & 1 deletion HISTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ Developers are not forced to upgrade if they don't really need it. Upgrade whene

Changes apply to `main` branch.

- Add `x/errors.RecoveryHandler` package-level function.
- Add `x/errors.Validation` package-level function to add one or more validations for the request payload before a service call of the below methods.
- Add `x/errors.Handler`, `CreateHandler`, `NoContentHandler`, `NoContenetOrNotModifiedHandler` and `ListHandler` ready-to-use handlers for service method calls to Iris Handler.
- Add `x/errors.Handler`, `CreateHandler`, `NoContentHandler`, `NoContentOrNotModifiedHandler` and `ListHandler` ready-to-use handlers for service method calls to Iris Handler.
- Add `x/errors.List` package-level function to support `ListObjects(ctx context.Context, opts pagination.ListOptions, f Filter) ([]Object, int64, error)` type of service calls.
- Simplify how validation errors on `/x/errors` package works. A new `x/errors/validation` sub-package added to make your life easier (using the powerful Generics feature).
- Add `x/errors.OK`, `Create`, `NoContent` and `NoContentOrNotModified` package-level generic functions as custom service method caller helpers. Example can be found [here](_examples/routing/http-wire-errors/service/main.go).
Expand Down
16 changes: 7 additions & 9 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ require (
github.com/kataras/blocks v0.0.8
github.com/kataras/golog v0.1.11
github.com/kataras/jwt v0.1.12
github.com/kataras/neffos v0.0.22
github.com/kataras/neffos v0.0.23
github.com/kataras/pio v0.0.13
github.com/kataras/sitemap v0.0.6
github.com/kataras/tunnel v0.0.4
Expand All @@ -40,7 +40,7 @@ require (
github.com/yosssi/ace v0.0.5
go.etcd.io/bbolt v1.3.8
golang.org/x/crypto v0.18.0
golang.org/x/exp v0.0.0-20240103183307-be819d1f06fc
golang.org/x/exp v0.0.0-20240110193028-0dcbfd608b1e
golang.org/x/net v0.20.0
golang.org/x/sys v0.16.0
golang.org/x/text v0.14.0
Expand All @@ -66,25 +66,23 @@ require (
github.com/gobwas/glob v0.2.3 // indirect
github.com/gobwas/httphead v0.1.0 // indirect
github.com/gobwas/pool v0.2.1 // indirect
github.com/gobwas/ws v1.3.0 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/gobwas/ws v1.3.2 // indirect
github.com/golang/protobuf v1.5.0 // indirect
github.com/google/go-querystring v1.1.0 // indirect
github.com/gorilla/css v1.0.0 // indirect
github.com/gorilla/websocket v1.5.0 // indirect
github.com/gorilla/websocket v1.5.1 // indirect
github.com/imkira/go-interpol v1.1.0 // indirect
github.com/iris-contrib/go.uuid v2.0.0+incompatible // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/mediocregopher/radix/v3 v3.8.1 // indirect
github.com/minio/highwayhash v1.0.2 // indirect
github.com/mitchellh/go-wordwrap v1.0.1 // indirect
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/nats-io/jwt/v2 v2.5.3 // indirect
github.com/nats-io/nats.go v1.28.0 // indirect
github.com/nats-io/nkeys v0.4.6 // indirect
github.com/nats-io/nats.go v1.31.0 // indirect
github.com/nats-io/nkeys v0.4.5 // indirect
github.com/nats-io/nuid v1.0.1 // indirect
github.com/nxadm/tail v1.4.11 // indirect
github.com/pkg/errors v0.8.1 // indirect
Expand Down
39 changes: 15 additions & 24 deletions go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions iris.go
Original file line number Diff line number Diff line change
Expand Up @@ -443,10 +443,10 @@ func (app *Application) GetContextPool() *context.Pool {
//
// type contextErrorHandler struct{}
// func (e *contextErrorHandler) HandleContextError(ctx iris.Context, err error) {
// errors.InvalidArgument.Err(ctx, err)
// errors.HandleError(ctx, err)
// }
// ...
// app.SetContextErrorHandler(new(contextErrorHandler))
// app.SetContextErrorHandler(new(contextErrorHandler))
func (app *Application) SetContextErrorHandler(errHandler context.ErrorHandler) *Application {
app.contextErrorHandler = errHandler
return app
Expand Down
4 changes: 2 additions & 2 deletions x/errors/context_error_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package errors
import "github.com/kataras/iris/v12/context"

// DefaultContextErrorHandler returns a context error handler
// which fires a JSON bad request (400) error message when
// which calls the HandleError on any incoming error when
// a rich rest response failed to be written to the client.
// Register it on Application.SetContextErrorHandler method.
var DefaultContextErrorHandler context.ErrorHandler = new(jsonErrorHandler)
Expand All @@ -13,5 +13,5 @@ type jsonErrorHandler struct{}
// HandleContextError completes the context.ErrorHandler interface. It's fired on
// rich rest response failures.
func (e *jsonErrorHandler) HandleContextError(ctx *context.Context, err error) {
InvalidArgument.Err(ctx, err)
HandleError(ctx, err)
}
24 changes: 24 additions & 0 deletions x/errors/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package errors
import (
stdContext "context"
"errors"
"fmt"
"io"
"net/http"

Expand All @@ -12,6 +13,29 @@ import (
"golang.org/x/exp/constraints"
)

// RecoveryHandler is a middleware which recovers from panics and sends an appropriate error response
// to the logger and the client.
func RecoveryHandler(ctx *context.Context) {
defer func() {
if rec := recover(); rec != nil {
var err error
switch v := rec.(type) {
case error:
err = v
case string:
err = New(v)
default:
err = fmt.Errorf("%v", v)
}

Internal.LogErr(ctx, err)
ctx.StopExecution()
}
}()

ctx.Next()
}

// Handle handles a generic response and error from a service call and sends a JSON response to the client.
// It returns a boolean value indicating whether the handle was successful or not.
// If the error is not nil, it calls HandleError to send an appropriate error response to the client.
Expand Down

0 comments on commit b2c37e1

Please sign in to comment.