Handle Errors
The MVC Application or a Controller's error handling varies based on the type of error and its source. In this section you learn how to handle each error depending of its type (e.g. http error like 404, response error from a method or a failure while a method trying to render a response). A Controller can use all the methods at once when it's necessary.
HTTP Errors
Let's start by the most critual one, handle HTTP errors per Controller.
To register an HTTP error handler the Controller (or one of its embedded fields) MUST contain a method named HandleHTTPError. That method can accept and output any type of arguments to render a response, like the rest of the Controller's method. The HandleHTTPError is automatically called by the Framework on HTTP Errors (client 4xx or server 5xx).
Example Code:
The following code snippet will render "errors/404.html" on 404 Not Found, "errors/500.html" on 500 Internal Server Error and e.t.c.
func (c *Base) HandleHTTPError(err mvc.Err, statusCode mvc.Code) mvc.View {
if err != nil {
// Do something with that error,
// e.g. view.Data = MyPageData{Message: err.Error()}
}
code := int(statusCode) // cast it to int.
return mvc.View{
Code: code,
Name: fmt.Sprintf("errors/%d.html", code),
}
}The input parameter of mvc.Code is optional but a good practise to follow. You could register a Context and get its error code through ctx.GetStatusCode().
This can accept dependencies and output values like any other Controller Method, however be careful if your registered dependencies depend only on successful(200...) requests.
Let's write a method which can fire HandleHTTPError. This is totally optional as HandleHTTPError will be called automatically on HTTP errors (e.g. client 404).
Also note that, if you register more than one HandleHTTPError in the same Party, you need to use the RouteOverlap feature as shown in the authenticated-controller example.
Custom Errors
A Controller's method can return a custom structure which is responsible to render a response based on given data. This error can be an HTTP Error or a view with 2xx Status OK or a redirect response with 3xx status code.
This can be achieved through a custom mvc.Result or mvc.Preflight (as we've shown at the previous sections).
Using a mvc.Result
mvc.ResultGood method to use when mvc.Result is returned from the Controller's method.
Let's create a custom Go structure with some data used to render the response.
Implement the mvc.Result through the Dispatch(iris.Context) method.
Remember: mvc.View, Response and e.t.c are all mvc.Result at the end, so you can call their Dispatch method to render even if a specific method cannot output values (like this
Dispatchone).
Alternatively, using just the Context's methods:
Usage
It's time to use our errorResponse. Let's design a GetBy method which returns mvc.Result, it returns an errorResponse when "user" was not found in our "database".
Using a mvc.Preflight
mvc.PreflightGood method to use when a return value type might be or might not complete mvc.Result or mvc.Preflight interfaces. E.g. a single response which can output data or error. Or even return different types at all, e.g. return a user struct (JSON by default) on valid requests and userError mvc.Preflight on failures. Read more about output values in the dependency injection section.
Complete the mvc.Preflight interface which will run right before the render of response, it can manipulate an object right before it is rendered or handle rendering all by it self by returning the iris.ErrStopExecution error.
Usage
Let's use our response type to a Controller's method. The User structure will be embedded into our response.Data field.
Remember: A structure can complete both mvc.Result and mvc.Preflight interfaces. If Preflight does not return
iris.ErrStopExecutionthen it proceeds with theDispatch(mvc.Result) method one. Here is the underline code.
The above example will render HTML errors when the client accepts html, otherwise will render both error and result as JSON. This method is the recommended one when designing APIs for 3rd-party programmers.
Hijack a Controller Method Result
There is one more option for advanced scenarios. Register a custom ResultHandler when you want to manipulate or change or log each controllers methods return values per MVC Application or globally.
Example Code:
Last updated
Was this helpful?