-
Notifications
You must be signed in to change notification settings - Fork 1
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
feat: Added function to generate error dict #51
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -1,18 +1,63 @@ | ||||||||||||||
from typing import Any | ||||||||||||||
from typing import Dict | ||||||||||||||
|
||||||||||||||
from flask import Response | ||||||||||||||
from flask import jsonify | ||||||||||||||
|
||||||||||||||
from flask_utils.errors.base_class import _BaseFlaskException | ||||||||||||||
|
||||||||||||||
|
||||||||||||||
def _generate_error_json(error: _BaseFlaskException, status_code: int) -> Response: | ||||||||||||||
def _generate_error_dict(error: _BaseFlaskException) -> Dict[str, Any]: | ||||||||||||||
""" | ||||||||||||||
This function is used to generate a json of the error passed | ||||||||||||||
This function is used to generate a dict of the error passed | ||||||||||||||
|
||||||||||||||
:param error: The error containing the message and solution | ||||||||||||||
:type error: _BaseFlaskException | ||||||||||||||
|
||||||||||||||
:param status_code: The status code of the error. | ||||||||||||||
:type status_code: int | ||||||||||||||
:return: Returns a dict containing a json representation of the error | ||||||||||||||
:rtype: dict | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Align The Apply this diff to adjust the docstring: - :rtype: dict
+ :rtype: Dict[str, Any] Committable suggestion
Suggested change
|
||||||||||||||
|
||||||||||||||
:Example: | ||||||||||||||
|
||||||||||||||
.. code-block:: python | ||||||||||||||
|
||||||||||||||
from flask_utils import ConflictError # or any other error | ||||||||||||||
from flask_utils.errors._error_template import _generate_error_json | ||||||||||||||
|
||||||||||||||
json = _generate_error_json(error) | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Update function references in The example code in the docstring still uses the old function name Apply this diff to correct the function name in the example and ensure consistency: from flask_utils import ConflictError # or any other error
- from flask_utils.errors._error_template import _generate_error_json
+ from flask_utils.errors._error_template import _generate_error_dict
- json = _generate_error_json(error)
+ json = _generate_error_dict(error) Also applies to: 43-52 |
||||||||||||||
# Sample output: | ||||||||||||||
# { | ||||||||||||||
# "success": False, | ||||||||||||||
# "error": { | ||||||||||||||
# "type": "ConflictError", | ||||||||||||||
# "name": "ConflictError", | ||||||||||||||
# "message": "This is the message", | ||||||||||||||
# "solution": "This is the solution" | ||||||||||||||
# }, | ||||||||||||||
# "code": 409 | ||||||||||||||
# } | ||||||||||||||
|
||||||||||||||
.. versionadded:: 0.8.0 | ||||||||||||||
""" | ||||||||||||||
|
||||||||||||||
return { | ||||||||||||||
"success": False, | ||||||||||||||
"error": { | ||||||||||||||
"type": error.__class__.__name__, | ||||||||||||||
"name": error.name, | ||||||||||||||
"message": error.msg, | ||||||||||||||
"solution": error.solution, | ||||||||||||||
}, | ||||||||||||||
"code": error.status_code, | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
|
||||||||||||||
def _generate_error_response(error: _BaseFlaskException) -> Response: | ||||||||||||||
""" | ||||||||||||||
This function is used to generate a json of the error passed | ||||||||||||||
|
||||||||||||||
:param error: The error containing the message and solution | ||||||||||||||
:type error: _BaseFlaskException | ||||||||||||||
|
||||||||||||||
:return: Returns a json containing all the info | ||||||||||||||
:rtype: flask.Response | ||||||||||||||
|
@@ -34,19 +79,14 @@ class MyError(_BaseFlaskException): | |||||||||||||
|
||||||||||||||
json = _generate_error_json(error, 666) | ||||||||||||||
|
||||||||||||||
.. versionchanged:: 0.8.0 | ||||||||||||||
This function was renamed from ``generate_error_json`` to ``_generate_error_response``. | ||||||||||||||
It now returns a ``flask.Response`` object, calling | ||||||||||||||
:func:`~flask_utils.errors._error_template._generate_error_json` to generate the json. | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Correct function reference in In the Apply this diff to fix the function reference: It now returns a ``flask.Response`` object, calling
- :func:`~flask_utils.errors._error_template._generate_error_json` to generate the json.
+ :func:`~flask_utils.errors._error_template._generate_error_dict` to generate the error dictionary. Committable suggestion
Suggested change
|
||||||||||||||
|
||||||||||||||
.. versionadded:: 0.1.0 | ||||||||||||||
""" | ||||||||||||||
success = False | ||||||||||||||
json = { | ||||||||||||||
"success": success, | ||||||||||||||
"error": { | ||||||||||||||
"type": error.__class__.__name__, | ||||||||||||||
"name": error.name, | ||||||||||||||
"message": error.msg, | ||||||||||||||
"solution": error.solution, | ||||||||||||||
}, | ||||||||||||||
"code": status_code, | ||||||||||||||
} | ||||||||||||||
json = _generate_error_dict(error) | ||||||||||||||
resp: Response = jsonify(json) | ||||||||||||||
resp.status_code = status_code | ||||||||||||||
resp.status_code = error.status_code | ||||||||||||||
return resp |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -5,14 +5,17 @@ class _BaseFlaskException(Exception): | |||||||||||||||||||
""" | ||||||||||||||||||||
This is the base class for all the exceptions in this package. | ||||||||||||||||||||
|
||||||||||||||||||||
:param msg: The message to be displayed in the error. | ||||||||||||||||||||
:param name: The name of the error | ||||||||||||||||||||
:type name: str | ||||||||||||||||||||
|
||||||||||||||||||||
:param msg: The message to be displayed | ||||||||||||||||||||
:type msg: str | ||||||||||||||||||||
:param solution: The solution to the error. | ||||||||||||||||||||
:type solution: Optional[str] | ||||||||||||||||||||
:param status_code: The status code of the error. | ||||||||||||||||||||
:type status_code: Optional[int] | ||||||||||||||||||||
:param name: The name of the error. | ||||||||||||||||||||
:type name: Optional[str] | ||||||||||||||||||||
|
||||||||||||||||||||
:param solution: The solution to the problem | ||||||||||||||||||||
:type solution: str | ||||||||||||||||||||
|
||||||||||||||||||||
:param status_code: The status code to be returned | ||||||||||||||||||||
:type status_code: int | ||||||||||||||||||||
|
||||||||||||||||||||
:Example: | ||||||||||||||||||||
|
||||||||||||||||||||
|
@@ -29,7 +32,7 @@ class MyError(_BaseFlaskException): | |||||||||||||||||||
.. versionadded:: 0.1.0 | ||||||||||||||||||||
""" | ||||||||||||||||||||
|
||||||||||||||||||||
name: Optional[str] = None | ||||||||||||||||||||
msg: Optional[str] = None | ||||||||||||||||||||
name: str = "BaseError" | ||||||||||||||||||||
msg: str = "An error occurred" | ||||||||||||||||||||
solution: Optional[str] = "Try again." | ||||||||||||||||||||
status_code: Optional[int] = 400 | ||||||||||||||||||||
status_code: int = 400 | ||||||||||||||||||||
Comment on lines
+35
to
+38
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Inconsistency between attributes and documentation There's an inconsistency between the attribute definitions and the documentation:
To resolve this inconsistency, consider updating the - solution: Optional[str] = "Try again."
+ solution: str = "Try again." This change will make the attribute definition consistent with the updated documentation. Committable suggestion
Suggested change
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import pytest | ||
|
||
from flask_utils.errors import GoneError | ||
from flask_utils.errors import ConflictError | ||
from flask_utils.errors import NotFoundError | ||
from flask_utils.errors import ForbiddenError | ||
from flask_utils.errors import BadRequestError | ||
from flask_utils.errors import UnauthorizedError | ||
from flask_utils.errors import WebServerIsDownError | ||
from flask_utils.errors import FailedDependencyError | ||
from flask_utils.errors import ServiceUnavailableError | ||
from flask_utils.errors import OriginIsUnreachableError | ||
from flask_utils.errors import UnprocessableEntityError | ||
|
||
|
||
class TestGenerateErrorDict: | ||
@pytest.mark.parametrize( | ||
"error", | ||
[ | ||
ConflictError("This is the message", "This is the solution"), | ||
GoneError("This is the message", "This is the solution"), | ||
ForbiddenError("This is the message", "This is the solution"), | ||
UnauthorizedError("This is the message", "This is the solution"), | ||
NotFoundError("This is the message", "This is the solution"), | ||
BadRequestError("This is the message", "This is the solution"), | ||
UnprocessableEntityError("This is the message", "This is the solution"), | ||
FailedDependencyError("This is the message", "This is the solution"), | ||
ServiceUnavailableError("This is the message", "This is the solution"), | ||
OriginIsUnreachableError("This is the message", "This is the solution"), | ||
WebServerIsDownError("This is the message", "This is the solution"), | ||
], | ||
) | ||
def test_generate_error_dict(self, error): | ||
from flask_utils.errors._error_template import _generate_error_dict | ||
|
||
assert _generate_error_dict(error) == { | ||
"success": False, | ||
"error": { | ||
"type": error.__class__.__name__, | ||
"name": error.name, | ||
"message": error.msg, | ||
"solution": error.solution, | ||
}, | ||
"code": error.status_code, | ||
} | ||
Comment on lines
+33
to
+45
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Effective test implementation with a minor optimization opportunity. The test method effectively validates the Consider moving the import statement outside the test method to improve performance, especially when running multiple tests: from flask_utils.errors._error_template import _generate_error_dict
class TestGenerateErrorDict:
# ... (existing code) ...
def test_generate_error_dict(self, error):
assert _generate_error_dict(error) == {
# ... (existing assertion) ...
} This change would prevent repeated imports across multiple test runs, potentially improving test execution time. |
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.
Tip
Codebase Verification
Incomplete renaming of
_generate_error_json
detected._generate_error_json
:flask_utils/errors/_error_template.py
:_generate_error_json
.tests/test_generate_error_dict.py
:_generate_error_json
.docs/source/api.rst
:_generate_error_json
.Please ensure that all instances of
_generate_error_json
are renamed to_generate_error_dict
to maintain consistency and prevent potential issues.Analysis chain
LGTM! Consider adding brief descriptions.
The addition of
_generate_error_dict
and_generate_error_response
functions to the Private API documentation aligns with the PR objectives. Good job on maintaining the documentation structure and following the naming convention for private functions.Consider adding a brief description for each function to improve the documentation. For example:
The AI summary mentioned that
_generate_error_json
was renamed to_generate_error_dict
. Let's verify this change:This will help ensure that the renaming was done consistently across the codebase.
Scripts executed
The following scripts were executed for the analysis:
Script:
Length of output: 393
Script:
Length of output: 1405