-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathpersonalization_error.py
118 lines (103 loc) · 3.64 KB
/
personalization_error.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: MIT-0
"""
Error abstractions
"""
import json
from http import HTTPStatus
from botocore.exceptions import ClientError
class PersonalizationError(Exception):
def __init__(
self,
type: str,
status_code: int,
error_code: str,
error_message: str,
sdk_status_code: int = None
):
super().__init__(error_message)
self.type = type
self.status_code = status_code
self.error_code = error_code
self.error_message = error_message
self.sdk_status_code = sdk_status_code
@classmethod
def from_client_error(cls, e: ClientError):
return cls(HTTPStatus.INTERNAL_SERVER_ERROR, e.response['Error']['Code'], e.response['Error']['Message'], e.response['ResponseMetadata']['HTTPStatusCode'])
class ValidationError(PersonalizationError):
def __init__(
self,
error_code: str,
error_message: str,
):
super().__init__('Validation', HTTPStatus.BAD_REQUEST, error_code, error_message)
class JSONDecodeValidationError(ValidationError):
def __init__(
self,
error_code: str,
error_message: str
):
super().__init__(error_code, error_message)
@classmethod
def from_json_decoder_error(cls, error_code: str, e: json.decoder.JSONDecodeError):
return cls(error_code, f"{e.msg}: line {e.lineno} column {e.colno} (char {e.pos})")
class ConfigError(PersonalizationError):
def __init__(
self,
status_code: int,
error_code: str,
error_message: str,
sdk_status_code: int = None
):
super().__init__('Configuration', status_code, error_code, error_message, sdk_status_code)
class PersonalizeError(PersonalizationError):
def __init__(
self,
status_code: int,
error_code: str,
error_message: str,
sdk_status_code: int = None
):
super().__init__('Personalize', status_code, error_code, error_message, sdk_status_code)
@classmethod
def from_client_error(cls, e: ClientError):
error_code = e.response['Error']['Code']
if error_code == 'ThrottlingException':
return cls(HTTPStatus.TOO_MANY_REQUESTS, error_code, e.response['Error']['Message'], e.response['ResponseMetadata']['HTTPStatusCode'])
return cls(HTTPStatus.INTERNAL_SERVER_ERROR, error_code, e.response['Error']['Message'], e.response['ResponseMetadata']['HTTPStatusCode'])
class DynamoDbError(PersonalizationError):
def __init__(
self,
status_code: int,
error_code: str,
error_message: str,
sdk_status_code: int = None
):
super().__init__('DynamoDB', status_code, error_code, error_message, sdk_status_code)
class EvidentlyError(PersonalizationError):
def __init__(
self,
status_code: int,
error_code: str,
error_message: str,
sdk_status_code: int = None
):
super().__init__('Evidently', status_code, error_code, error_message, sdk_status_code)
class LambdaError(PersonalizationError):
def __init__(
self,
status_code: int,
error_code: str,
error_message: str,
sdk_status_code: int = None
):
super().__init__('Function', status_code, error_code, error_message, sdk_status_code)
class SageMakerError(PersonalizationError):
def __init__(
self,
status_code: int,
error_code: str,
error_message: str,
sdk_status_code: int = None
):
super().__init__('SageMaker', status_code, error_code, error_message, sdk_status_code)