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

Commit b92d87b

Browse files
committed
Added MethodNotAllowed
1 parent c13ccc1 commit b92d87b

File tree

5 files changed

+93
-1
lines changed

5 files changed

+93
-1
lines changed

flask_utils/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Increment versions here according to SemVer
2-
__version__ = "0.8.0"
2+
__version__ = "0.9.0"
33

44
from flask_utils.utils import is_it_true
55
from flask_utils.errors import GoneError
@@ -10,6 +10,7 @@
1010
from flask_utils.errors import UnauthorizedError
1111
from flask_utils.errors import WebServerIsDownError
1212
from flask_utils.errors import FailedDependencyError
13+
from flask_utils.errors import MethodNotAllowedError
1314
from flask_utils.errors import ServiceUnavailableError
1415
from flask_utils.errors import OriginIsUnreachableError
1516
from flask_utils.errors import UnprocessableEntityError
@@ -28,6 +29,7 @@
2829
"GoneError",
2930
"UnprocessableEntityError",
3031
"ServiceUnavailableError",
32+
"MethodNotAllowedError",
3133
"validate_params",
3234
"is_it_true",
3335
"FlaskUtils",

flask_utils/errors/__init__.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from flask_utils.errors.unauthorized import UnauthorizedError
1010
from flask_utils.errors._error_template import _generate_error_response
1111
from flask_utils.errors.failed_dependency import FailedDependencyError
12+
from flask_utils.errors.method_not_allowed import MethodNotAllowedError
1213
from flask_utils.errors.web_server_is_down import WebServerIsDownError
1314
from flask_utils.errors.service_unavailable import ServiceUnavailableError
1415
from flask_utils.errors.unprocessableentity import UnprocessableEntityError
@@ -212,6 +213,20 @@ def generate_service_unavailable(error: ServiceUnavailableError) -> Response:
212213

213214
return _generate_error_response(error)
214215

216+
@application.errorhandler(MethodNotAllowedError)
217+
def generate_method_not_allowed(error: MethodNotAllowedError) -> Response:
218+
"""
219+
This is the 405 response creator. It will create a 405 response with
220+
a custom message and the 405 code.
221+
222+
:param error: The error body
223+
:type error: MethodNotAllowedError
224+
225+
:return: Returns the response formatted
226+
:rtype: flask.Response
227+
"""
228+
return _generate_error_response(error)
229+
215230

216231
__all__ = [
217232
"BadRequestError",
@@ -226,5 +241,6 @@ def generate_service_unavailable(error: ServiceUnavailableError) -> Response:
226241
"GoneError",
227242
"UnprocessableEntityError",
228243
"ServiceUnavailableError",
244+
"MethodNotAllowedError",
229245
"_register_error_handlers",
230246
]
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
from typing import Optional
2+
3+
from flask_utils.errors.base_class import _BaseFlaskException
4+
5+
6+
class MethodNotAllowedError(_BaseFlaskException):
7+
"""This is the MethodNotAllowedError exception class.
8+
9+
When raised, it will return 405 status code with the message and solution provided.
10+
11+
:param msg: The message to be displayed in the error.
12+
:type msg: str
13+
:param solution: The solution to the error.
14+
:type solution: Optional[str]
15+
16+
:Example:
17+
18+
.. code-block:: python
19+
20+
from flask_utils.errors import MethodNotAllowedError
21+
22+
# Inside a Flask route
23+
@app.route('/example', methods=['POST'])
24+
def example_route():
25+
...
26+
if some_condition:
27+
raise MethodNotAllowedError("This is a not allowed error.")
28+
29+
The above code would return the following JSON response from Flask:
30+
31+
.. code-block:: json
32+
33+
{
34+
"success": false,
35+
"error": {
36+
"type": "Method Not Allowed",
37+
"name": "Method Not Allowed",
38+
"message": "This is a Method Not Allowed error.",
39+
"solution": "Try again."
40+
},
41+
"code": 404
42+
}
43+
44+
.. versionadded:: 0.1.0
45+
"""
46+
47+
def __init__(self, msg: str, solution: Optional[str] = "Try again.") -> None:
48+
self.name = "Method Not Allowed"
49+
self.msg = msg
50+
self.solution = solution
51+
self.status_code = 405

tests/test_errors.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import pytest
22

3+
from flask_utils import MethodNotAllowedError
34
from flask_utils.errors.gone import GoneError
45
from flask_utils.errors.conflict import ConflictError
56
from flask_utils.errors.notfound import NotFoundError
@@ -59,6 +60,10 @@ def unprocessable_entity():
5960
def service_unavailable():
6061
raise ServiceUnavailableError("Service unavailable error")
6162

63+
@flask_client.get("/method_not_allowed")
64+
def method_not_allowed():
65+
raise MethodNotAllowedError("Method not allowed error")
66+
6267

6368
def test_bad_request_error_handler(client):
6469
response = client.get("/bad_request")
@@ -179,3 +184,19 @@ def test_service_unavailable_error_handler(client):
179184
assert response_json["error"]["solution"] == "Try again later."
180185
assert response_json["error"]["name"] == "Service Unavailable"
181186
assert response_json["error"]["type"] == "ServiceUnavailableError"
187+
188+
189+
def test_method_not_allowed_error_handler(client):
190+
response = client.get("/method_not_allowed")
191+
assert response.status_code == 405
192+
193+
response_json = response.get_json()
194+
assert response_json["error"]["message"] == "Method not allowed error"
195+
assert response_json["error"]["solution"] == "Try again."
196+
assert response_json["error"]["name"] == "Method Not Allowed"
197+
assert response_json["error"]["type"] == "MethodNotAllowedError"
198+
199+
200+
def test_method_not_allowed_with_post_method(client):
201+
response = client.post("/method_not_allowed")
202+
assert response.status_code == 405

tests/test_generate_error_dict.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import pytest
22

3+
from flask_utils import MethodNotAllowedError
34
from flask_utils.errors import GoneError
45
from flask_utils.errors import ConflictError
56
from flask_utils.errors import NotFoundError
@@ -28,6 +29,7 @@ class TestGenerateErrorDict:
2829
ServiceUnavailableError("This is the message", "This is the solution"),
2930
OriginIsUnreachableError("This is the message", "This is the solution"),
3031
WebServerIsDownError("This is the message", "This is the solution"),
32+
MethodNotAllowedError("This is the message", "This is the solution"),
3133
],
3234
)
3335
def test_generate_error_dict(self, error):

0 commit comments

Comments
 (0)