Skip to content
This repository has been archived by the owner on Nov 19, 2024. It is now read-only.

Commit

Permalink
Added MethodNotAllowed
Browse files Browse the repository at this point in the history
  • Loading branch information
Seluj78 committed Sep 25, 2024
1 parent c13ccc1 commit b92d87b
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 1 deletion.
4 changes: 3 additions & 1 deletion flask_utils/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Increment versions here according to SemVer
__version__ = "0.8.0"
__version__ = "0.9.0"

from flask_utils.utils import is_it_true
from flask_utils.errors import GoneError
Expand All @@ -10,6 +10,7 @@
from flask_utils.errors import UnauthorizedError
from flask_utils.errors import WebServerIsDownError
from flask_utils.errors import FailedDependencyError
from flask_utils.errors import MethodNotAllowedError
from flask_utils.errors import ServiceUnavailableError
from flask_utils.errors import OriginIsUnreachableError
from flask_utils.errors import UnprocessableEntityError
Expand All @@ -28,6 +29,7 @@
"GoneError",
"UnprocessableEntityError",
"ServiceUnavailableError",
"MethodNotAllowedError",
"validate_params",
"is_it_true",
"FlaskUtils",
Expand Down
16 changes: 16 additions & 0 deletions flask_utils/errors/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from flask_utils.errors.unauthorized import UnauthorizedError
from flask_utils.errors._error_template import _generate_error_response
from flask_utils.errors.failed_dependency import FailedDependencyError
from flask_utils.errors.method_not_allowed import MethodNotAllowedError
from flask_utils.errors.web_server_is_down import WebServerIsDownError
from flask_utils.errors.service_unavailable import ServiceUnavailableError
from flask_utils.errors.unprocessableentity import UnprocessableEntityError
Expand Down Expand Up @@ -212,6 +213,20 @@ def generate_service_unavailable(error: ServiceUnavailableError) -> Response:

return _generate_error_response(error)

@application.errorhandler(MethodNotAllowedError)
def generate_method_not_allowed(error: MethodNotAllowedError) -> Response:
"""
This is the 405 response creator. It will create a 405 response with
a custom message and the 405 code.
:param error: The error body
:type error: MethodNotAllowedError
:return: Returns the response formatted
:rtype: flask.Response
"""
return _generate_error_response(error)


__all__ = [
"BadRequestError",
Expand All @@ -226,5 +241,6 @@ def generate_service_unavailable(error: ServiceUnavailableError) -> Response:
"GoneError",
"UnprocessableEntityError",
"ServiceUnavailableError",
"MethodNotAllowedError",
"_register_error_handlers",
]
51 changes: 51 additions & 0 deletions flask_utils/errors/method_not_allowed.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
from typing import Optional

from flask_utils.errors.base_class import _BaseFlaskException


class MethodNotAllowedError(_BaseFlaskException):
"""This is the MethodNotAllowedError exception class.
When raised, it will return 405 status code with the message and solution provided.
:param msg: The message to be displayed in the error.
:type msg: str
:param solution: The solution to the error.
:type solution: Optional[str]
:Example:
.. code-block:: python
from flask_utils.errors import MethodNotAllowedError
# Inside a Flask route
@app.route('/example', methods=['POST'])
def example_route():
...
if some_condition:
raise MethodNotAllowedError("This is a not allowed error.")
The above code would return the following JSON response from Flask:
.. code-block:: json
{
"success": false,
"error": {
"type": "Method Not Allowed",
"name": "Method Not Allowed",
"message": "This is a Method Not Allowed error.",
"solution": "Try again."
},
"code": 404
}
.. versionadded:: 0.1.0
"""

def __init__(self, msg: str, solution: Optional[str] = "Try again.") -> None:
self.name = "Method Not Allowed"
self.msg = msg
self.solution = solution
self.status_code = 405
21 changes: 21 additions & 0 deletions tests/test_errors.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import pytest

from flask_utils import MethodNotAllowedError
from flask_utils.errors.gone import GoneError
from flask_utils.errors.conflict import ConflictError
from flask_utils.errors.notfound import NotFoundError
Expand Down Expand Up @@ -59,6 +60,10 @@ def unprocessable_entity():
def service_unavailable():
raise ServiceUnavailableError("Service unavailable error")

@flask_client.get("/method_not_allowed")
def method_not_allowed():
raise MethodNotAllowedError("Method not allowed error")


def test_bad_request_error_handler(client):
response = client.get("/bad_request")
Expand Down Expand Up @@ -179,3 +184,19 @@ def test_service_unavailable_error_handler(client):
assert response_json["error"]["solution"] == "Try again later."
assert response_json["error"]["name"] == "Service Unavailable"
assert response_json["error"]["type"] == "ServiceUnavailableError"


def test_method_not_allowed_error_handler(client):
response = client.get("/method_not_allowed")
assert response.status_code == 405

response_json = response.get_json()
assert response_json["error"]["message"] == "Method not allowed error"
assert response_json["error"]["solution"] == "Try again."
assert response_json["error"]["name"] == "Method Not Allowed"
assert response_json["error"]["type"] == "MethodNotAllowedError"


def test_method_not_allowed_with_post_method(client):
response = client.post("/method_not_allowed")
assert response.status_code == 405
2 changes: 2 additions & 0 deletions tests/test_generate_error_dict.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import pytest

from flask_utils import MethodNotAllowedError
from flask_utils.errors import GoneError
from flask_utils.errors import ConflictError
from flask_utils.errors import NotFoundError
Expand Down Expand Up @@ -28,6 +29,7 @@ class TestGenerateErrorDict:
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"),
MethodNotAllowedError("This is the message", "This is the solution"),
],
)
def test_generate_error_dict(self, error):
Expand Down

0 comments on commit b92d87b

Please sign in to comment.