1
- """
2
- # Intercom Error Objects
3
-
4
- `core/errors.py`
5
-
6
- This module contains the IntercomErrorObject and IntercomErrorList classes, which
7
- are custom exceptions for Intercom error objects. These models/schemas are implemented
8
- as defined by the Intercom API Reference [1].
9
-
10
- ---
11
- - [1] https://developers.intercom.com/intercom-api-reference/reference/error-objects
12
- """
13
1
# Built-ins
14
2
from pprint import pformat
15
- from typing import Any
3
+ from typing import Any , List
16
4
17
5
# External
18
- from marshmallow import (
19
- fields ,
20
- post_load ,
21
- )
6
+ from marshmallow import fields , Schema , post_load , ValidationError
22
7
23
8
# From Current Package
9
+ # Assuming that the SchemaBase import is valid and it extends from marshmallow.Schema
24
10
from .schema_base import SchemaBase
25
11
26
12
27
13
class IntercomErrorObjectSchema (SchemaBase ):
28
14
"""
29
15
Schema for an Intercom error object.
30
-
31
- Attributes:
32
- code (str): The code of the error.
33
- message (str): The message of the error.
34
- field (str): The field of the error (optional).
35
16
"""
36
- code = fields .Str ()
37
- message = fields .Str ()
38
- field = fields .Str ()
39
- request_id = fields .Str ()
17
+ code = fields .Str (required = True )
18
+ message = fields .Str (required = True )
19
+ field = fields .Str (missing = None )
20
+ request_id = fields .Str (missing = None )
40
21
41
22
@post_load
42
23
def make_intercom_error_object (self , data , ** kwargs ):
@@ -46,58 +27,51 @@ def make_intercom_error_object(self, data, **kwargs):
46
27
class IntercomErrorListSchema (SchemaBase ):
47
28
"""
48
29
Schema for a list of Intercom error objects.
49
-
50
- Attributes:
51
- type (str): The type of the error.
52
- errors (List[IntercomErrorObjectSchema]): The list of errors.
53
30
"""
54
- type = fields .Str ()
55
- errors = fields .List (fields .Nested (IntercomErrorObjectSchema ))
56
- request_id = fields .Str ()
31
+ type = fields .Str (required = True )
32
+ errors = fields .List (fields .Nested (IntercomErrorObjectSchema ), required = True )
33
+ request_id = fields .Str (missing = None )
57
34
58
35
@post_load
59
36
def make_intercom_error_list (self , data , ** kwargs ):
60
37
return IntercomErrorList (** data )
61
38
62
39
63
- class IntercomErrorObject :
40
+ class IntercomErrorObject ( Exception ) :
64
41
""" Custom exception for an Intercom error object. """
65
-
66
- def __init__ (self , ** kwargs : Any ):
67
- self .code = kwargs .get ("code" , "" )
68
- self .message = kwargs .get ("message" , "" )
69
-
70
- if kwargs .get ("field" ):
71
- self .field = kwargs .get ("field" )
72
-
73
- if kwargs .get ("request_id" ):
74
- self .request_id = kwargs .get ("request_id" )
42
+ def __init__ (self , code : str , message : str , field : str = None , request_id : str = None ):
43
+ super ().__init__ (message )
44
+ self .code = code
45
+ self .message = message
46
+ self .field = field
47
+ self .request_id = request_id
75
48
76
49
def __str__ (self ):
77
- return f"\n { pformat (self .__dict__ )} \n "
78
-
79
- def __repr__ (self ):
80
- return self .__str__ ()
50
+ return f"IntercomErrorObject(code={ self .code } , message={ self .message } , field={ self .field } , request_id={ self .request_id } )"
81
51
82
52
83
53
class IntercomErrorList (Exception ):
84
54
""" Custom exception for a list of Intercom error objects. """
85
- def __init__ (self , ** kwargs ):
86
- self .type = kwargs .get ("type" , "" )
87
- self .errors = kwargs .get ("errors" , [])
88
- self .request_id = kwargs .get ("request_id" , None )
55
+ def __init__ (self , type : str , errors : List [IntercomErrorObject ], request_id : str = None ):
56
+ message = f"Intercom API returned multiple errors: { pformat (errors )} "
57
+ super ().__init__ (message )
58
+ self .type = type
59
+ self .errors = errors
60
+ self .request_id = request_id
89
61
90
- super ().__init__ (f"Error Response from Intercom API. Request ID: { self .request_id } \n { pformat (self .__dict__ )} " )
91
62
92
-
93
- def catch_api_error (response ): # type: ignore
94
- """ Catches API errors and raises them as as custom exceptions. """
63
+ def catch_api_error (response ):
64
+ """ Catches API errors and raises them as custom exceptions. """
95
65
if 200 <= response .status_code < 300 :
96
66
return response
97
67
98
- data = response . json ()
99
- error = IntercomErrorListSchema (). load ( data )
100
-
101
- print ( response .content )
68
+ try :
69
+ data = response . json ( )
70
+ except ValueError :
71
+ response .raise_for_status ( )
102
72
103
- raise error # type: ignore
73
+ try :
74
+ error_list = IntercomErrorListSchema ().load (data )
75
+ raise error_list
76
+ except ValidationError as e :
77
+ raise ValueError (f"Error parsing error response: { e .messages } " )
0 commit comments