Techdebt - Simplify some exceptions (#5567)

This commit is contained in:
Bert Blommers 2022-10-16 12:06:23 +00:00 committed by GitHub
parent ea8eb0fb78
commit 8e3f2331d4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 95 additions and 202 deletions

View File

@ -1,10 +1,10 @@
import json import json
from werkzeug.exceptions import BadRequest from moto.core.exceptions import JsonRESTError
class AthenaClientError(BadRequest): class AthenaClientError(JsonRESTError):
def __init__(self, code, message): def __init__(self, code, message):
super().__init__() super().__init__(error_type="InvalidRequestException", message=message)
self.description = json.dumps( self.description = json.dumps(
{ {
"Error": { "Error": {

View File

@ -1,4 +1,4 @@
from werkzeug.exceptions import BadRequest from moto.core.exceptions import RESTError
from jinja2 import Template from jinja2 import Template
@ -9,36 +9,32 @@ class UnformattedGetAttTemplateException(Exception):
status_code = 400 status_code = 400
class ValidationError(BadRequest): class ValidationError(RESTError):
def __init__(self, name_or_id=None, message=None): def __init__(self, name_or_id=None, message=None):
if message is None: if message is None:
message = "Stack with id {0} does not exist".format(name_or_id) message = "Stack with id {0} does not exist".format(name_or_id)
template = Template(ERROR_RESPONSE) template = Template(ERROR_RESPONSE)
super().__init__() super().__init__(error_type="ValidationError", message=message)
self.description = template.render(code="ValidationError", message=message) self.description = template.render(code="ValidationError", message=message)
class MissingParameterError(BadRequest): class MissingParameterError(RESTError):
def __init__(self, parameter_name): def __init__(self, parameter_name):
template = Template(ERROR_RESPONSE) template = Template(ERROR_RESPONSE)
super().__init__() message = "Missing parameter {0}".format(parameter_name)
self.description = template.render( super().__init__(error_type="ValidationError", message=message)
code="Missing Parameter", self.description = template.render(code="Missing Parameter", message=message)
message="Missing parameter {0}".format(parameter_name),
)
class ExportNotFound(BadRequest): class ExportNotFound(RESTError):
"""Exception to raise if a template tries to import a non-existent export""" """Exception to raise if a template tries to import a non-existent export"""
def __init__(self, export_name): def __init__(self, export_name):
template = Template(ERROR_RESPONSE) template = Template(ERROR_RESPONSE)
super().__init__() message = "No export named {0} found.".format(export_name)
self.description = template.render( super().__init__(error_type="ExportNotFound", message=message)
code="ExportNotFound", self.description = template.render(code="ExportNotFound", message=message)
message="No export named {0} found.".format(export_name),
)
class UnsupportedAttribute(ValidationError): class UnsupportedAttribute(ValidationError):

View File

@ -1,25 +1,15 @@
import json from moto.core.exceptions import JsonRESTError
from werkzeug.exceptions import BadRequest
class ResourceNotFoundError(BadRequest): class ResourceNotFoundError(JsonRESTError):
def __init__(self, message): def __init__(self, message):
super().__init__() super().__init__(error_type="ResourceNotFoundException", message=message)
self.description = json.dumps(
{"message": message, "__type": "ResourceNotFoundException"}
)
class InvalidNameException(BadRequest): class InvalidNameException(JsonRESTError):
message = "1 validation error detected: Value '{}' at 'identityPoolName' failed to satisfy constraint: Member must satisfy regular expression pattern: [\\w\\s+=,.@-]+" message = "1 validation error detected: Value '{}' at 'identityPoolName' failed to satisfy constraint: Member must satisfy regular expression pattern: [\\w\\s+=,.@-]+"
def __init__(self, name): def __init__(self, name):
super().__init__() msg = InvalidNameException.message.format(name)
self.description = json.dumps( super().__init__(error_type="ValidationException", message=msg)
{
"message": InvalidNameException.message.format(name),
"__type": "ValidationException",
}
)

View File

@ -1,62 +1,39 @@
import json
from werkzeug.exceptions import BadRequest
from moto.core.exceptions import JsonRESTError from moto.core.exceptions import JsonRESTError
class ResourceNotFoundError(BadRequest): class ResourceNotFoundError(JsonRESTError):
def __init__(self, message): def __init__(self, message):
super().__init__() super().__init__(error_type="ResourceNotFoundException", message=message)
self.description = json.dumps(
{"message": message, "__type": "ResourceNotFoundException"}
)
class UserNotFoundError(BadRequest): class UserNotFoundError(JsonRESTError):
def __init__(self, message): def __init__(self, message):
super().__init__() super().__init__(error_type="UserNotFoundException", message=message)
self.description = json.dumps(
{"message": message, "__type": "UserNotFoundException"}
)
class UsernameExistsException(BadRequest): class UsernameExistsException(JsonRESTError):
def __init__(self, message): def __init__(self, message):
super().__init__() super().__init__(error_type="UsernameExistsException", message=message)
self.description = json.dumps(
{"message": message, "__type": "UsernameExistsException"}
)
class GroupExistsException(BadRequest): class GroupExistsException(JsonRESTError):
def __init__(self, message): def __init__(self, message):
super().__init__() super().__init__(error_type="GroupExistsException", message=message)
self.description = json.dumps(
{"message": message, "__type": "GroupExistsException"}
)
class NotAuthorizedError(BadRequest): class NotAuthorizedError(JsonRESTError):
def __init__(self, message): def __init__(self, message):
super().__init__() super().__init__(error_type="NotAuthorizedException", message=message)
self.description = json.dumps(
{"message": message, "__type": "NotAuthorizedException"}
)
class UserNotConfirmedException(BadRequest): class UserNotConfirmedException(JsonRESTError):
def __init__(self, message): def __init__(self, message):
super().__init__() super().__init__(error_type="UserNotConfirmedException", message=message)
self.description = json.dumps(
{"message": message, "__type": "UserNotConfirmedException"}
)
class ExpiredCodeException(BadRequest): class ExpiredCodeException(JsonRESTError):
def __init__(self, message): def __init__(self, message):
super().__init__() super().__init__(error_type="ExpiredCodeException", message=message)
self.description = json.dumps(
{"message": message, "__type": "ExpiredCodeException"}
)
class InvalidParameterException(JsonRESTError): class InvalidParameterException(JsonRESTError):

View File

@ -1,21 +1,14 @@
import json from moto.core.exceptions import JsonRESTError
from werkzeug.exceptions import BadRequest
class ResourceNotFoundError(BadRequest): class ResourceNotFoundError(JsonRESTError):
def __init__(self, message): def __init__(self, message):
super().__init__() super().__init__(error_type="ResourceNotFoundException", message=message)
self.description = json.dumps(
{"message": message, "__type": "ResourceNotFoundException"}
)
class ResourceInUseError(BadRequest): class ResourceInUseError(JsonRESTError):
def __init__(self, message): def __init__(self, message):
super().__init__() super().__init__(error_type="ResourceInUseException", message=message)
self.description = json.dumps(
{"message": message, "__type": "ResourceInUseException"}
)
class StreamNotFoundError(ResourceNotFoundError): class StreamNotFoundError(ResourceNotFoundError):
@ -23,13 +16,10 @@ class StreamNotFoundError(ResourceNotFoundError):
super().__init__(f"Stream {stream_name} under account {account_id} not found.") super().__init__(f"Stream {stream_name} under account {account_id} not found.")
class StreamCannotBeUpdatedError(BadRequest): class StreamCannotBeUpdatedError(JsonRESTError):
def __init__(self, stream_name, account_id): def __init__(self, stream_name, account_id):
super().__init__()
message = f"Request is invalid. Stream {stream_name} under account {account_id} is in On-Demand mode." message = f"Request is invalid. Stream {stream_name} under account {account_id} is in On-Demand mode."
self.description = json.dumps( super().__init__(error_type="ValidationException", message=message)
{"message": message, "__type": "ValidationException"}
)
class ShardNotFoundError(ResourceNotFoundError): class ShardNotFoundError(ResourceNotFoundError):
@ -44,12 +34,9 @@ class ConsumerNotFound(ResourceNotFoundError):
super().__init__(f"Consumer {consumer}, account {account_id} not found.") super().__init__(f"Consumer {consumer}, account {account_id} not found.")
class InvalidArgumentError(BadRequest): class InvalidArgumentError(JsonRESTError):
def __init__(self, message): def __init__(self, message):
super().__init__() super().__init__(error_type="InvalidArgumentException", message=message)
self.description = json.dumps(
{"message": message, "__type": "InvalidArgumentException"}
)
class InvalidRetentionPeriod(InvalidArgumentError): class InvalidRetentionPeriod(InvalidArgumentError):
@ -73,45 +60,27 @@ class InvalidIncreaseRetention(InvalidArgumentError):
super().__init__(msg) super().__init__(msg)
class ValidationException(BadRequest): class ValidationException(JsonRESTError):
def __init__(self, value, position, regex_to_match): def __init__(self, value, position, regex_to_match):
super().__init__() msg = f"1 validation error detected: Value '{value}' at '{position}' failed to satisfy constraint: Member must satisfy regular expression pattern: {regex_to_match}"
self.description = json.dumps( super().__init__(error_type="ValidationException", message=msg)
{
"message": f"1 validation error detected: Value '{value}' at '{position}' failed to satisfy constraint: Member must satisfy regular expression pattern: {regex_to_match}",
"__type": "ValidationException",
}
)
class RecordSizeExceedsLimit(BadRequest): class RecordSizeExceedsLimit(JsonRESTError):
def __init__(self, position): def __init__(self, position):
super().__init__() msg = f"1 validation error detected: Value at 'records.{position}.member.data' failed to satisfy constraint: Member must have length less than or equal to 1048576"
self.description = json.dumps( super().__init__(error_type="ValidationException", message=msg)
{
"message": f"1 validation error detected: Value at 'records.{position}.member.data' failed to satisfy constraint: Member must have length less than or equal to 1048576",
"__type": "ValidationException",
}
)
class TotalRecordsSizeExceedsLimit(BadRequest): class TotalRecordsSizeExceedsLimit(JsonRESTError):
def __init__(self): def __init__(self):
super().__init__() super().__init__(
self.description = json.dumps( error_type="InvalidArgumentException",
{ message="Records size exceeds 5 MB limit",
"message": "Records size exceeds 5 MB limit",
"__type": "InvalidArgumentException",
}
) )
class TooManyRecords(BadRequest): class TooManyRecords(JsonRESTError):
def __init__(self): def __init__(self):
super().__init__() msg = "1 validation error detected: Value at 'records' failed to satisfy constraint: Member must have length less than or equal to 500"
self.description = json.dumps( super().__init__(error_type="ValidationException", message=msg)
{
"message": "1 validation error detected: Value at 'records' failed to satisfy constraint: Member must have length less than or equal to 500",
"__type": "ValidationException",
}
)

View File

@ -1,6 +1,6 @@
import json import json
from moto.core.exceptions import JsonRESTError
from functools import wraps from functools import wraps
from werkzeug.exceptions import HTTPException
def exception_handler(f): def exception_handler(f):
@ -14,10 +14,9 @@ def exception_handler(f):
return _wrapper return _wrapper
class ManagedBlockchainClientError(HTTPException): class ManagedBlockchainClientError(JsonRESTError):
code = 400
def __init__(self, error_type, message): def __init__(self, error_type, message):
super().__init__(error_type=error_type, message=message)
self.error_type = error_type self.error_type = error_type
self.message = message self.message = message
self.description = json.dumps({"message": self.message}) self.description = json.dumps({"message": self.message})
@ -28,10 +27,6 @@ class ManagedBlockchainClientError(HTTPException):
("x-amzn-ErrorType", self.error_type), ("x-amzn-ErrorType", self.error_type),
] ]
@property
def response(self):
return self.get_body()
def get_body(self, *args, **kwargs): # pylint: disable=unused-argument def get_body(self, *args, **kwargs): # pylint: disable=unused-argument
return self.description return self.description

View File

@ -1,60 +1,38 @@
from __future__ import unicode_literals from moto.core.exceptions import JsonRESTError
import json
from werkzeug.exceptions import BadRequest
class DisabledApiException(BadRequest): class DisabledApiException(JsonRESTError):
def __init__(self, message): def __init__(self, message):
super().__init__() super().__init__(error_type="DisabledApiException", message=message)
self.description = json.dumps(
{"message": message, "__type": "DisabledApiException"}
class InternalServiceErrorException(JsonRESTError):
def __init__(self, message):
super().__init__(error_type="InternalServiceErrorException", message=message)
class InvalidCustomerIdentifierException(JsonRESTError):
def __init__(self, message):
super().__init__(
error_type="InvalidCustomerIdentifierException", message=message
) )
class InternalServiceErrorException(BadRequest): class InvalidProductCodeException(JsonRESTError):
def __init__(self, message): def __init__(self, message):
super().__init__() super().__init__(error_type="InvalidProductCodeException", message=message)
self.description = json.dumps(
{"message": message, "__type": "InternalServiceErrorException"}
)
class InvalidCustomerIdentifierException(BadRequest): class InvalidUsageDimensionException(JsonRESTError):
def __init__(self, message): def __init__(self, message):
super().__init__() super().__init__(error_type="InvalidUsageDimensionException", message=message)
self.description = json.dumps(
{"message": message, "__type": "InvalidCustomerIdentifierException"}
)
class InvalidProductCodeException(BadRequest): class ThrottlingException(JsonRESTError):
def __init__(self, message): def __init__(self, message):
super().__init__() super().__init__(error_type="ThrottlingException", message=message)
self.description = json.dumps(
{"message": message, "__type": "InvalidProductCodeException"}
)
class InvalidUsageDimensionException(BadRequest): class TimestampOutOfBoundsException(JsonRESTError):
def __init__(self, message): def __init__(self, message):
super().__init__() super().__init__(error_type="TimestampOutOfBoundsException", message=message)
self.description = json.dumps(
{"message": message, "__type": "InvalidUsageDimensionException"}
)
class ThrottlingException(BadRequest):
def __init__(self, message):
super().__init__()
self.description = json.dumps(
{"message": message, "__type": "ThrottlingException"}
)
class TimestampOutOfBoundsException(BadRequest):
def __init__(self, message):
super().__init__()
self.description = json.dumps(
{"message": message, "__type": "TimestampOutOfBoundsException"}
)

View File

@ -1,18 +1,11 @@
import json from moto.core.exceptions import JsonRESTError
from werkzeug.exceptions import BadRequest
class ResourceNotFoundException(BadRequest): class ResourceNotFoundException(JsonRESTError):
def __init__(self, message): def __init__(self, message):
super().__init__() super().__init__(error_type="ResourceNotFoundException", message=message)
self.description = json.dumps(
{"message": message, "__type": "ResourceNotFoundException"}
)
class ValidationException(BadRequest): class ValidationException(JsonRESTError):
def __init__(self, message): def __init__(self, message):
super().__init__() super().__init__(error_type="ResourceNotFoundException", message=message)
self.description = json.dumps(
{"message": message, "__type": "ResourceNotFoundException"}
)

View File

@ -1,10 +1,10 @@
from jinja2 import Template from jinja2 import Template
from werkzeug.exceptions import BadRequest from moto.core.exceptions import RESTError
class RDSClientError(BadRequest): class RDSClientError(RESTError):
def __init__(self, code, message): def __init__(self, code, message):
super().__init__() super().__init__(error_type=code, message=message)
template = Template( template = Template(
""" """
<ErrorResponse> <ErrorResponse>

View File

@ -1,10 +1,10 @@
import json import json
from werkzeug.exceptions import BadRequest from moto.core.exceptions import JsonRESTError
class RedshiftClientError(BadRequest): class RedshiftClientError(JsonRESTError):
def __init__(self, code, message): def __init__(self, code, message):
super().__init__() super().__init__(error_type=code, message=message)
self.description = json.dumps( self.description = json.dumps(
{ {
"Error": {"Code": code, "Message": message, "Type": "Sender"}, "Error": {"Code": code, "Message": message, "Type": "Sender"},

View File

@ -1,13 +1,8 @@
import json from moto.core.exceptions import JsonRESTError
from werkzeug.exceptions import HTTPException
class BadRequestException(HTTPException): class BadRequestException(JsonRESTError):
code = 400 code = 400
def __init__(self, message, **kwargs): def __init__(self, message, **kwargs):
super().__init__( super().__init__(error_type="BadRequestException", message=message)
description=json.dumps({"Message": message, "Code": "BadRequestException"}),
**kwargs
)