Skip to content
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

SAL Server returns custom exceptions #47

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 32 additions & 8 deletions sal/core/exception.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from werkzeug.exceptions import HTTPException

# base exception
class SALException(Exception):

Expand All @@ -9,34 +11,56 @@ def __init__(self, message=None, *args, **kwargs):
message = message if message else self.message
super().__init__(message, *args, **kwargs)

class SALHTTPException(HTTPException, SALException):

"""
Base SAL specific HTTP exception

Inherits from ``HTTPException`` first as we want it to use that `__init__`

Inheriting from both means it will be caught if either ``HTTPException`` or
``SALException``

Inheriting from ``HTTPException`` means it will have an associated status
code (`code`) which can be handled by Flask
"""

pass

# server side exceptions
class InvalidPath(SALException):
message = 'Path does not conform to path specification.'
class InvalidPath(SALHTTPException):
code = 404
description = 'Path does not conform to path specification.'


class NodeNotFound(SALException):
message = 'The supplied path does not point to a valid node.'
code = 404
description = 'The supplied path does not point to a valid node.'


class UnsupportedOperation(SALException):
message = 'Operation is not supported.'
code = 500
description = 'Operation is not supported.'


class InvalidRequest(SALException):
message = 'The request sent to the server could not be handled.'
code = 400
description = 'The request sent to the server could not be handled.'


class AuthenticationFailed(SALException):
message = 'Valid authorisation credentials were not supplied.'
code = 401
description = 'Valid authorisation credentials were not supplied.'


class PermissionDenied(SALException):
message = 'The user does not have permission to perform this operation.'
code = 403
description = 'The user does not have permission to perform this operation.'


class InternalError(SALException):
message = 'An error occurred affecting server operation. Please contact your administrator.'
code = 500
description = 'An error occurred affecting server operation. Please contact your administrator.'


# client side exceptions
Expand Down
20 changes: 20 additions & 0 deletions sal/server/api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from flask.json import jsonify
from flask_restful import Api

class ErrorHandlerApi(Api):

"""
Subclasses flask_restul.Api to add error handling
"""

def handle_error(self, e):
"""
Converts the error (including any custom messages/description) to JSON

:param e: The exception to handle
:type e: werkzeug.exceptions.HTTPException
:return: Tuple of JSON equivalent of `e` and its status code
"""

return jsonify({"message":str(e),
"exception":e.__class__.__name__}), e.code
43 changes: 2 additions & 41 deletions sal/server/main.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from flask import Flask
from flask_restful import Api

from sal.core.version import VERSION as RELEASE_VERSION
from sal.core import exception
from sal.server.api import ErrorHandlerApi
from sal.server.interface import PersistenceProvider, AuthenticationProvider, AuthorisationProvider
from sal.server.resource import ServerInfo, DataTree, Authenticator
from sal.dataclass import *
Expand Down Expand Up @@ -46,47 +46,8 @@ def __init__(self, persistence_provider, authentication_provider=None, authorisa
'API_VERSION': API_VERSION
}

# exception handling mapping table
error_map = {
'InvalidRequest': {
'message': exception.InvalidRequest.message,
'status': 400,
'exception': 'InvalidRequest'
},
'AuthenticationFailed': {
'message': exception.AuthenticationFailed.message,
'status': 401,
'exception': 'AuthenticationFailed'
},
'PermissionDenied': {
'message': exception.PermissionDenied.message,
'status': 403,
'exception': 'PermissionDenied'
},
'InvalidPath': {
'message': exception.InvalidPath.message,
'status': 404,
'exception': 'InvalidPath'
},
'NodeNotFound': {
'message': exception.NodeNotFound.message,
'status': 404,
'exception': 'NodeNotFound'
},
'UnsupportedOperation': {
'message': exception.UnsupportedOperation.message,
'status': 500,
'exception': 'UnsupportedOperation'
},
'InternalError': {
'message': exception.InternalError.message,
'status': 500,
'exception': 'InternalError'
},
}

# build api
api = Api(self, errors=error_map)
api = ErrorHandlerApi(self)
api.add_resource(ServerInfo, '/')
api.add_resource(DataTree, '/data', '/data/', '/data/<path:path>')
api.add_resource(Authenticator, '/auth', '/auth/')
Expand Down