@@ -16,15 +16,27 @@ class PrimaryKeyError(LocalError):
1616
1717# https://developer.mozilla.org/en-US/docs/Web/HTTP/Status#client_error_responses
1818class APIError (Exception ):
19- def __init__ (self , message : Optional [str ] = None ):
19+ def __init__ (self , project_id : str , message : Optional [str ] = None ):
20+ self .project_id = project_id
2021 if message is None :
2122 message = "Please check the SDK documentation at https://github.com/code-kern-ai/refinery-python"
2223 super ().__init__ (message )
2324
2425
2526# 401 Unauthorized
2627class UnauthorizedError (APIError ):
27- pass
28+ def __init__ (self , project_id : str , message : Optional [str ] = None ):
29+ super ().__init__ (project_id , message )
30+
31+
32+ # 403 Forbidden
33+ class ForbiddenError (APIError ):
34+ def __init__ (
35+ self ,
36+ project_id : str ,
37+ message = "You can't access the project with your current role." ,
38+ ):
39+ super ().__init__ (project_id , message )
2840
2941
3042# 404 Not Found
@@ -33,15 +45,17 @@ class NotFoundError(APIError):
3345
3446
3547class UnknownProjectError (APIError ):
36- def __init__ (self , project_id : str ):
48+ def __init__ (self , project_id : str , message : Optional [ str ] = None ):
3749 super ().__init__ (
38- message = f"Could not find project '{ project_id } '. Please check your input."
50+ project_id ,
51+ f"Could not find project '{ project_id } '. Please check your input." ,
3952 )
4053
4154
4255# 500 Server Error
4356class InternalServerError (APIError ):
44- pass
57+ def __init__ (self , project_id : str , message : Optional [str ] = None ):
58+ super ().__init__ (project_id , message )
4559
4660
4761class FileImportError (Exception ):
@@ -51,11 +65,13 @@ class FileImportError(Exception):
5165# mirror this from the rest api class ErrorCodes
5266class ErrorCodes :
5367 UNRECOGNIZED_USER = "UNRECOGNIZED_USER" # not actively used in SDK
68+ FORBIDDEN_USER = "FORBIDDEN_USER"
5469 PROJECT_NOT_FOUND = "PROJECT_NOT_FOUND"
5570
5671
5772RESPONSE_CODES_API_EXCEPTION_MAP = {
5873 401 : {"*" : UnauthorizedError },
74+ 403 : {"*" : ForbiddenError },
5975 404 : {"*" : NotFoundError , ErrorCodes .PROJECT_NOT_FOUND : UnknownProjectError },
6076 500 : {"*" : InternalServerError },
6177}
@@ -65,10 +81,11 @@ def get_api_exception_class(
6581 status_code : int ,
6682 error_code : Optional [str ] = None ,
6783 error_message : Optional [str ] = None ,
84+ project_id : Optional [str ] = None ,
6885) -> APIError :
6986 exception_or_dict = RESPONSE_CODES_API_EXCEPTION_MAP .get (status_code , APIError )
7087 if isinstance (exception_or_dict , dict ):
7188 exception_class = exception_or_dict .get (error_code , exception_or_dict ["*" ])
89+ return exception_class (project_id = project_id )
7290 else :
73- exception_class = exception_or_dict
74- return exception_class (error_message )
91+ return exception_or_dict (project_id = project_id , message = error_message )
0 commit comments