-
Notifications
You must be signed in to change notification settings - Fork 926
Ensure errors are wrapped in FirestoreError in DatastoreImpl methods #4788
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
dconeybe
merged 10 commits into
master
from
dconeybe/EnsureFirestoreErrorThrownInDatabaseTs
Apr 19, 2021
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
29f2a7d
datastore.ts: wrap non-FirestoreError errors in FirestoreError
dconeybe e1e81e4
Add unit tests for datastore.ts
dconeybe 8788522
Format code
dconeybe 5634510
Add a changeset
dconeybe 18a9e2e
datastore.test.ts: fix copyright year
dconeybe eb0a96f
datastore.test.ts: fix lint errors
dconeybe d9e3b6a
Merge remote-tracking branch 'origin/master' into EnsureFirestoreErro…
dconeybe b8f4e37
datastore.test.ts: Add b/185584343 to the TODO
dconeybe 5ccc34d
datastore.test.ts: also verify that the thrown exception's name is Fi…
dconeybe 7bbbfe5
Merge remote-tracking branch 'origin/master' into EnsureFirestoreErro…
dconeybe File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@firebase/firestore': patch | ||
--- | ||
|
||
Ensure that errors get wrapped in FirestoreError |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,221 @@ | ||
/** | ||
* @license | ||
* Copyright 2021 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import { expect, use } from 'chai'; | ||
import * as chaiAsPromised from 'chai-as-promised'; | ||
|
||
import { EmptyCredentialsProvider, Token } from '../../../src/api/credentials'; | ||
import { DatabaseId } from '../../../src/core/database_info'; | ||
import { Connection, Stream } from '../../../src/remote/connection'; | ||
import { | ||
Datastore, | ||
newDatastore, | ||
invokeCommitRpc, | ||
invokeBatchGetDocumentsRpc | ||
} from '../../../src/remote/datastore'; | ||
import { JsonProtoSerializer } from '../../../src/remote/serializer'; | ||
import { Code, FirestoreError } from '../../../src/util/error'; | ||
|
||
use(chaiAsPromised); | ||
|
||
// TODO(b/185584343): Improve the coverage of these tests. | ||
// At the time of writing, the tests only cover the error handling in | ||
// `invokeRPC()` and `invokeStreamingRPC()`. | ||
describe('Datastore', () => { | ||
class MockConnection implements Connection { | ||
invokeRPC<Req, Resp>( | ||
rpcName: string, | ||
path: string, | ||
request: Req, | ||
token: Token | null | ||
): Promise<Resp> { | ||
throw new Error('MockConnection.invokeRPC() must be replaced'); | ||
} | ||
|
||
invokeStreamingRPC<Req, Resp>( | ||
rpcName: string, | ||
path: string, | ||
request: Req, | ||
token: Token | null | ||
): Promise<Resp[]> { | ||
throw new Error('MockConnection.invokeStreamingRPC() must be replaced'); | ||
} | ||
|
||
openStream<Req, Resp>( | ||
rpcName: string, | ||
token: Token | null | ||
): Stream<Req, Resp> { | ||
throw new Error('MockConnection.openStream() must be replaced'); | ||
} | ||
} | ||
|
||
class MockCredentialsProvider extends EmptyCredentialsProvider { | ||
invalidateTokenInvoked = false; | ||
invalidateToken(): void { | ||
this.invalidateTokenInvoked = true; | ||
} | ||
} | ||
|
||
const serializer = new JsonProtoSerializer( | ||
new DatabaseId('test-project'), | ||
/* useProto3Json= */ false | ||
); | ||
|
||
async function invokeDatastoreImplInvokeRpc( | ||
datastore: Datastore | ||
): Promise<void> { | ||
// Since we cannot access the `DatastoreImpl` class directly, invoke its | ||
// `invokeRPC()` method indirectly via `invokeCommitRpc()`. | ||
await invokeCommitRpc(datastore, /* mutations= */ []); | ||
} | ||
|
||
async function invokeDatastoreImplInvokeStreamingRPC( | ||
datastore: Datastore | ||
): Promise<void> { | ||
// Since we cannot access the `DatastoreImpl` class directly, invoke its | ||
// `invokeStreamingRPC()` method indirectly via | ||
// `invokeBatchGetDocumentsRpc()`. | ||
await invokeBatchGetDocumentsRpc(datastore, /* keys= */ []); | ||
} | ||
|
||
it('newDatastore() returns an an instance of Datastore', () => { | ||
const datastore = newDatastore( | ||
new EmptyCredentialsProvider(), | ||
new MockConnection(), | ||
serializer | ||
); | ||
expect(datastore).to.be.an.instanceof(Datastore); | ||
}); | ||
|
||
it('DatastoreImpl.invokeRPC() fails if terminated', async () => { | ||
const datastore = newDatastore( | ||
new EmptyCredentialsProvider(), | ||
new MockConnection(), | ||
serializer | ||
); | ||
datastore.terminate(); | ||
await expect(invokeDatastoreImplInvokeRpc(datastore)) | ||
.to.eventually.be.rejectedWith(/terminated/) | ||
.and.include({ | ||
'name': 'FirebaseError', | ||
'code': Code.FAILED_PRECONDITION | ||
}); | ||
}); | ||
|
||
it('DatastoreImpl.invokeRPC() rethrows a FirestoreError', async () => { | ||
const connection = new MockConnection(); | ||
connection.invokeRPC = () => | ||
Promise.reject(new FirestoreError(Code.ABORTED, 'zzyzx')); | ||
const credentials = new MockCredentialsProvider(); | ||
const datastore = newDatastore(credentials, connection, serializer); | ||
await expect(invokeDatastoreImplInvokeRpc(datastore)) | ||
.to.eventually.be.rejectedWith('zzyzx') | ||
.and.include({ | ||
'name': 'FirebaseError', | ||
'code': Code.ABORTED | ||
}); | ||
expect(credentials.invalidateTokenInvoked).to.be.false; | ||
}); | ||
|
||
it('DatastoreImpl.invokeRPC() wraps unknown exceptions in a FirestoreError', async () => { | ||
const connection = new MockConnection(); | ||
connection.invokeRPC = () => Promise.reject('zzyzx'); | ||
const credentials = new MockCredentialsProvider(); | ||
const datastore = newDatastore(credentials, connection, serializer); | ||
await expect(invokeDatastoreImplInvokeRpc(datastore)) | ||
.to.eventually.be.rejectedWith('zzyzx') | ||
.and.include({ | ||
'name': 'FirebaseError', | ||
'code': Code.UNKNOWN | ||
}); | ||
expect(credentials.invalidateTokenInvoked).to.be.false; | ||
}); | ||
|
||
it('DatastoreImpl.invokeRPC() invalidates the token if unauthenticated', async () => { | ||
const connection = new MockConnection(); | ||
connection.invokeRPC = () => | ||
Promise.reject(new FirestoreError(Code.UNAUTHENTICATED, 'zzyzx')); | ||
const credentials = new MockCredentialsProvider(); | ||
const datastore = newDatastore(credentials, connection, serializer); | ||
await expect(invokeDatastoreImplInvokeRpc(datastore)) | ||
.to.eventually.be.rejectedWith('zzyzx') | ||
.and.include({ | ||
'name': 'FirebaseError', | ||
'code': Code.UNAUTHENTICATED | ||
}); | ||
expect(credentials.invalidateTokenInvoked).to.be.true; | ||
}); | ||
|
||
it('DatastoreImpl.invokeStreamingRPC() fails if terminated', async () => { | ||
const datastore = newDatastore( | ||
new EmptyCredentialsProvider(), | ||
new MockConnection(), | ||
serializer | ||
); | ||
datastore.terminate(); | ||
await expect(invokeDatastoreImplInvokeStreamingRPC(datastore)) | ||
.to.eventually.be.rejectedWith(/terminated/) | ||
.and.include({ | ||
'name': 'FirebaseError', | ||
'code': Code.FAILED_PRECONDITION | ||
}); | ||
}); | ||
|
||
it('DatastoreImpl.invokeStreamingRPC() rethrows a FirestoreError', async () => { | ||
const connection = new MockConnection(); | ||
connection.invokeStreamingRPC = () => | ||
Promise.reject(new FirestoreError(Code.ABORTED, 'zzyzx')); | ||
const credentials = new MockCredentialsProvider(); | ||
const datastore = newDatastore(credentials, connection, serializer); | ||
await expect(invokeDatastoreImplInvokeStreamingRPC(datastore)) | ||
.to.eventually.be.rejectedWith('zzyzx') | ||
.and.include({ | ||
'name': 'FirebaseError', | ||
'code': Code.ABORTED | ||
}); | ||
expect(credentials.invalidateTokenInvoked).to.be.false; | ||
}); | ||
|
||
it('DatastoreImpl.invokeStreamingRPC() wraps unknown exceptions in a FirestoreError', async () => { | ||
const connection = new MockConnection(); | ||
connection.invokeStreamingRPC = () => Promise.reject('zzyzx'); | ||
const credentials = new MockCredentialsProvider(); | ||
const datastore = newDatastore(credentials, connection, serializer); | ||
await expect(invokeDatastoreImplInvokeStreamingRPC(datastore)) | ||
.to.eventually.be.rejectedWith('zzyzx') | ||
.and.include({ | ||
'name': 'FirebaseError', | ||
'code': Code.UNKNOWN | ||
}); | ||
expect(credentials.invalidateTokenInvoked).to.be.false; | ||
}); | ||
|
||
it('DatastoreImpl.invokeStreamingRPC() invalidates the token if unauthenticated', async () => { | ||
const connection = new MockConnection(); | ||
connection.invokeStreamingRPC = () => | ||
Promise.reject(new FirestoreError(Code.UNAUTHENTICATED, 'zzyzx')); | ||
const credentials = new MockCredentialsProvider(); | ||
const datastore = newDatastore(credentials, connection, serializer); | ||
await expect(invokeDatastoreImplInvokeStreamingRPC(datastore)) | ||
.to.eventually.be.rejectedWith('zzyzx') | ||
.and.include({ | ||
'name': 'FirebaseError', | ||
'code': Code.UNAUTHENTICATED | ||
}); | ||
expect(credentials.invalidateTokenInvoked).to.be.true; | ||
}); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn't seem to verify that we return a FirestoreError. I think you can do this as such:
If that doesn't work, you can use a try/catch here.
This also applies to other tests such as"DatastoreImpl.invokeRPC() wraps unknown exceptions in a FirestoreError". Since GRPC errors also have codes, I suspect that these tests may have passed even without your change.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done. Unfortunately, the "nice" way of using
rejectedWith
as you suggest doesn't work. The check incorrectly fails with this message:(note: both the actual and expected strings in the message are equal)
But I found that you can verify multiple properties at once. So I used that to verify that the
name
isFirebaseError
in addition to verifying the code.