Merge pull request #1364 from ecordell/error-json-fixes
Fix error-related issues
This commit is contained in:
commit
09064853ac
4 changed files with 42 additions and 27 deletions
|
@ -39,9 +39,9 @@ api.decorators = [csrf_protect,
|
||||||
@crossdomain(origin='*', headers=['Authorization', 'Content-Type'])
|
@crossdomain(origin='*', headers=['Authorization', 'Content-Type'])
|
||||||
def handle_api_error(error):
|
def handle_api_error(error):
|
||||||
response = Response(json.dumps(error.to_dict()), error.status_code, mimetype='application/problem+json')
|
response = Response(json.dumps(error.to_dict()), error.status_code, mimetype='application/problem+json')
|
||||||
if error.status_code is 401:
|
if error.status_code == 401:
|
||||||
response.headers['WWW-Authenticate'] = ('Bearer error="%s" error_description="%s"' %
|
response.headers['WWW-Authenticate'] = ('Bearer error="%s" error_description="%s"' %
|
||||||
(error.error_type, error.error_description))
|
(error.error_type.value, error.error_description))
|
||||||
return response
|
return response
|
||||||
|
|
||||||
def resource(*urls, **kwargs):
|
def resource(*urls, **kwargs):
|
||||||
|
|
|
@ -180,30 +180,38 @@ def swagger_route_data(include_internal=False, compact=False):
|
||||||
models[response_schema_name] = view_class.schemas[response_schema_name]
|
models[response_schema_name] = view_class.schemas[response_schema_name]
|
||||||
|
|
||||||
models['ApiError'] = {
|
models['ApiError'] = {
|
||||||
'type': 'object',
|
'type': 'object',
|
||||||
'properties': {
|
'properties': {
|
||||||
'status': {
|
'status': {
|
||||||
'type': 'integer',
|
'type': 'integer',
|
||||||
'description': 'Status code of the response.'
|
'description': 'Status code of the response.'
|
||||||
},
|
|
||||||
'type': {
|
|
||||||
'type': 'string',
|
|
||||||
'description': 'Reference to the type of the error.'
|
|
||||||
},
|
|
||||||
'detail': {
|
|
||||||
'type': 'string',
|
|
||||||
'description': 'Details about the specific instance of the error.'
|
|
||||||
},
|
|
||||||
'title': {
|
|
||||||
'type': 'string',
|
|
||||||
'description': 'Unique error code to identify the type of error.'
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
'required': [
|
'type': {
|
||||||
'status',
|
'type': 'string',
|
||||||
'type',
|
'description': 'Reference to the type of the error.'
|
||||||
'title',
|
},
|
||||||
]
|
'detail': {
|
||||||
|
'type': 'string',
|
||||||
|
'description': 'Details about the specific instance of the error.'
|
||||||
|
},
|
||||||
|
'title': {
|
||||||
|
'type': 'string',
|
||||||
|
'description': 'Unique error code to identify the type of error.'
|
||||||
|
},
|
||||||
|
'error_message': {
|
||||||
|
'type': 'string',
|
||||||
|
'description': 'Deprecated; alias for detail'
|
||||||
|
},
|
||||||
|
'error_type': {
|
||||||
|
'type': 'string',
|
||||||
|
'description': 'Deprecated; alias for detail'
|
||||||
|
}
|
||||||
|
},
|
||||||
|
'required': [
|
||||||
|
'status',
|
||||||
|
'type',
|
||||||
|
'title',
|
||||||
|
]
|
||||||
}
|
}
|
||||||
|
|
||||||
responses = {
|
responses = {
|
||||||
|
@ -224,8 +232,8 @@ def swagger_route_data(include_internal=False, compact=False):
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for status, body in responses.items():
|
for _, body in responses.items():
|
||||||
body['schema'] = {'$ref': '#/definitions/ApiError'}
|
body['schema'] = {'$ref': '#/definitions/ApiError'}
|
||||||
|
|
||||||
if method_name == 'DELETE':
|
if method_name == 'DELETE':
|
||||||
responses['204'] = {
|
responses['204'] = {
|
||||||
|
|
|
@ -67,7 +67,9 @@ class ApiException(Exception):
|
||||||
|
|
||||||
if self.error_description is not None:
|
if self.error_description is not None:
|
||||||
rv['detail'] = self.error_description
|
rv['detail'] = self.error_description
|
||||||
|
rv['error_message'] = self.error_description # TODO: deprecate
|
||||||
|
|
||||||
|
rv['error_type'] = self.error_type.value # TODO: deprecate
|
||||||
rv['title'] = self.error_type.value
|
rv['title'] = self.error_type.value
|
||||||
rv['type'] = url_for('error', error_type=self.error_type.value, _external=True)
|
rv['type'] = url_for('error', error_type=self.error_type.value, _external=True)
|
||||||
rv['status'] = self.status_code
|
rv['status'] = self.status_code
|
||||||
|
|
|
@ -103,6 +103,11 @@ class TestAuth(ApiTestCase):
|
||||||
self.conduct_basic_auth('$oauthtoken', 'foobar')
|
self.conduct_basic_auth('$oauthtoken', 'foobar')
|
||||||
self.verify_no_identity()
|
self.verify_no_identity()
|
||||||
|
|
||||||
|
def test_oauth_invalid_http_response(self):
|
||||||
|
rv = self.app.get(api.url_for(User), headers={'Authorization': 'Bearer bad_token'})
|
||||||
|
assert 'WWW-Authenticate' in rv.headers
|
||||||
|
self.assertEquals(401, rv.status_code)
|
||||||
|
|
||||||
def test_oauth_valid_user(self):
|
def test_oauth_valid_user(self):
|
||||||
user = model.user.get_user(ADMIN_ACCESS_USER)
|
user = model.user.get_user(ADMIN_ACCESS_USER)
|
||||||
self.create_oauth(user)
|
self.create_oauth(user)
|
||||||
|
|
Reference in a new issue