2020import * as jwt from 'jsonwebtoken' ;
2121import * as _ from 'lodash' ;
2222import * as chai from 'chai' ;
23- import * as nock from 'nock' ;
2423import * as sinon from 'sinon' ;
2524import * as sinonChai from 'sinon-chai' ;
2625import * as chaiAsPromised from 'chai-as-promised' ;
@@ -32,7 +31,7 @@ import { FirebaseApp } from '../../../src/app/firebase-app';
3231import {
3332 AuthRequestHandler , TenantAwareAuthRequestHandler , AbstractAuthRequestHandler ,
3433} from '../../../src/auth/auth-api-request' ;
35- import { AuthClientErrorCode , FirebaseAppError , FirebaseAuthError } from '../../../src/utils/error' ;
34+ import { AuthClientErrorCode , FirebaseAuthError } from '../../../src/utils/error' ;
3635
3736import * as validator from '../../../src/utils/validator' ;
3837import { DecodedAuthBlockingToken , FirebaseTokenVerifier } from '../../../src/auth/token-verifier' ;
@@ -3977,167 +3976,5 @@ AUTH_CONFIGS.forEach((testConfig) => {
39773976 } ) ;
39783977 } ) ;
39793978
3980- describe ( 'Standardized Error Handling Demonstration' , ( ) => {
3981- let auth : Auth ;
3982- let mockApp : FirebaseApp ;
3983-
3984- beforeEach ( ( ) => {
3985- mockApp = mocks . appWithOptions ( {
3986- credential : new mocks . MockCredential ( ) ,
3987- projectId : 'project-id' ,
3988- } ) ;
3989- auth = new Auth ( mockApp ) ;
3990- } ) ;
3991-
3992- afterEach ( ( ) => {
3993- nock . cleanAll ( ) ;
3994- } ) ;
3995-
3996- it ( 'demoErrorWithCause: should have a cause when a low-level network error occurs' , async ( ) => {
3997- const uid = 'some-uid' ;
3998- const expectedError = new Error ( 'Network failure' ) ;
3999-
4000- // Simulate a low-level network error using nock
4001- nock ( 'https://identitytoolkit.googleapis.com' )
4002- . post ( / .* a c c o u n t s : l o o k u p .* / )
4003- . replyWithError ( expectedError ) ;
4004-
4005- console . log ( '\n\n=======================================================' ) ;
4006- console . log ( '🔥 DEMONSTRATION 1: FIREBASE ERROR WITH CAUSE' ) ;
4007- console . log ( '=======================================================' ) ;
4008- try {
4009- console . log ( 'Attempting to fetch a user with a simulated network error...' ) ;
4010- await auth . getUser ( uid ) ;
4011- } catch ( err : any ) {
4012- if ( err instanceof FirebaseAppError || err instanceof FirebaseAuthError ) {
4013- console . log ( '\n🔥 Caught Exception:' ) ;
4014- console . log ( 'Exception message:' , err . message ) ;
4015- console . log ( 'Error code:' , err . code ) ;
4016- console . log ( 'Cause:' , err . cause ) ;
4017-
4018- if ( err . httpResponse ) {
4019- console . log ( '\n📡 HTTP Response Metadata Attached:' ) ;
4020- console . log ( 'Status Code:' , err . httpResponse . status ) ;
4021- console . log ( 'Content Data:' , err . httpResponse . data ) ;
4022- console . log ( 'Headers:' , err . httpResponse . headers ) ;
4023- } else {
4024- console . log ( '\n📡 No HTTP Response metadata attached.' ) ;
4025- }
4026- }
4027- }
4028- console . log ( '=======================================================\n\n' ) ;
4029- } ) ;
4030-
4031- it ( 'demoErrorWithResponse: should have httpResponse when a request fails' , async ( ) => {
4032- const uid = 'some-uid' ;
4033-
4034- // Simulate a server error using nock
4035- nock ( 'https://identitytoolkit.googleapis.com' )
4036- . post ( / .* a c c o u n t s : l o o k u p .* / )
4037- . reply ( 200 , {
4038- // Missing 'users' field
4039- somethingElse : 'unexpected'
4040- } ) ;
4041-
4042- console . log ( '\n\n=======================================================' ) ;
4043- console . log ( '🔥 DEMONSTRATION 2: FIREBASE ERROR WITH RESPONSE' ) ;
4044- console . log ( '=======================================================' ) ;
4045- try {
4046- console . log ( 'Attempting to fetch a user with a simulated server error...' ) ;
4047- await auth . getUser ( uid ) ;
4048- } catch ( err : any ) {
4049- if ( err instanceof FirebaseAppError || err instanceof FirebaseAuthError ) {
4050- console . log ( '\n🔥 Caught Exception:' ) ;
4051- console . log ( 'Exception message:' , err . message ) ;
4052- console . log ( 'Error code:' , err . code ) ;
4053- console . log ( 'Cause:' , err . cause ) ;
4054-
4055- if ( err . httpResponse ) {
4056- console . log ( '\n📡 HTTP Response Metadata Attached:' ) ;
4057- console . log ( ' Status Code:' , err . httpResponse . status ) ;
4058- console . log ( ' Content Data:' , JSON . stringify ( err . httpResponse . data ) ) ;
4059- console . log ( ' Headers:' , JSON . stringify ( err . httpResponse . headers ) ) ;
4060- } else {
4061- console . log ( '\n📡 No HTTP Response metadata attached.' ) ;
4062- }
4063- }
4064- }
4065- console . log ( '=======================================================\n\n' ) ;
4066- } ) ;
4067- it ( 'demoErrorWithUnextractableCode: should have cause when errorCode is missing' , async ( ) => {
4068- const uid = 'some-uid' ;
4069-
4070- // Simulate a server error with an invalid body using nock
4071- nock ( 'https://identitytoolkit.googleapis.com' )
4072- . post ( / .* a c c o u n t s : l o o k u p .* / )
4073- . reply ( 500 , {
4074- // No error code or message
4075- foo : 'bar'
4076- } ) ;
4077-
4078- console . log ( '\n\n=======================================================' ) ;
4079- console . log ( '🔥 DEMONSTRATION 3: FIREBASE ERROR WITH UNEXTRACTABLE CODE' ) ;
4080- console . log ( '=======================================================' ) ;
4081- try {
4082- console . log ( 'Attempting to fetch a user with a simulated unextractable error...' ) ;
4083- await auth . getUser ( uid ) ;
4084- } catch ( err : any ) {
4085- if ( err instanceof FirebaseAppError || err instanceof FirebaseAuthError ) {
4086- console . log ( '\n🔥 Caught Exception:' ) ;
4087- console . log ( err ) ;
4088- console . log ( 'Exception message:' , err . message ) ;
4089- console . log ( 'Error code:' , err . code ) ;
4090- console . log ( 'Cause:' , err . cause ) ;
4091-
4092- if ( err . httpResponse ) {
4093- console . log ( '\n📡 HTTP Response Metadata Attached:' ) ;
4094- console . log ( ' Status Code:' , err . httpResponse . status ) ;
4095- console . log ( ' Content Data:' , JSON . stringify ( err . httpResponse . data ) ) ;
4096- console . log ( ' Headers:' , JSON . stringify ( err . httpResponse . headers ) ) ;
4097- } else {
4098- console . log ( '\n📡 No HTTP Response metadata attached.' ) ;
4099- }
4100- }
4101- }
4102- console . log ( '=======================================================\n\n' ) ;
4103- } ) ;
41043979
4105- it ( 'demoErrorWithValidFormat: should have cause and httpResponse when error has valid format' , async ( ) => {
4106- const uid = 'some-uid' ;
4107-
4108- // Simulate a server error with a valid body using nock
4109- nock ( 'https://identitytoolkit.googleapis.com' )
4110- . post ( / .* a c c o u n t s : l o o k u p .* / )
4111- . reply ( 400 , {
4112- error : {
4113- message : 'USER_NOT_FOUND : Some detailed custom message here: with more colons' ,
4114- }
4115- } ) ;
4116-
4117- console . log ( '\n\n=======================================================' ) ;
4118- console . log ( '🔥 DEMONSTRATION 4: FIREBASE ERROR WITH VALID FORMAT' ) ;
4119- console . log ( '=======================================================' ) ;
4120- try {
4121- console . log ( 'Attempting to fetch a user with a simulated valid formatted error...' ) ;
4122- await auth . getUser ( uid ) ;
4123- } catch ( err : any ) {
4124- if ( err instanceof FirebaseAppError || err instanceof FirebaseAuthError ) {
4125- console . log ( '\n🔥 Caught Exception:' ) ;
4126- console . log ( 'Exception message:' , err . message ) ;
4127- console . log ( 'Error code:' , err . code ) ;
4128- console . log ( 'Cause:' , err . cause ) ;
4129-
4130- if ( err . httpResponse ) {
4131- console . log ( '\n📡 HTTP Response Metadata Attached:' ) ;
4132- console . log ( ' Status Code:' , err . httpResponse . status ) ;
4133- console . log ( ' Content Data:' , JSON . stringify ( err . httpResponse . data ) ) ;
4134- console . log ( ' Headers:' , JSON . stringify ( err . httpResponse . headers ) ) ;
4135- } else {
4136- console . log ( '\n📡 No HTTP Response metadata attached.' ) ;
4137- }
4138- }
4139- }
4140- console . log ( '=======================================================\n\n' ) ;
4141- } ) ;
4142- } ) ;
41433980} ) ;
0 commit comments