From 2850716452f0d8580a5f64fc472ce0eda0c3f364 Mon Sep 17 00:00:00 2001 From: Ricky Lam Date: Wed, 15 Mar 2023 00:07:06 -0700 Subject: [PATCH 1/3] Change _fail to use NETWORK_REQUEST_FAILED --- packages/auth/src/api/index.test.ts | 38 ++++++++++++++--------------- packages/auth/src/api/index.ts | 5 +++- 2 files changed, 22 insertions(+), 21 deletions(-) diff --git a/packages/auth/src/api/index.test.ts b/packages/auth/src/api/index.test.ts index 579f6c5e8ad..74da561f273 100644 --- a/packages/auth/src/api/index.test.ts +++ b/packages/auth/src/api/index.test.ts @@ -308,26 +308,6 @@ describe('api/_performApiRequest', () => { }); }); - context('with non-Firebase Errors', () => { - afterEach(mockFetch.tearDown); - - it('should handle non-FirebaseErrors', async () => { - mockFetch.setUpWithOverride(() => { - return new Promise((_, reject) => reject(new Error('error'))); - }); - const promise = _performApiRequest( - auth, - HttpMethod.POST, - Endpoint.SIGN_UP, - request - ); - await expect(promise).to.be.rejectedWith( - FirebaseError, - 'auth/internal-error' - ); - }); - }); - context('with network issues', () => { afterEach(mockFetch.tearDown); @@ -365,6 +345,24 @@ describe('api/_performApiRequest', () => { expect(clock.clearTimeout).to.have.been.called; clock.restore(); }); + + it('should handle network failure', async () => { + mockFetch.setUpWithOverride(() => { + return new Promise((_, reject) => + reject(new Error('network error')) + ); + }); + const promise = _performApiRequest( + auth, + HttpMethod.POST, + Endpoint.SIGN_UP, + request + ); + await expect(promise).to.be.rejectedWith( + FirebaseError, + 'auth/network-request-failed' + ); + }); }); context('edgcase error mapping', () => { diff --git a/packages/auth/src/api/index.ts b/packages/auth/src/api/index.ts index 0194c43d9a5..900125ecb7a 100644 --- a/packages/auth/src/api/index.ts +++ b/packages/auth/src/api/index.ts @@ -181,7 +181,10 @@ export async function _performFetchWithErrorHandling( if (e instanceof FirebaseError) { throw e; } - _fail(auth, AuthErrorCode.INTERNAL_ERROR, { 'message': String(e) }); + // Changing this to a different error code will log user out when there is a network error + // because we treat any error other than NETWORK_REQUEST_FAILED as token is invalid. + // https://github.com/firebase/firebase-js-sdk/blob/master/packages/auth/src/core/auth/auth_impl.ts#L309-L316 + _fail(auth, AuthErrorCode.NETWORK_REQUEST_FAILED, { 'message': String(e) }); } } From 3813804dc153a7a9673205c5ec085f4c1167c190 Mon Sep 17 00:00:00 2001 From: Ricky Lam Date: Wed, 15 Mar 2023 00:23:29 -0700 Subject: [PATCH 2/3] Add changeset --- .changeset/sixty-buckets-repeat.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/sixty-buckets-repeat.md diff --git a/.changeset/sixty-buckets-repeat.md b/.changeset/sixty-buckets-repeat.md new file mode 100644 index 00000000000..40591209839 --- /dev/null +++ b/.changeset/sixty-buckets-repeat.md @@ -0,0 +1,5 @@ +--- +'@firebase/auth': patch +--- + +Modify \_fail to use AuthErrorCode.NETWORK_REQUEST_FAILED From f3810d8cbc38bcaa948b64c3f9ec938cffc0688c Mon Sep 17 00:00:00 2001 From: Ricky Lam Date: Wed, 15 Mar 2023 13:53:33 -0700 Subject: [PATCH 3/3] Check if network error is propagated in error message --- packages/auth/src/api/index.test.ts | 10 ++++++---- packages/auth/src/api/index.ts | 2 +- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/packages/auth/src/api/index.test.ts b/packages/auth/src/api/index.test.ts index 74da561f273..af3b61610d2 100644 --- a/packages/auth/src/api/index.test.ts +++ b/packages/auth/src/api/index.test.ts @@ -358,10 +358,12 @@ describe('api/_performApiRequest', () => { Endpoint.SIGN_UP, request ); - await expect(promise).to.be.rejectedWith( - FirebaseError, - 'auth/network-request-failed' - ); + await expect(promise) + .to.be.rejectedWith(FirebaseError, 'auth/network-request-failed') + .eventually.with.nested.property( + 'customData.message', + 'Error: network error' + ); }); }); diff --git a/packages/auth/src/api/index.ts b/packages/auth/src/api/index.ts index 900125ecb7a..6c9d775d243 100644 --- a/packages/auth/src/api/index.ts +++ b/packages/auth/src/api/index.ts @@ -183,7 +183,7 @@ export async function _performFetchWithErrorHandling( } // Changing this to a different error code will log user out when there is a network error // because we treat any error other than NETWORK_REQUEST_FAILED as token is invalid. - // https://github.com/firebase/firebase-js-sdk/blob/master/packages/auth/src/core/auth/auth_impl.ts#L309-L316 + // https://github.com/firebase/firebase-js-sdk/blob/4fbc73610d70be4e0852e7de63a39cb7897e8546/packages/auth/src/core/auth/auth_impl.ts#L309-L316 _fail(auth, AuthErrorCode.NETWORK_REQUEST_FAILED, { 'message': String(e) }); } }