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- """
131# Built-ins
142from pprint import pformat
15- from typing import Any
3+ from typing import Any , List
164
175# External
18- from marshmallow import (
19- fields ,
20- post_load ,
21- )
6+ from marshmallow import fields , Schema , post_load , ValidationError
227
238# From Current Package
9+ # Assuming that the SchemaBase import is valid and it extends from marshmallow.Schema
2410from .schema_base import SchemaBase
2511
2612
2713class IntercomErrorObjectSchema (SchemaBase ):
2814 """
2915 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).
3516 """
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 )
4021
4122 @post_load
4223 def make_intercom_error_object (self , data , ** kwargs ):
@@ -46,58 +27,51 @@ def make_intercom_error_object(self, data, **kwargs):
4627class IntercomErrorListSchema (SchemaBase ):
4728 """
4829 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.
5330 """
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 )
5734
5835 @post_load
5936 def make_intercom_error_list (self , data , ** kwargs ):
6037 return IntercomErrorList (** data )
6138
6239
63- class IntercomErrorObject :
40+ class IntercomErrorObject ( Exception ) :
6441 """ 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
7548
7649 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 } )"
8151
8252
8353class IntercomErrorList (Exception ):
8454 """ 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
8961
90- super ().__init__ (f"Error Response from Intercom API. Request ID: { self .request_id } \n { pformat (self .__dict__ )} " )
9162
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. """
9565 if 200 <= response .status_code < 300 :
9666 return response
9767
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 ( )
10272
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