-
-
Notifications
You must be signed in to change notification settings - Fork 263
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
Define custom exceptions for better handling error response #5899
Conversation
------------------------------------------------------ This commit adds custom exception classes and error handling logic to the project. The new classes extend the `BaseException` class from `werkzeug.exceptions.HTTPException` and provide additional functionality for handling different types of errors. The custom exception classes included in this commit are: - `BadRequest`: Represents a bad request error (400). - `Unauthorized`: Represents an unauthorized access error (401). - `Forbidden`: Represents a forbidden access error (403). - `NotFound`: Represents a resource not found error (404). - `Conflict`: Represents a conflict error (409). Each exception class accepts optional parameters for sub-code, message, and additional details. If not provided, default values are used. The error messages are retrieved from the `ERROR_MESSAGES` dictionary defined in the `backend` module. The commit also includes the `to_dict()` method in the `BaseException` class, which converts the exception object to a dictionary representation containing error code, sub-code, message, and details. These custom exception classes enhance the error handling capabilities of the project and provide a standardized way to handle and communicate various types of errors.
---------------------------------------------------- As newly defined custom exceptions are not explicitly handled within the code (because Flask's error handler handles them as they are inherited from Werkzeug's HTTPException), they will be reported as unhandled errors in Sentry. This will result in a significant amount of noise on the Sentry dashboard. To reduce this noise, we need to ignore all these handled errors and exclude them from being reported to Sentry.
cd3d550
to
4376028
Compare
d1ad302
to
f1cd93a
Compare
--------------------------------------- On the app, there may be some unhandled exceptions. A error handler is defined that collects all of these unhandled errors and returns the error message in the desired format.
f1cd93a
to
912f00f
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please fix the file.close() issue. Otherwise, good to go.
Kudos, SonarCloud Quality Gate passed! 0 Bugs No Coverage information |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. Good to go.
Merged. |
------------------------------------ We defined custom HttpExceptions on #5899 to better handle errors but these exceptions were not used anywhere. This commit has replaced the previous NotFound custom exception which was simply a basic exceptio class with the new implementation.
------------------------------------ We defined custom HttpExceptions on #5899 to better handle errors but these exceptions were not used anywhere. This commit has replaced the previous NotFound custom exception which was simply a basic exceptio class with the new implementation.
------------------------------------ We defined custom HttpExceptions on #5899 to better handle errors but these exceptions were not used anywhere. This commit has replaced the previous NotFound custom exception which was simply a basic exceptio class with the new implementation.
------------------------------------ We defined custom HttpExceptions on #5899 to better handle errors but these exceptions were not used anywhere. This commit has replaced the previous NotFound custom exception which was simply a basic exceptio class with the new implementation.
------------------------------------ We defined custom HttpExceptions on #5899 to better handle errors but these exceptions were not used anywhere. This commit has replaced the previous NotFound custom exception which was simply a basic exceptio class with the new implementation.
closes #5909
This pull request (PR) introduces the necessary infrastructure to enhance backend error handling in the project. It includes the implementation of custom exception classes that extend the
BaseException
class fromwerkzeug.exceptions.HTTPException
. These custom exceptions offer additional features for handling various types of errors effectively.Moreover, this PR also introduces a mechanism to separate error messages from the code. The error messages are now stored in a JSON file named "error_messages.json". This approach simplifies the management and updates of error messages independently, providing more flexibility in maintaining and modifying them
Example:
We can raise a NotFound Exception as:
This will return error response as:
For more info please have a look at individual commit messages.
NOTE: This PR mainly focuses on setting up the infrastructure in order to better error handling; Hence these custom exceptions aren't used to handle errors.