Skip to content

Commit 9e0fd4f

Browse files
committed
fix: Used a helper function to map RequestResponse to HttpResponse
1 parent 8457e85 commit 9e0fd4f

22 files changed

+380
-142
lines changed

src/app-check/app-check-api-client-internal.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import { FirebaseApp } from '../app/firebase-app';
2020
import {
2121
HttpRequestConfig, HttpClient, RequestResponseError, AuthorizedHttpClient, RequestResponse
2222
} from '../utils/api-request';
23-
import { PrefixedFirebaseError, ErrorInfo } from '../utils/error';
23+
import { PrefixedFirebaseError, ErrorInfo, toHttpResponse } from '../utils/error';
2424
import * as utils from '../utils/index';
2525
import * as validator from '../utils/validator';
2626
import { AppCheckToken } from './app-check-api';
@@ -113,11 +113,7 @@ export class AppCheckApiClient {
113113
throw new FirebaseAppCheckError({
114114
code: 'invalid-argument',
115115
message: '`alreadyConsumed` must be a boolean value.',
116-
httpResponse: {
117-
status: resp.status,
118-
headers: resp.headers,
119-
data: resp.data
120-
}
116+
httpResponse: toHttpResponse(resp)
121117
});
122118
}
123119
return resp.data.alreadyConsumed || false;
@@ -179,7 +175,7 @@ export class AppCheckApiClient {
179175
return new FirebaseAppCheckError({
180176
code: 'unknown-error',
181177
message: `Unexpected response with status: ${response.status} and body: ${response.text}`,
182-
httpResponse: err.response,
178+
httpResponse: toHttpResponse(response),
183179
cause: err
184180
});
185181
}
@@ -190,7 +186,7 @@ export class AppCheckApiClient {
190186
code = APP_CHECK_ERROR_CODE_MAPPING[error.status];
191187
}
192188
const message = error.message || `Unknown server error: ${response.text}`;
193-
return new FirebaseAppCheckError({ code, message, httpResponse: err.response, cause: err });
189+
return new FirebaseAppCheckError({ code, message, httpResponse: toHttpResponse(response), cause: err });
194190
}
195191

196192
/**

src/auth/auth-api-request.ts

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import * as validator from '../utils/validator';
2020
import { App } from '../app/index';
2121
import { FirebaseApp } from '../app/firebase-app';
2222
import { deepCopy, deepExtend } from '../utils/deep-copy';
23-
import { AuthClientErrorCode, FirebaseAuthError } from '../utils/error';
23+
import { AuthClientErrorCode, FirebaseAuthError, toHttpResponse } from '../utils/error';
2424
import {
2525
ApiSettings, AuthorizedHttpClient, HttpRequestConfig, RequestResponseError, RequestResponse,
2626
} from '../utils/api-request';
@@ -591,7 +591,7 @@ export const FIREBASE_AUTH_CREATE_SESSION_COOKIE =
591591
if (!validator.isNonEmptyString(response.data?.sessionCookie)) {
592592
throw new FirebaseAuthError({
593593
...AuthClientErrorCode.INTERNAL_ERROR,
594-
httpResponse: response,
594+
httpResponse: toHttpResponse(response),
595595
});
596596
}
597597
});
@@ -660,7 +660,7 @@ export const FIREBASE_AUTH_GET_ACCOUNT_INFO = new ApiSettings('/accounts:lookup'
660660
if (!data.users || !data.users.length) {
661661
throw new FirebaseAuthError({
662662
...AuthClientErrorCode.USER_NOT_FOUND,
663-
httpResponse: response,
663+
httpResponse: toHttpResponse(response),
664664
});
665665
}
666666
});
@@ -736,14 +736,14 @@ export const FIREBASE_AUTH_BATCH_DELETE_ACCOUNTS = new ApiSettings('/accounts:ba
736736
throw new FirebaseAuthError({
737737
...AuthClientErrorCode.INTERNAL_ERROR,
738738
message: 'INTERNAL ASSERT FAILED: Server BatchDeleteAccountResponse is missing an errors.index field',
739-
httpResponse: response,
739+
httpResponse: toHttpResponse(response),
740740
});
741741
}
742742
if (!batchDeleteErrorInfo.localId) {
743743
throw new FirebaseAuthError({
744744
...AuthClientErrorCode.INTERNAL_ERROR,
745745
message: 'INTERNAL ASSERT FAILED: Server BatchDeleteAccountResponse is missing an errors.localId field',
746-
httpResponse: response,
746+
httpResponse: toHttpResponse(response),
747747
});
748748
}
749749
// Allow the (error) message to be missing/undef.
@@ -779,7 +779,7 @@ export const FIREBASE_AUTH_SET_ACCOUNT_INFO = new ApiSettings('/accounts:update'
779779
if (!data?.localId) {
780780
throw new FirebaseAuthError({
781781
...AuthClientErrorCode.USER_NOT_FOUND,
782-
httpResponse: response,
782+
httpResponse: toHttpResponse(response),
783783
});
784784
}
785785
});
@@ -823,7 +823,7 @@ export const FIREBASE_AUTH_SIGN_UP_NEW_USER = new ApiSettings('/accounts', 'POST
823823
throw new FirebaseAuthError({
824824
...AuthClientErrorCode.INTERNAL_ERROR,
825825
message: 'INTERNAL ASSERT FAILED: Unable to create new user',
826-
httpResponse: response,
826+
httpResponse: toHttpResponse(response),
827827
});
828828
}
829829
});
@@ -856,7 +856,7 @@ const FIREBASE_AUTH_GET_OOB_CODE = new ApiSettings('/accounts:sendOobCode', 'POS
856856
throw new FirebaseAuthError({
857857
...AuthClientErrorCode.INTERNAL_ERROR,
858858
message: 'INTERNAL ASSERT FAILED: Unable to create the email action link',
859-
httpResponse: response,
859+
httpResponse: toHttpResponse(response),
860860
});
861861
}
862862
});
@@ -875,7 +875,7 @@ const GET_OAUTH_IDP_CONFIG = new ApiSettings('/oauthIdpConfigs/{providerId}', 'G
875875
throw new FirebaseAuthError({
876876
...AuthClientErrorCode.INTERNAL_ERROR,
877877
message: 'INTERNAL ASSERT FAILED: Unable to get OIDC configuration',
878-
httpResponse: response,
878+
httpResponse: toHttpResponse(response),
879879
});
880880
}
881881
});
@@ -901,7 +901,7 @@ const CREATE_OAUTH_IDP_CONFIG = new ApiSettings('/oauthIdpConfigs?oauthIdpConfig
901901
throw new FirebaseAuthError({
902902
...AuthClientErrorCode.INTERNAL_ERROR,
903903
message: 'INTERNAL ASSERT FAILED: Unable to create new OIDC configuration',
904-
httpResponse: response,
904+
httpResponse: toHttpResponse(response),
905905
});
906906
}
907907
});
@@ -920,7 +920,7 @@ const UPDATE_OAUTH_IDP_CONFIG = new ApiSettings('/oauthIdpConfigs/{providerId}?u
920920
throw new FirebaseAuthError({
921921
...AuthClientErrorCode.INTERNAL_ERROR,
922922
message: 'INTERNAL ASSERT FAILED: Unable to update OIDC configuration',
923-
httpResponse: response,
923+
httpResponse: toHttpResponse(response),
924924
});
925925
}
926926
});
@@ -964,7 +964,7 @@ const GET_INBOUND_SAML_CONFIG = new ApiSettings('/inboundSamlConfigs/{providerId
964964
throw new FirebaseAuthError({
965965
...AuthClientErrorCode.INTERNAL_ERROR,
966966
message: 'INTERNAL ASSERT FAILED: Unable to get SAML configuration',
967-
httpResponse: response,
967+
httpResponse: toHttpResponse(response),
968968
});
969969
}
970970
});
@@ -990,7 +990,7 @@ const CREATE_INBOUND_SAML_CONFIG = new ApiSettings('/inboundSamlConfigs?inboundS
990990
throw new FirebaseAuthError({
991991
...AuthClientErrorCode.INTERNAL_ERROR,
992992
message: 'INTERNAL ASSERT FAILED: Unable to create new SAML configuration',
993-
httpResponse: response,
993+
httpResponse: toHttpResponse(response),
994994
});
995995
}
996996
});
@@ -1009,7 +1009,7 @@ const UPDATE_INBOUND_SAML_CONFIG = new ApiSettings('/inboundSamlConfigs/{provide
10091009
throw new FirebaseAuthError({
10101010
...AuthClientErrorCode.INTERNAL_ERROR,
10111011
message: 'INTERNAL ASSERT FAILED: Unable to update SAML configuration',
1012-
httpResponse: response,
1012+
httpResponse: toHttpResponse(response),
10131013
});
10141014
}
10151015
});
@@ -1977,7 +1977,7 @@ export abstract class AbstractAuthRequestHandler {
19771977
...AuthClientErrorCode.INTERNAL_ERROR,
19781978
message: 'An internal error occurred while attempting to extract the errorcode from the error.',
19791979
cause: err,
1980-
httpResponse: err.response,
1980+
httpResponse: toHttpResponse(err.response),
19811981
});
19821982
}
19831983
throw FirebaseAuthError.fromServerError(errorCode, /* message */ undefined, err);
@@ -2026,7 +2026,7 @@ const GET_PROJECT_CONFIG = new ApiSettings('/config', 'GET')
20262026
throw new FirebaseAuthError({
20272027
...AuthClientErrorCode.INTERNAL_ERROR,
20282028
message: 'INTERNAL ASSERT FAILED: Unable to get project config',
2029-
httpResponse: response,
2029+
httpResponse: toHttpResponse(response),
20302030
});
20312031
}
20322032
});
@@ -2040,7 +2040,7 @@ const UPDATE_PROJECT_CONFIG = new ApiSettings('/config?updateMask={updateMask}',
20402040
throw new FirebaseAuthError({
20412041
...AuthClientErrorCode.INTERNAL_ERROR,
20422042
message: 'INTERNAL ASSERT FAILED: Unable to update project config',
2043-
httpResponse: response,
2043+
httpResponse: toHttpResponse(response),
20442044
});
20452045
}
20462046
});
@@ -2055,7 +2055,7 @@ const GET_TENANT = new ApiSettings('/tenants/{tenantId}', 'GET')
20552055
throw new FirebaseAuthError({
20562056
...AuthClientErrorCode.INTERNAL_ERROR,
20572057
message: 'INTERNAL ASSERT FAILED: Unable to get tenant',
2058-
httpResponse: response,
2058+
httpResponse: toHttpResponse(response),
20592059
});
20602060
}
20612061
});
@@ -2074,7 +2074,7 @@ const UPDATE_TENANT = new ApiSettings('/tenants/{tenantId}?updateMask={updateMas
20742074
throw new FirebaseAuthError({
20752075
...AuthClientErrorCode.INTERNAL_ERROR,
20762076
message: 'INTERNAL ASSERT FAILED: Unable to update tenant',
2077-
httpResponse: response,
2077+
httpResponse: toHttpResponse(response),
20782078
});
20792079
}
20802080
});
@@ -2111,7 +2111,7 @@ const CREATE_TENANT = new ApiSettings('/tenants', 'POST')
21112111
throw new FirebaseAuthError({
21122112
...AuthClientErrorCode.INTERNAL_ERROR,
21132113
message: 'INTERNAL ASSERT FAILED: Unable to create tenant',
2114-
httpResponse: response,
2114+
httpResponse: toHttpResponse(response),
21152115
});
21162116
}
21172117
});

src/data-connect/data-connect-api-client-internal.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import { FirebaseApp } from '../app/firebase-app';
2020
import {
2121
HttpRequestConfig, HttpClient, RequestResponseError, AuthorizedHttpClient
2222
} from '../utils/api-request';
23-
import { PrefixedFirebaseError, ErrorInfo } from '../utils/error';
23+
import { PrefixedFirebaseError, ErrorInfo, toHttpResponse } from '../utils/error';
2424
import * as utils from '../utils/index';
2525
import * as validator from '../utils/validator';
2626
import { ConnectorConfig, ExecuteGraphqlResponse, GraphqlOptions, OperationOptions } from './data-connect-api';
@@ -385,7 +385,7 @@ export class DataConnectApiClient {
385385
throw new FirebaseDataConnectError({
386386
code: DATA_CONNECT_ERROR_CODE_MAPPING.QUERY_ERROR,
387387
message: allMessages,
388-
httpResponse: resp,
388+
httpResponse: toHttpResponse(resp),
389389
});
390390
}
391391
return Promise.resolve({
@@ -403,7 +403,7 @@ export class DataConnectApiClient {
403403
return new FirebaseDataConnectError({
404404
code: DATA_CONNECT_ERROR_CODE_MAPPING.UNKNOWN,
405405
message: `Unexpected response with status: ${response.status} and body: ${response.text}`,
406-
httpResponse: err.response,
406+
httpResponse: toHttpResponse(response),
407407
cause: err
408408
});
409409
}
@@ -414,7 +414,12 @@ export class DataConnectApiClient {
414414
code = DATA_CONNECT_ERROR_CODE_MAPPING[error.status];
415415
}
416416
const message = error.message || `Unknown server error: ${response.text}`;
417-
return new FirebaseDataConnectError({ code: code, message: message, httpResponse: err.response, cause: err });
417+
return new FirebaseDataConnectError({
418+
code,
419+
message,
420+
httpResponse: toHttpResponse(response),
421+
cause: err,
422+
});
418423
}
419424

420425
/**

src/database/database.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import { URL } from 'url';
1818
import * as path from 'path';
1919

2020
import { FirebaseDatabase } from '@firebase/database-types';
21-
import { FirebaseDatabaseError, AppErrorCodes, FirebaseAppError } from '../utils/error';
21+
import { FirebaseDatabaseError, AppErrorCodes, FirebaseAppError, toHttpResponse } from '../utils/error';
2222
import { Database as DatabaseImpl } from '@firebase/database-compat/standalone';
2323

2424
import { App } from '../app';
@@ -299,7 +299,7 @@ class DatabaseRulesClient {
299299
return new FirebaseDatabaseError({
300300
code: AppErrorCodes.INTERNAL_ERROR,
301301
message: this.getErrorMessage(err),
302-
httpResponse: err.response,
302+
httpResponse: toHttpResponse(err.response),
303303
cause: err,
304304
});
305305
}

src/eventarc/eventarc-client-internal.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import {
2424
} from '../utils/api-request';
2525
import { FirebaseApp } from '../app/firebase-app';
2626
import * as utils from '../utils';
27-
import { PrefixedFirebaseError } from '../utils/error';
27+
import { PrefixedFirebaseError, toHttpResponse } from '../utils/error';
2828
import { CloudEvent } from './cloudevent';
2929

3030
const EVENTARC_API = 'https://eventarcpublishing.googleapis.com/v1';
@@ -128,7 +128,7 @@ export class EventarcApiClient {
128128
return new FirebaseEventarcError({
129129
code: 'unknown-error',
130130
message: `Unexpected response with status: ${response.status}.`,
131-
httpResponse: response,
131+
httpResponse: toHttpResponse(response),
132132
cause: err,
133133
});
134134
}

src/extensions/extensions-api-client-internal.ts

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import { App } from '../app';
1919
import { FirebaseApp } from '../app/firebase-app';
2020
import { AuthorizedHttpClient, HttpClient, RequestResponseError, HttpRequestConfig } from '../utils/api-request';
21-
import { FirebaseAppError, PrefixedFirebaseError, ErrorInfo } from '../utils/error';
21+
import { FirebaseAppError, PrefixedFirebaseError, ErrorInfo, toHttpResponse } from '../utils/error';
2222
import * as validator from '../utils/validator';
2323
import * as utils from '../utils';
2424

@@ -87,21 +87,41 @@ export class ExtensionsApiClient {
8787
return new FirebaseExtensionsError({
8888
code: 'unknown-error',
8989
message: `Unexpected response with status: ${response.status} and body: ${response.text}`,
90-
httpResponse: err.response,
90+
httpResponse: toHttpResponse(response),
9191
cause: err
9292
});
9393
}
9494
const error = response.data?.error;
9595
const message = error?.message || `Unknown server error: ${response.text}`;
9696
switch (error.code) {
9797
case 403:
98-
return new FirebaseExtensionsError({ code: 'forbidden', message, httpResponse: err.response, cause: err });
98+
return new FirebaseExtensionsError({
99+
code: 'forbidden',
100+
message,
101+
httpResponse: toHttpResponse(response),
102+
cause: err,
103+
});
99104
case 404:
100-
return new FirebaseExtensionsError({ code: 'not-found', message, httpResponse: err.response, cause: err });
105+
return new FirebaseExtensionsError({
106+
code: 'not-found',
107+
message,
108+
httpResponse: toHttpResponse(response),
109+
cause: err,
110+
});
101111
case 500:
102-
return new FirebaseExtensionsError({ code: 'internal-error', message, httpResponse: err.response, cause: err });
112+
return new FirebaseExtensionsError({
113+
code: 'internal-error',
114+
message,
115+
httpResponse: toHttpResponse(response),
116+
cause: err,
117+
});
103118
}
104-
return new FirebaseExtensionsError({ code: 'unknown-error', message, httpResponse: err.response, cause: err });
119+
return new FirebaseExtensionsError({
120+
code: 'unknown-error',
121+
message,
122+
httpResponse: toHttpResponse(response),
123+
cause: err,
124+
});
105125
}
106126
}
107127

src/functions/functions-api-client-internal.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import { FirebaseApp } from '../app/firebase-app';
2020
import {
2121
HttpRequestConfig, HttpClient, RequestResponseError, AuthorizedHttpClient
2222
} from '../utils/api-request';
23-
import { PrefixedFirebaseError, ErrorInfo } from '../utils/error';
23+
import { PrefixedFirebaseError, ErrorInfo, toHttpResponse } from '../utils/error';
2424
import * as utils from '../utils/index';
2525
import * as validator from '../utils/validator';
2626
import { TaskOptions } from './functions-api';
@@ -183,7 +183,7 @@ export class FunctionsApiClient {
183183
throw new FirebaseFunctionsError({
184184
code: 'task-already-exists',
185185
message: `A task with ID ${opts?.id} already exists`,
186-
httpResponse: err.response,
186+
httpResponse: toHttpResponse(err.response),
187187
cause: err,
188188
});
189189
} else {
@@ -387,7 +387,7 @@ export class FunctionsApiClient {
387387
return new FirebaseFunctionsError({
388388
code: 'unknown-error',
389389
message: `Unexpected response with status: ${response.status} and body: ${response.text}`,
390-
httpResponse: err.response,
390+
httpResponse: toHttpResponse(response),
391391
cause: err
392392
});
393393
}
@@ -398,7 +398,7 @@ export class FunctionsApiClient {
398398
code = FUNCTIONS_ERROR_CODE_MAPPING[error.status];
399399
}
400400
const message = error.message || `Unknown server error: ${response.text}`;
401-
return new FirebaseFunctionsError({ code, message, httpResponse: err.response, cause: err });
401+
return new FirebaseFunctionsError({ code, message, httpResponse: toHttpResponse(response), cause: err });
402402
}
403403
}
404404

0 commit comments

Comments
 (0)