From 032b9c40088e406b74b6a9aed56f791fd45b5557 Mon Sep 17 00:00:00 2001 From: Bert Blommers Date: Thu, 5 Nov 2020 11:20:18 +0000 Subject: [PATCH 1/2] Tech Debt - Remove duplicate AWSError classes --- moto/acm/models.py | 14 +----------- moto/applicationautoscaling/exceptions.py | 17 -------------- moto/batch/exceptions.py | 28 +++++------------------ moto/core/exceptions.py | 17 ++++++++++++++ moto/sagemaker/exceptions.py | 17 -------------- moto/sagemaker/responses.py | 2 +- moto/stepfunctions/exceptions.py | 18 +-------------- moto/xray/exceptions.py | 23 ------------------- moto/xray/models.py | 3 ++- moto/xray/responses.py | 3 ++- 10 files changed, 30 insertions(+), 112 deletions(-) diff --git a/moto/acm/models.py b/moto/acm/models.py index 6e4ac1508..3963b88c2 100644 --- a/moto/acm/models.py +++ b/moto/acm/models.py @@ -1,9 +1,9 @@ from __future__ import unicode_literals import re -import json import datetime from moto.core import BaseBackend, BaseModel +from moto.core.exceptions import AWSError from moto.ec2 import ec2_backends from .utils import make_arn_for_certificate @@ -50,18 +50,6 @@ def datetime_to_epoch(date): return int((date - datetime.datetime(1970, 1, 1)).total_seconds()) -class AWSError(Exception): - TYPE = None - STATUS = 400 - - def __init__(self, message): - self.message = message - - def response(self): - resp = {"__type": self.TYPE, "message": self.message} - return json.dumps(resp), dict(status=self.STATUS) - - class AWSValidationException(AWSError): TYPE = "ValidationException" diff --git a/moto/applicationautoscaling/exceptions.py b/moto/applicationautoscaling/exceptions.py index 8d5fb3c0c..e409da4e7 100644 --- a/moto/applicationautoscaling/exceptions.py +++ b/moto/applicationautoscaling/exceptions.py @@ -1,24 +1,7 @@ from __future__ import unicode_literals -import json from moto.core.exceptions import JsonRESTError -class AWSError(Exception): - """ Copied from acm/models.py; this class now exists in >5 locations, - maybe this should be centralised for use by any module? - """ - - TYPE = None - STATUS = 400 - - def __init__(self, message): - self.message = message - - def response(self): - resp = {"__type": self.TYPE, "message": self.message} - return json.dumps(resp), dict(status=self.STATUS) - - class AWSValidationException(JsonRESTError): def __init__(self, message, **kwargs): super(AWSValidationException, self).__init__( diff --git a/moto/batch/exceptions.py b/moto/batch/exceptions.py index c411f3fce..5d3ea3fd0 100644 --- a/moto/batch/exceptions.py +++ b/moto/batch/exceptions.py @@ -1,40 +1,24 @@ from __future__ import unicode_literals -import json - - -class AWSError(Exception): - CODE = None - STATUS = 400 - - def __init__(self, message, code=None, status=None): - self.message = message - self.code = code if code is not None else self.CODE - self.status = status if status is not None else self.STATUS - - def response(self): - return ( - json.dumps({"__type": self.code, "message": self.message}), - dict(status=self.status), - ) +from moto.core.exceptions import AWSError class InvalidRequestException(AWSError): - CODE = "InvalidRequestException" + TYPE = "InvalidRequestException" class InvalidParameterValueException(AWSError): - CODE = "InvalidParameterValue" + TYPE = "InvalidParameterValue" class ValidationError(AWSError): - CODE = "ValidationError" + TYPE = "ValidationError" class InternalFailure(AWSError): - CODE = "InternalFailure" + TYPE = "InternalFailure" STATUS = 500 class ClientException(AWSError): - CODE = "ClientException" + TYPE = "ClientException" STATUS = 400 diff --git a/moto/core/exceptions.py b/moto/core/exceptions.py index ea91eda63..6938f9bf1 100644 --- a/moto/core/exceptions.py +++ b/moto/core/exceptions.py @@ -2,6 +2,7 @@ from __future__ import unicode_literals from werkzeug.exceptions import HTTPException from jinja2 import DictLoader, Environment +import json SINGLE_ERROR_RESPONSE = """ @@ -109,6 +110,22 @@ class AuthFailureError(RESTError): ) +class AWSError(Exception): + TYPE = None + STATUS = 400 + + def __init__(self, message, type=None, status=None): + self.message = message + self.type = type if type is not None else self.TYPE + self.status = status if status is not None else self.STATUS + + def response(self): + return ( + json.dumps({"__type": self.type, "message": self.message}), + dict(status=self.status), + ) + + class InvalidNextTokenException(JsonRESTError): """For AWS Config resource listing. This will be used by many different resource types, and so it is in moto.core.""" diff --git a/moto/sagemaker/exceptions.py b/moto/sagemaker/exceptions.py index e2d01e82e..0331fee89 100644 --- a/moto/sagemaker/exceptions.py +++ b/moto/sagemaker/exceptions.py @@ -1,5 +1,4 @@ from __future__ import unicode_literals -import json from moto.core.exceptions import RESTError, JsonRESTError ERROR_WITH_MODEL_NAME = """{% extends 'single_error' %} @@ -30,22 +29,6 @@ class MissingModel(ModelError): ) -class AWSError(Exception): - TYPE = None - STATUS = 400 - - def __init__(self, message, type=None, status=None): - self.message = message - self.type = type if type is not None else self.TYPE - self.status = status if status is not None else self.STATUS - - def response(self): - return ( - json.dumps({"__type": self.type, "message": self.message}), - dict(status=self.status), - ) - - class ValidationError(JsonRESTError): def __init__(self, message, **kwargs): super(ValidationError, self).__init__("ValidationException", message, **kwargs) diff --git a/moto/sagemaker/responses.py b/moto/sagemaker/responses.py index 749ac787f..d5d2cab43 100644 --- a/moto/sagemaker/responses.py +++ b/moto/sagemaker/responses.py @@ -2,9 +2,9 @@ from __future__ import unicode_literals import json +from moto.core.exceptions import AWSError from moto.core.responses import BaseResponse from moto.core.utils import amzn_request_id -from .exceptions import AWSError from .models import sagemaker_backends diff --git a/moto/stepfunctions/exceptions.py b/moto/stepfunctions/exceptions.py index b5fd2ddb9..a24c15008 100644 --- a/moto/stepfunctions/exceptions.py +++ b/moto/stepfunctions/exceptions.py @@ -1,21 +1,5 @@ from __future__ import unicode_literals -import json - - -class AWSError(Exception): - TYPE = None - STATUS = 400 - - def __init__(self, message, type=None, status=None): - self.message = message - self.type = type if type is not None else self.TYPE - self.status = status if status is not None else self.STATUS - - def response(self): - return ( - json.dumps({"__type": self.type, "message": self.message}), - dict(status=self.status), - ) +from moto.core.exceptions import AWSError class ExecutionAlreadyExists(AWSError): diff --git a/moto/xray/exceptions.py b/moto/xray/exceptions.py index 8b5c87e36..2449cb45d 100644 --- a/moto/xray/exceptions.py +++ b/moto/xray/exceptions.py @@ -1,26 +1,3 @@ -import json - - -class AWSError(Exception): - CODE = None - STATUS = 400 - - def __init__(self, message, code=None, status=None): - self.message = message - self.code = code if code is not None else self.CODE - self.status = status if status is not None else self.STATUS - - def response(self): - return ( - json.dumps({"__type": self.code, "message": self.message}), - dict(status=self.status), - ) - - -class InvalidRequestException(AWSError): - CODE = "InvalidRequestException" - - class BadSegmentException(Exception): def __init__(self, seg_id=None, code=None, message=None): self.id = seg_id diff --git a/moto/xray/models.py b/moto/xray/models.py index 39d8ae2d4..6352fa37c 100644 --- a/moto/xray/models.py +++ b/moto/xray/models.py @@ -6,7 +6,8 @@ import datetime from collections import defaultdict import json from moto.core import BaseBackend, BaseModel -from .exceptions import BadSegmentException, AWSError +from moto.core.exceptions import AWSError +from .exceptions import BadSegmentException class TelemetryRecords(BaseModel): diff --git a/moto/xray/responses.py b/moto/xray/responses.py index 118f2de2f..aaf56c80a 100644 --- a/moto/xray/responses.py +++ b/moto/xray/responses.py @@ -3,10 +3,11 @@ import json import datetime from moto.core.responses import BaseResponse +from moto.core.exceptions import AWSError from six.moves.urllib.parse import urlsplit from .models import xray_backends -from .exceptions import AWSError, BadSegmentException +from .exceptions import BadSegmentException class XRayResponse(BaseResponse): From 3b6162de670d47856e6d377912c2fdf4d5f430a9 Mon Sep 17 00:00:00 2001 From: Bert Blommers Date: Fri, 6 Nov 2020 16:34:09 +0000 Subject: [PATCH 2/2] Refactor Forecast to also use shared AWSError class --- moto/forecast/exceptions.py | 18 +----------------- 1 file changed, 1 insertion(+), 17 deletions(-) diff --git a/moto/forecast/exceptions.py b/moto/forecast/exceptions.py index ad86e90fc..dbc6f6414 100644 --- a/moto/forecast/exceptions.py +++ b/moto/forecast/exceptions.py @@ -1,22 +1,6 @@ from __future__ import unicode_literals -import json - - -class AWSError(Exception): - TYPE = None - STATUS = 400 - - def __init__(self, message, type=None, status=None): - self.message = message - self.type = type if type is not None else self.TYPE - self.status = status if status is not None else self.STATUS - - def response(self): - return ( - json.dumps({"__type": self.type, "message": self.message}), - dict(status=self.status), - ) +from moto.core.exceptions import AWSError class InvalidInputException(AWSError):