Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error handling in MVC mapper functions #1187

Closed
pantonov opened this issue Feb 14, 2019 · 3 comments

Comments

@pantonov
Copy link

commented Feb 14, 2019

Consider the following example:

func (c *TodoController) BeforeActivation(b mvc.BeforeActivation) {
    b.Dependencies().Add(func(ctx iris.Context) (items []todo.Item) {
        ctx.ReadJSON(&items)
        return
    })
}

There is no way to process e.g. JSON conversion errors here. What about supporting second error return argument (or int HTTP error code), just like in normal mapped MVC methods, e.g.:

func (c *TodoController) BeforeActivation(b mvc.BeforeActivation) {
    b.Dependencies().Add(func(ctx iris.Context) (items []todo.Item, err error) {
        err = ctx.ReadJSON(&items)
        return
    })
}

when err != nil, status code 400 (+ err.Error() as body) should be sent as HTTP response and MVC method(s) not called.

@kataras kataras added this to the v11.2.0 milestone Feb 15, 2019

kataras added a commit that referenced this issue Feb 15, 2019
add support for mvc and hero dynamic dependencies to understand the e…
…rror type as a second output value as requested at: #1187
@kataras

This comment has been minimized.

Copy link
Owner

commented Feb 15, 2019

@pantonov , I've just added support for second output of error type for the dynamic dependencies ^ for the upcoming v11.2 . Although you could already do that by:

	b.Dependencies().Add(func(ctx iris.Context) (v testCustomStruct) {
		err := ctx.ReadJSON(&v)
		if err != nil {
			ctx.StatusCode(iris.StatusBadRequest)
			ctx.WriteString(err.Error())
                        ctx.StopExecution() // this will stop the execution of a controller's method.
		}
		return
	})
@pantonov

This comment has been minimized.

Copy link
Author

commented Feb 16, 2019

@kataras, that was a first thing I've tried, but ctx.StopExecution() by some reason did not work for me in this context. Controller function was called anyway despite ctx.StopExecution(). So I the only options were to pass error in context (or unexported field of testCustomStruct) and check it later in controller method.

@kataras

This comment has been minimized.

Copy link
Owner

commented Feb 16, 2019

Yes, this will work on the new version so you should be fine, if you don't mind you could try out the upcoming release by installing the v11.2.0 branch and give me more feedback if needed ( I added a _test for that case though)

@kataras kataras closed this Jul 23, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.