diff options
| author | Georgios Andreadis <info@gandreadis.com> | 2020-06-26 12:17:26 +0200 |
|---|---|---|
| committer | Georgios Andreadis <info@gandreadis.com> | 2020-06-26 12:17:26 +0200 |
| commit | 92b94b59ad80329a2c99471edbf5bbdc9af1e525 (patch) | |
| tree | d9b71de4473ffe5d5decc8631f82429694860dcc /opendc/util | |
| parent | c15448fb6f3aacc8939f2714fc20304dde98cd28 (diff) | |
Revamp error responses
Diffstat (limited to 'opendc/util')
| -rw-r--r-- | opendc/util/exceptions.py | 8 | ||||
| -rw-r--r-- | opendc/util/rest.py | 13 |
2 files changed, 19 insertions, 2 deletions
diff --git a/opendc/util/exceptions.py b/opendc/util/exceptions.py index caf6dd8e..e73dad4f 100644 --- a/opendc/util/exceptions.py +++ b/opendc/util/exceptions.py @@ -55,3 +55,11 @@ class MissingParameterError(ParameterError): self.parameter_name = parameter_name self.parameter_location = parameter_location + + +class ClientRequestError(Exception): + """Raised when a 4xx response is to be returned.""" + + def __init__(self, response): + super(ClientRequestError, self).__init__(str(response)) + self.response = response diff --git a/opendc/util/rest.py b/opendc/util/rest.py index 33371e52..e70998a8 100644 --- a/opendc/util/rest.py +++ b/opendc/util/rest.py @@ -6,6 +6,7 @@ import sys from oauth2client import client, crypt from opendc.util import exceptions, parameter_checker +from opendc.util.exceptions import ClientRequestError class Request(object): @@ -71,14 +72,22 @@ class Request(object): def check_required_parameters(self, **kwargs): """Raise an error if a parameter is missing or of the wrong type.""" - parameter_checker.check(self, **kwargs) + try: + parameter_checker.check(self, **kwargs) + except exceptions.ParameterError as e: + raise ClientRequestError(Response(400, str(e))) def process(self): """Process the Request and return a Response.""" method = getattr(self.module, self.method) - response = method(self) + try: + response = method(self) + except ClientRequestError as e: + e.response.id = self.id + return e.response + response.id = self.id return response |
