Could your team benefit from an addition of a proficient coder with unambiguous communication and dry humor? Connect with me.
Turbo Tickets is an issue tracker app I built using C# .NET while following the MVC pattern.
As my capstone project Turbo Tickets grew in functionality, I knew that the scope for errors was also increasing. As a result, error handling became of utmost importance. I achieved that by implementing 3 layers that could catch errors.
By default, I had the option to return `NotFound()` in catch blocks but that did not feel too intentional. I designed custom error pages or “views” in my MVC app that corresponded to specific errors.
Next, I had to add logic to redirect the viewers to these error pages.
Service Layer: I implemented a service layer interface that had all the databse querying methods. So any query exception in this layer, could cause errors for the users. I added try catch blocks here and in the catch blocks, I'd throw an exception so that the code execution would go back to the code that was calling the service method.
Controller:
- Service Layer Error: In my controller actions, I again had try/catch blocks and an exception thrown in the service layer, would then throw an exception in my controller method too, triggering the catch block in my controller method. In these catch blocks, I would redirect the viewer to a Generic Error page.
- Access Denied Error: I also have logic in my methods which checks whether the logged in user is authorized to make the request. If not, there's no exception thrown but the user is redirected to a page showing an “Access Denied” error.
- Null Check Error: Finally, I also added checks for any integral data having a null value. If such values are found to be null, I'd redirect the viewer to an error page. Only if the object would have non null data, would the viewer be directed to the corresponding view.
MiddleWare: Lastly, I also have a middleware method that takes in a parameter representing the http request/response object. The method waits for the request to be resolved. If the response's status code is 404, the user is redirected to a “not found” error view. This would be helpful if a user requested a url that doesn't correspond to a valid controller action. If the response's status code is 500, the user is redirected to a “generic” error.
With great functionalities, come great responsibilities to handle errors and I only considered the project complete once I implemented the error handling to my satisfaction.
0 Comments