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
Closed

Error handling in MVC mapper functions #1187

pantonov opened this issue Feb 14, 2019 · 3 comments
Labels
Milestone

Comments

@pantonov
Copy link

pantonov 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 kataras added good first issue A user wrote a good first issue with clear instructions 🪶 type: feature 🕰 status:in-progress ⌨️ prio:normal labels Feb 15, 2019
kataras added a commit that referenced this issue Feb 15, 2019
…rror type as a second output value as requested at: #1187
@kataras
Copy link
Owner

kataras 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
Copy link
Author

@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
Copy link
Owner

kataras 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 as completed Jul 23, 2019
github-actions bot pushed a commit to goproxies/github.com-kataras-iris that referenced this issue Jul 27, 2020
…rror type as a second output value as requested at: kataras#1187

Former-commit-id: 49e29c06aaaa22743354981342c29fc9d5953d0e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants