File tree Expand file tree Collapse file tree 2 files changed +31
-2
lines changed Expand file tree Collapse file tree 2 files changed +31
-2
lines changed Original file line number Diff line number Diff line change @@ -43,9 +43,18 @@ export class FirebaseMessagingRequestHandler {
4343 */
4444 private static getErrorCode ( response : any ) : string | null {
4545 if ( validator . isNonNullObject ( response ) && 'error' in response ) {
46- if ( typeof response . error === 'string' ) {
46+ if ( validator . isString ( response . error ) ) {
4747 return response . error ;
48- } else if ( 'status' in response . error ) {
48+ }
49+ if ( validator . isArray ( response . error . details ) ) {
50+ const fcmErrorType = 'type.googleapis.com/google.firebase.fcm.v1.FcmErrorCode' ;
51+ for ( const element of response . error . details ) {
52+ if ( element [ '@type' ] === fcmErrorType ) {
53+ return element . errorCode ;
54+ }
55+ }
56+ }
57+ if ( 'status' in response . error ) {
4958 return response . error . status ;
5059 } else {
5160 return response . error . message ;
Original file line number Diff line number Diff line change @@ -419,6 +419,26 @@ describe('Messaging', () => {
419419 . and . have . property ( 'code' , 'messaging/invalid-argument' ) ;
420420 } ) ;
421421
422+ it ( 'should fail when the backend server returns a detailed error with FCM error code' , ( ) => {
423+ const resp = {
424+ error : {
425+ status : 'INVALID_ARGUMENT' ,
426+ message : 'test error message' ,
427+ details : [
428+ {
429+ '@type' : 'type.googleapis.com/google.firebase.fcm.v1.FcmErrorCode' ,
430+ 'errorCode' : 'UNREGISTERED' ,
431+ } ,
432+ ] ,
433+ } ,
434+ } ;
435+ mockedRequests . push ( mockSendError ( 404 , 'json' , resp ) ) ;
436+ return messaging . send (
437+ { token : 'mock-token' } ,
438+ ) . should . eventually . be . rejectedWith ( 'test error message' )
439+ . and . have . property ( 'code' , 'messaging/registration-token-not-registered' ) ;
440+ } ) ;
441+
422442 it ( 'should map server error code to client-side error' , ( ) => {
423443 const resp = {
424444 error : {
You can’t perform that action at this time.
0 commit comments