59
59
}
60
60
61
61
62
+ # See https://github.com/googleapis/googleapis/blob/master/google/rpc/code.proto
63
+ _RPC_CODE_TO_ERROR_CODE = {
64
+ 1 : exceptions .CANCELLED ,
65
+ 2 : exceptions .UNKNOWN ,
66
+ 3 : exceptions .INVALID_ARGUMENT ,
67
+ 4 : exceptions .DEADLINE_EXCEEDED ,
68
+ 5 : exceptions .NOT_FOUND ,
69
+ 6 : exceptions .ALREADY_EXISTS ,
70
+ 7 : exceptions .PERMISSION_DENIED ,
71
+ 8 : exceptions .RESOURCE_EXHAUSTED ,
72
+ 9 : exceptions .FAILED_PRECONDITION ,
73
+ 10 : exceptions .ABORTED ,
74
+ 11 : exceptions .OUT_OF_RANGE ,
75
+ 13 : exceptions .INTERNAL ,
76
+ 14 : exceptions .UNAVAILABLE ,
77
+ 15 : exceptions .DATA_LOSS ,
78
+ 16 : exceptions .UNAUTHENTICATED ,
79
+ }
80
+
81
+
62
82
def _get_initialized_app (app ):
63
83
"""Returns a reference to an initialized App instance."""
64
84
if app is None :
@@ -75,6 +95,7 @@ def _get_initialized_app(app):
75
95
' firebase_admin.App, but given "{0}".' .format (type (app )))
76
96
77
97
98
+
78
99
def get_app_service (app , name , initializer ):
79
100
app = _get_initialized_app (app )
80
101
return app ._get_service (name , initializer ) # pylint: disable=protected-access
@@ -108,6 +129,27 @@ def handle_platform_error_from_requests(error, handle_func=None):
108
129
return exc if exc else _handle_func_requests (error , message , error_dict )
109
130
110
131
132
+ def handle_operation_error (error ):
133
+ """Constructs a ``FirebaseError`` from the given operation error.
134
+
135
+ Args:
136
+ error: An error returned by a long running operation.
137
+
138
+ Returns:
139
+ FirebaseError: A ``FirebaseError`` that can be raised to the user code.
140
+ """
141
+ if not isinstance (error , dict ):
142
+ return exceptions .UnknownError (
143
+ message = 'Unknown error while making a remote service call: {0}' .format (error ),
144
+ cause = error )
145
+
146
+ rpc_code = error .get ('code' )
147
+ message = error .get ('message' )
148
+ error_code = _rpc_code_to_error_code (rpc_code )
149
+ err_type = _error_code_to_exception_type (error_code )
150
+ return err_type (message = message )
151
+
152
+
111
153
def _handle_func_requests (error , message , error_dict ):
112
154
"""Constructs a ``FirebaseError`` from the given GCP error.
113
155
@@ -264,6 +306,9 @@ def _http_status_to_error_code(status):
264
306
63DA
"""Maps an HTTP status to a platform error code."""
265
307
return _HTTP_STATUS_TO_ERROR_CODE .get (status , exceptions .UNKNOWN )
266
308
309
+ def _rpc_code_to_error_code (rpc_code ):
310
+ """Maps an RPC code to a platform error code."""
311
+ return _RPC_CODE_TO_ERROR_CODE .get (rpc_code , exceptions .UNKNOWN )
267
312
268
313
def _error_code_to_exception_type (code ):
269
314
"""Maps a platform error code to an exception type."""
0 commit comments