diff options
| author | jc0b <j@jc0b.computer> | 2020-06-26 12:30:16 +0200 |
|---|---|---|
| committer | jc0b <j@jc0b.computer> | 2020-06-26 12:30:16 +0200 |
| commit | f1017676a150de60b13ff2b33ca83079d87aebfc (patch) | |
| tree | 15c84fd426f65d54c70d405b9a2fc82b579c2812 /opendc/util | |
| parent | 6f51282cd7c3945ddd0fac68407a7a7be57aa2ba (diff) | |
| parent | 19bede4fc7f7320bb4eb16c3fe1a211b19ab4714 (diff) | |
Merged refactoring changes with upstream
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..2563c419 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 ClientError(Exception): + """Raised when a 4xx response is to be returned.""" + + def __init__(self, response): + super(ClientError, self).__init__(str(response)) + self.response = response diff --git a/opendc/util/rest.py b/opendc/util/rest.py index 33371e52..dc5478de 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 ClientError 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 ClientError(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 ClientError as e: + e.response.id = self.id + return e.response + response.id = self.id return response |
