-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #482 from alphagov/ris-api-error-handlers
add common error handlers for "api" flask apps
- Loading branch information
Showing
7 changed files
with
136 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,4 @@ | |
from .flask_init import init_app, init_manager | ||
|
||
|
||
__version__ = '45.1.1' | ||
__version__ = '45.2.0' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
from werkzeug.exceptions import Unauthorized | ||
|
||
|
||
class UnauthorizedWWWAuthenticate(Unauthorized): | ||
""" | ||
This is a near verbatim copy of an improvement to an as-yet-unreleased upstream version of werkzeug that allows | ||
us to specify a www_authenticate argument containing the contents of that field. We should get rid of this once | ||
we're able to upgrade past that version. | ||
From werkzeug 8ed5b3f9a285eca756c3ab33f8c370a88eab3842 | ||
""" | ||
def __init__(self, www_authenticate=None, description=None): | ||
super().__init__(description=description) | ||
if not isinstance(www_authenticate, (tuple, list)): | ||
www_authenticate = (www_authenticate,) | ||
self.www_authenticate = www_authenticate | ||
|
||
def get_headers(self, environ=None): | ||
headers = super().get_headers(environ) | ||
if self.www_authenticate: | ||
headers.append(( | ||
'WWW-Authenticate', | ||
', '.join([str(x) for x in self.www_authenticate]) | ||
)) | ||
return headers |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# the contents of .frontend are exposed as the top-level module for backwards compatibility | ||
from dmutils.errors.frontend import * # noqa |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import json | ||
|
||
from flask import current_app, jsonify | ||
from werkzeug.exceptions import BadRequest | ||
|
||
|
||
class ValidationError(ValueError): | ||
@property | ||
def message(self): | ||
return self.args[0] | ||
|
||
|
||
def json_error_handler(e): | ||
try: | ||
# initially we'll try and assume this is an HTTPException of some sort. for the most part, the default | ||
# HTTPExceptions render themselves in the desired way if returned as a response. the only change we want to | ||
# make is to enclose the error description in json. | ||
response = e.get_response() | ||
response.set_data(json.dumps({"error": e.description})) | ||
response.mimetype = current_app.config["JSONIFY_MIMETYPE"] | ||
|
||
return response | ||
except Exception: | ||
# either `e` wasn't an HTTPException or something went wrong in trying to jsonify it | ||
return jsonify(error="Internal error"), 500 | ||
|
||
|
||
def validation_error_handler(validation_error): | ||
return json_error_handler(BadRequest(description=validation_error.message)) |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters