"""Custom exceptions.""" class OwsError(Exception): """Exception to create OwsException from str message.""" def __init__(self, message, status=500): """Initialize exception.""" super().__init__() self.message = message self.status = status @classmethod def not_found(cls, message=None): """Return not found error.""" return OwsError(message, 404) @classmethod def forbidden(cls, message='Access Forbidden'): """Return not found error.""" return OwsError(message, 403) @classmethod def bad_request(cls, message=None): """Return bad request error.""" return OwsError(message, 400) @classmethod def internal_server_error(cls, message=None): """Return bad request error.""" return OwsError(message, 500) @classmethod def response_error(cls, response): """Return a ows response error.""" return OwsError(response.text, response.status_code) @classmethod def from_boto3_client_error(cls, client_error, app_error): """Turn a boto3 ClientError into an OwsError.""" error_response = client_error.response error_code = error_response['Error']['Code'] error_message = error_response['Error']['Message'] error_status = error_response['ResponseMetadata']['HTTPStatusCode'] raise OwsError('{code}: {app_err}, {boto_err}'.format( code=error_code, app_err=app_error, boto_err=error_message), error_status) def __str__(self): """Turn this exception into a string.""" if not self.message: return str(self.status) return 'Status %d: %s' % (self.status, self.message)