Skip to content

Commit b2c37e1

Browse files
committed
add x/errors.RecoveryHandler
1 parent 06a6e63 commit b2c37e1

File tree

7 files changed

+55
-41
lines changed

7 files changed

+55
-41
lines changed

.github/workflows/codeql-analysis.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ jobs:
4242
uses: actions/checkout@v4
4343

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

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

7777
- name: Perform CodeQL Analysis
78-
uses: github/codeql-action/analyze@v2
78+
uses: github/codeql-action/analyze@v3

HISTORY.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@ Developers are not forced to upgrade if they don't really need it. Upgrade whene
2323

2424
Changes apply to `main` branch.
2525

26+
- Add `x/errors.RecoveryHandler` package-level function.
2627
- 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.
27-
- Add `x/errors.Handler`, `CreateHandler`, `NoContentHandler`, `NoContenetOrNotModifiedHandler` and `ListHandler` ready-to-use handlers for service method calls to Iris Handler.
28+
- Add `x/errors.Handler`, `CreateHandler`, `NoContentHandler`, `NoContentOrNotModifiedHandler` and `ListHandler` ready-to-use handlers for service method calls to Iris Handler.
2829
- 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.
2930
- 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).
3031
- 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).

go.mod

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ require (
2424
github.com/kataras/blocks v0.0.8
2525
github.com/kataras/golog v0.1.11
2626
github.com/kataras/jwt v0.1.12
27-
github.com/kataras/neffos v0.0.22
27+
github.com/kataras/neffos v0.0.23
2828
github.com/kataras/pio v0.0.13
2929
github.com/kataras/sitemap v0.0.6
3030
github.com/kataras/tunnel v0.0.4
@@ -40,7 +40,7 @@ require (
4040
github.com/yosssi/ace v0.0.5
4141
go.etcd.io/bbolt v1.3.8
4242
golang.org/x/crypto v0.18.0
43-
golang.org/x/exp v0.0.0-20240103183307-be819d1f06fc
43+
golang.org/x/exp v0.0.0-20240110193028-0dcbfd608b1e
4444
golang.org/x/net v0.20.0
4545
golang.org/x/sys v0.16.0
4646
golang.org/x/text v0.14.0
@@ -66,25 +66,23 @@ require (
6666
github.com/gobwas/glob v0.2.3 // indirect
6767
github.com/gobwas/httphead v0.1.0 // indirect
6868
github.com/gobwas/pool v0.2.1 // indirect
69-
github.com/gobwas/ws v1.3.0 // indirect
70-
github.com/golang/protobuf v1.5.2 // indirect
69+
github.com/gobwas/ws v1.3.2 // indirect
70+
github.com/golang/protobuf v1.5.0 // indirect
7171
github.com/google/go-querystring v1.1.0 // indirect
7272
github.com/gorilla/css v1.0.0 // indirect
73-
github.com/gorilla/websocket v1.5.0 // indirect
73+
github.com/gorilla/websocket v1.5.1 // indirect
7474
github.com/imkira/go-interpol v1.1.0 // indirect
7575
github.com/iris-contrib/go.uuid v2.0.0+incompatible // indirect
7676
github.com/josharian/intern v1.0.0 // indirect
7777
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 // indirect
7878
github.com/mattn/go-colorable v0.1.13 // indirect
7979
github.com/mattn/go-isatty v0.0.19 // indirect
8080
github.com/mediocregopher/radix/v3 v3.8.1 // indirect
81-
github.com/minio/highwayhash v1.0.2 // indirect
8281
github.com/mitchellh/go-wordwrap v1.0.1 // indirect
8382
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 // indirect
8483
github.com/modern-go/reflect2 v1.0.2 // indirect
85-
github.com/nats-io/jwt/v2 v2.5.3 // indirect
86-
github.com/nats-io/nats.go v1.28.0 // indirect
87-
github.com/nats-io/nkeys v0.4.6 // indirect
84+
github.com/nats-io/nats.go v1.31.0 // indirect
85+
github.com/nats-io/nkeys v0.4.5 // indirect
8886
github.com/nats-io/nuid v1.0.1 // indirect
8987
github.com/nxadm/tail v1.4.11 // indirect
9088
github.com/pkg/errors v0.8.1 // indirect

go.sum

Lines changed: 15 additions & 24 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

iris.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -443,10 +443,10 @@ func (app *Application) GetContextPool() *context.Pool {
443443
//
444444
// type contextErrorHandler struct{}
445445
// func (e *contextErrorHandler) HandleContextError(ctx iris.Context, err error) {
446-
// errors.InvalidArgument.Err(ctx, err)
446+
// errors.HandleError(ctx, err)
447447
// }
448448
// ...
449-
// app.SetContextErrorHandler(new(contextErrorHandler))
449+
// app.SetContextErrorHandler(new(contextErrorHandler))
450450
func (app *Application) SetContextErrorHandler(errHandler context.ErrorHandler) *Application {
451451
app.contextErrorHandler = errHandler
452452
return app

x/errors/context_error_handler.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package errors
33
import "github.com/kataras/iris/v12/context"
44

55
// DefaultContextErrorHandler returns a context error handler
6-
// which fires a JSON bad request (400) error message when
6+
// which calls the HandleError on any incoming error when
77
// a rich rest response failed to be written to the client.
88
// Register it on Application.SetContextErrorHandler method.
99
var DefaultContextErrorHandler context.ErrorHandler = new(jsonErrorHandler)
@@ -13,5 +13,5 @@ type jsonErrorHandler struct{}
1313
// HandleContextError completes the context.ErrorHandler interface. It's fired on
1414
// rich rest response failures.
1515
func (e *jsonErrorHandler) HandleContextError(ctx *context.Context, err error) {
16-
InvalidArgument.Err(ctx, err)
16+
HandleError(ctx, err)
1717
}

x/errors/handlers.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package errors
33
import (
44
stdContext "context"
55
"errors"
6+
"fmt"
67
"io"
78
"net/http"
89

@@ -12,6 +13,29 @@ import (
1213
"golang.org/x/exp/constraints"
1314
)
1415

16+
// RecoveryHandler is a middleware which recovers from panics and sends an appropriate error response
17+
// to the logger and the client.
18+
func RecoveryHandler(ctx *context.Context) {
19+
defer func() {
20+
if rec := recover(); rec != nil {
21+
var err error
22+
switch v := rec.(type) {
23+
case error:
24+
err = v
25+
case string:
26+
err = New(v)
27+
default:
28+
err = fmt.Errorf("%v", v)
29+
}
30+
31+
Internal.LogErr(ctx, err)
32+
ctx.StopExecution()
33+
}
34+
}()
35+
36+
ctx.Next()
37+
}
38+
1539
// Handle handles a generic response and error from a service call and sends a JSON response to the client.
1640
// It returns a boolean value indicating whether the handle was successful or not.
1741
// If the error is not nil, it calls HandleError to send an appropriate error response to the client.

0 commit comments

Comments
 (0)