1
0
mirror of https://github.com/kataras/iris.git synced 2026-01-06 03:27:27 +00:00

add a dependency-injection examples folder for the next release and some improvements

Former-commit-id: 040168afb7caf808618f7da5e68ae8eb01cb7170
This commit is contained in:
Gerasimos (Makis) Maropoulos
2020-03-01 02:17:19 +02:00
parent 5fc24812bc
commit ce2eae9121
19 changed files with 214 additions and 76 deletions

View File

@@ -225,7 +225,7 @@ func dispatchFuncResult(ctx context.Context, values []reflect.Value) error {
continue
}
if statusCode < 400 {
if statusCode < 400 && value != ErrStopExecution {
statusCode = DefaultErrStatusCode
}
@@ -286,7 +286,11 @@ func dispatchCommon(ctx context.Context,
if contentType == "" {
// to respect any ctx.ContentType(...) call
// especially if v is not nil.
contentType = ctx.GetContentType()
if contentType = ctx.GetContentType(); contentType == "" {
// if it's still empty set to JSON. (useful for dynamic middlewares that returns an int status code and the next handler dispatches the JSON,
// see dependency-injection/basic/middleware example)
contentType = context.ContentJSONHeaderValue
}
}
if v != nil {
@@ -302,10 +306,13 @@ func dispatchCommon(ctx context.Context,
if strings.HasPrefix(contentType, context.ContentJavascriptHeaderValue) {
_, err = ctx.JSONP(v)
} else if strings.HasPrefix(contentType, context.ContentXMLHeaderValue) {
_, err = ctx.XML(v, context.XML{Indent: " "})
_, err = ctx.XML(v)
// no need: context.XML{Indent: " "}), after v12.2,
// if not iris.WithOptimizations passed and indent is empty then it sets it to two spaces for JSON, JSONP and XML,
// otherwise given indentation.
} else {
// defaults to json if content type is missing or its application/json.
_, err = ctx.JSON(v, context.JSON{Indent: " "})
_, err = ctx.JSON(v)
}
return err

View File

@@ -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

View File

@@ -156,7 +156,7 @@ before begin the implementation of it.
b.Register("user_dep", func(db myDB) User{...}).DependsOn("db")
b.Handler(func(user User) error{...})
b.Handler(func(ctx iris.Context, reuseDB myDB) {...})
Why linked over automatically? Because more thna one dependency can implement the same input and
Why linked over automatically? Because more than one dependency can implement the same input and
end-user does not care about ordering the registered ones.
Link with `DependsOn` SHOULD be optional, if exists then limit the available dependencies,
`DependsOn` SHOULD accept comma-separated values, e.g. "db, otherdep" and SHOULD also work
@@ -170,6 +170,8 @@ so, in theory, end-developers could achieve same results by hand-code(inside the
26 Feb 2020. Gerasimos Maropoulos
______________________________________________
29 Feb 2020. It's done.
*/
type testMessage struct {