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

Define custom exceptions for better handling error response #5899

Merged
merged 4 commits into from
Jun 14, 2023

Conversation

Aadesh-Baral
Copy link
Contributor

@Aadesh-Baral Aadesh-Baral commented Jun 9, 2023

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 from werkzeug.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:

raise NotFound(sub_code="PROJECT_NOT_FOUND", project_id=1)

This will return error response as:

{
  "error": {
    "message": "The requested project was not found in the server."
    "code": 404,
    "sub_code": "PROJECT_NOT_FOUND",
    "details": {"project_id": 1}
}

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.

@Aadesh-Baral Aadesh-Baral self-assigned this Jun 9, 2023
------------------------------------------------------

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.
@Aadesh-Baral Aadesh-Baral force-pushed the enhance/5197-backend-error-handling branch from cd3d550 to 4376028 Compare June 9, 2023 05:04
@Aadesh-Baral Aadesh-Baral changed the title Improvements to backend error handling Define custom exceptions for better handling error response Jun 13, 2023
@Aadesh-Baral Aadesh-Baral force-pushed the enhance/5197-backend-error-handling branch 3 times, most recently from d1ad302 to f1cd93a Compare June 14, 2023 05:18
---------------------------------------
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.
@Aadesh-Baral Aadesh-Baral force-pushed the enhance/5197-backend-error-handling branch from f1cd93a to 912f00f Compare June 14, 2023 05:21
@Aadesh-Baral Aadesh-Baral marked this pull request as ready for review June 14, 2023 05:28
Copy link
Contributor

@d-rita d-rita left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Contributor

@eternaltyro eternaltyro left a 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.

backend/__init__.py Outdated Show resolved Hide resolved
backend/__init__.py Show resolved Hide resolved
@sonarcloud
Copy link

sonarcloud bot commented Jun 14, 2023

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
0.0% 0.0% Duplication

Copy link
Contributor

@eternaltyro eternaltyro left a 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.

@eternaltyro eternaltyro merged commit e6053dc into develop Jun 14, 2023
8 checks passed
@eternaltyro eternaltyro deleted the enhance/5197-backend-error-handling branch June 14, 2023 11:58
@eternaltyro
Copy link
Contributor

Merged.

Aadesh-Baral added a commit that referenced this pull request Jun 21, 2023
------------------------------------
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.
Aadesh-Baral added a commit that referenced this pull request Jun 21, 2023
------------------------------------
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.
Aadesh-Baral added a commit that referenced this pull request Jul 18, 2023
------------------------------------
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.
Aadesh-Baral added a commit that referenced this pull request Aug 7, 2023
------------------------------------
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.
Aadesh-Baral added a commit that referenced this pull request Aug 7, 2023
------------------------------------
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Define custom exceptions for better handling backend errors
3 participants