-
-
Notifications
You must be signed in to change notification settings - Fork 278
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
Improve error handling on backend and code readability #5197
Comments
I would like to solve this issue. Kindly assign this task to me. |
@joanjeremiah feel free to work on this. |
@tsmock can you take a look here and provide the team some feedback on this error handling scheme under proposal? |
My first response is "LGTM". I spent quite a bit of time tracking down errors when the stack traces weren't being printed, and it looks like #5899 will help fix that once they move over to the specific exceptions. Beyond that:
|
The current approach to handling backend exceptions has resulted in code readability issues and challenges in maintaining the codebase.
1. Fix messed-up code readability #5911, #5909
The way exceptions are currently handled in the code has made it harder to read and comprehend. Combining SubCodes and error messages when raising exceptions can confuse new contributors trying to understand the error-handling logic.
For example, we have an endpoint that updates the project metadata. This is only permitted to users with Project Management permission. To return an error if the request is made by the user without Project Management permission we raise an exception like
ValueError
with the message “This action is only allowed to Project Managers”. Since we also have aSubCode
parameter in the error response we are passing a subcode in the message itself like:On the view function, then we split the error string by
-
to get the subcode and error message like:Proposed Solution:
Create a custom exceptions on which we can easily pass the extra information like sub-code, messages and any other args and the exception class should be able to prepare response based on the information passed.
2. Excessive try-except blocks: #5912
The code contains multiple try-except blocks to handle various types of exceptions. This can lead to code redundancy and reduced clarity, as each exception is managed separately.
For instance, if the view function expects NotFound, ValueError, and DataError exceptions, it handles them individually:
Proposed Solution:
The new custom exceptions should be handled using flask error handling mechanism instead of using try except blocks. All of the unhandled exceptions should be captured by flask generic error handler and return standard error response.
3. More detailed and standardized error response: #5910, #5909
Improving the error response to provide more information is essential. It can include detailed SubCodes, clear error messages, and additional context, making it easier for frontend developers and end-users to understand the issues.
For example, instead of a simple "RESOURCE_NOT_FOUND" SubCode, we can use "PROJECT_NOT_FOUND" and include the project ID in the response:
In case of invalid request we can provide in which field the issue occurred like in case of missing required field we can raise following error response:
4. Separation of error messages from the code: #5909
Storing error messages in a separate JSON file provides a more organized and flexible approach. This allows easy updates and modifications of error messages without modifying the codebase.
Create an error_messages.json file to store all error messages, each with a unique sub-code, descriptive message, and additional information if required.
The text was updated successfully, but these errors were encountered: