mirror of
https://github.com/kataras/iris.git
synced 2025-12-21 11:57:02 +00:00
☕ add a dependency-injection examples folder for the next release and some improvements
Former-commit-id: 040168afb7caf808618f7da5e68ae8eb01cb7170
This commit is contained in:
@@ -18,23 +18,6 @@ func (fn ErrorHandlerFunc) HandleError(ctx context.Context, err error) {
|
||||
fn(ctx, err)
|
||||
}
|
||||
|
||||
var (
|
||||
// DefaultErrStatusCode is the default error status code (400)
|
||||
// when the response contains a non-nil error or a request-scoped binding error occur.
|
||||
DefaultErrStatusCode = 400
|
||||
|
||||
// DefaultErrorHandler is the default error handler which is fired
|
||||
// when a function returns a non-nil error or a request-scoped dependency failed to binded.
|
||||
DefaultErrorHandler = ErrorHandlerFunc(func(ctx context.Context, err error) {
|
||||
if status := ctx.GetStatusCode(); status == 0 || !context.StatusCodeNotSuccessful(status) {
|
||||
ctx.StatusCode(DefaultErrStatusCode)
|
||||
}
|
||||
|
||||
ctx.WriteString(err.Error())
|
||||
ctx.StopExecution()
|
||||
})
|
||||
)
|
||||
|
||||
var (
|
||||
// ErrSeeOther may be returned from a dependency handler to skip a specific dependency
|
||||
// based on custom logic.
|
||||
@@ -45,6 +28,26 @@ var (
|
||||
ErrStopExecution = fmt.Errorf("stop execution")
|
||||
)
|
||||
|
||||
var (
|
||||
// DefaultErrStatusCode is the default error status code (400)
|
||||
// when the response contains a non-nil error or a request-scoped binding error occur.
|
||||
DefaultErrStatusCode = 400
|
||||
|
||||
// DefaultErrorHandler is the default error handler which is fired
|
||||
// when a function returns a non-nil error or a request-scoped dependency failed to binded.
|
||||
DefaultErrorHandler = ErrorHandlerFunc(func(ctx context.Context, err error) {
|
||||
if err != ErrStopExecution {
|
||||
if status := ctx.GetStatusCode(); status == 0 || !context.StatusCodeNotSuccessful(status) {
|
||||
ctx.StatusCode(DefaultErrStatusCode)
|
||||
}
|
||||
|
||||
ctx.WriteString(err.Error())
|
||||
}
|
||||
|
||||
ctx.StopExecution()
|
||||
})
|
||||
)
|
||||
|
||||
func makeHandler(fn interface{}, c *Container) context.Handler {
|
||||
if fn == nil {
|
||||
panic("makeHandler: function is nil")
|
||||
@@ -77,10 +80,12 @@ func makeHandler(fn interface{}, c *Container) context.Handler {
|
||||
if err != nil {
|
||||
if err == ErrSeeOther {
|
||||
continue
|
||||
} else if err == ErrStopExecution {
|
||||
ctx.StopExecution()
|
||||
return // return without error.
|
||||
}
|
||||
// handled inside ErrorHandler.
|
||||
// else if err == ErrStopExecution {
|
||||
// ctx.StopExecution()
|
||||
// return // return without error.
|
||||
// }
|
||||
|
||||
c.GetErrorHandler(ctx).HandleError(ctx, err)
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user