This repository has been archived by the owner on Nov 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cfcc4eb
commit 2b91ca9
Showing
6 changed files
with
120 additions
and
224 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
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 |
---|---|---|
@@ -1,105 +1,73 @@ | ||
class ConnectionError(Exception): | ||
def __init__(self, response, content=None, message=None): | ||
""" | ||
Wraps errors related with | ||
requests to backend | ||
""" | ||
|
||
def __init__(self, response=None, message=None): | ||
""" | ||
Initializes the exception with the response | ||
received from the backend | ||
Params: | ||
`response`: The response received from backend | ||
`message`: A custom message | ||
Return: | ||
`response`: Returns the response received from the server | ||
""" | ||
self.response = response | ||
self.content = content | ||
self.message = message | ||
|
||
def __str__(self): | ||
message = 'Request failed.' | ||
""" | ||
Formats the error for the user so that it's | ||
easier to understand | ||
""" | ||
message = 'Request failed' | ||
if hasattr(self.response, 'status_code'): | ||
message = '{}{}'.format(message, ' Status code: %s.' % self.response.status_code) | ||
message = '{}{}'.format(message, '\nStatus code: %s' % self.response.status_code) | ||
if hasattr(self.response, 'reason'): | ||
message = '{}{}'.format(message, ' Response message: %s.' % self.response.reason) | ||
if self.content: | ||
message = '{}{}'.format(message, ' Error message: %s' % str(self.content)) | ||
message = '{}{}'.format(message, '\nResponse message: %s' % self.response.reason) | ||
if hasattr(self.response, 'content'): | ||
message = '{}{}'.format(message, '\n%s' % self.response.content) | ||
if self.message: | ||
message = '{}{}'.format(message, '\n%s' % self.message) | ||
return message | ||
|
||
|
||
class Redirection(ConnectionError): | ||
class MissingParam(Exception): | ||
""" | ||
3xx | ||
Wraps errors caught when setting up | ||
the url with its parameters | ||
""" | ||
def __str__(self): | ||
message = super(Redirection, self).__str__() | ||
if self.response.get('Location'): | ||
message = "{} => {}" .format(message, self.response.get('Location')) | ||
return message | ||
|
||
|
||
class MissingParam(TypeError): | ||
pass | ||
|
||
def __init__(self, param): | ||
""" | ||
Initializes the exception with the | ||
missing parameter name | ||
class MissingConfig(Exception): | ||
pass | ||
`param`: The missing parameter | ||
""" | ||
self.param = param | ||
|
||
|
||
class ClientError(ConnectionError): | ||
""" | ||
4xx Client Error | ||
""" | ||
pass | ||
|
||
|
||
class BadRequest(ClientError): | ||
""" | ||
400 Bad Request | ||
""" | ||
pass | ||
|
||
|
||
class UnauthorizedAccess(ClientError): | ||
""" | ||
401 Unauthorized | ||
""" | ||
pass | ||
|
||
|
||
class ForbiddenAccess(ClientError): | ||
""" | ||
403 Forbidden | ||
""" | ||
|
||
|
||
class ResourceNotFound(ClientError): | ||
""" | ||
404 Not Found | ||
""" | ||
pass | ||
|
||
|
||
class ResourceConflict(ClientError): | ||
""" | ||
409 Conflict | ||
""" | ||
pass | ||
|
||
|
||
class ResourceGone(ClientError): | ||
""" | ||
410 Gone | ||
""" | ||
pass | ||
|
||
|
||
class ResourceInvalid(ClientError): | ||
""" | ||
422 Invalid | ||
""" | ||
pass | ||
def __str__(self): | ||
return 'Missing %s parameter' % self.param | ||
|
||
|
||
class ServerError(ConnectionError): | ||
class MissingEnvVar(Exception): | ||
""" | ||
5xx Server Error | ||
Wraps errors for missing | ||
environment variables | ||
""" | ||
pass | ||
def __init__(self, env): | ||
""" | ||
Initializes the exception with the | ||
missing variable name | ||
`env`: The missing environment variable | ||
""" | ||
self.env = env | ||
|
||
class MethodNotAllowed(ClientError): | ||
""" | ||
405 Method Not Allowed | ||
""" | ||
|
||
def allowed_methods(self): | ||
return self.response['Allow'] | ||
def __str__(self): | ||
return 'Missing %s environment variable' % self.env |
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
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
Oops, something went wrong.