Skip to content

Surface transaction errors in browser #2017

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
merged 3 commits into from
Jul 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 25 additions & 8 deletions packages/firestore/src/platform_browser/webchannel_connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ import { DatabaseId, DatabaseInfo } from '../core/database_info';
import { SDK_VERSION } from '../core/version';
import { Connection, Stream } from '../remote/connection';
import {
mapCodeFromHttpStatus,
mapCodeFromRpcStatus
mapCodeFromRpcStatus,
mapCodeFromHttpResponseErrorStatus
} from '../remote/rpc_error';
import { StreamBridge } from '../remote/stream_bridge';
import { assert, fail } from '../util/assert';
Expand Down Expand Up @@ -123,12 +123,29 @@ export class WebChannelConnection implements Connection {
xhr.getResponseText()
);
if (status > 0) {
reject(
new FirestoreError(
mapCodeFromHttpStatus(status),
'Server responded with status ' + xhr.getStatusText()
)
);
const responseError = xhr.getResponseJson().error;
if (
!!responseError &&
!!responseError.status &&
!!responseError.message
) {
const firestoreErrorCode = mapCodeFromHttpResponseErrorStatus(
responseError.status
);
reject(
new FirestoreError(
firestoreErrorCode,
responseError.message
)
);
} else {
reject(
new FirestoreError(
Code.UNKNOWN,
'Server responded with status ' + xhr.getStatus()
)
);
}
} else {
// If we received an HTTP_ERROR but there's no status code,
// it's most probably a connection issue
Expand Down
15 changes: 15 additions & 0 deletions packages/firestore/src/remote/rpc_error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -298,3 +298,18 @@ export function mapCodeFromHttpStatus(status: number): Code {
return Code.UNKNOWN;
}
}

/**
* Converts an HTTP response's error status to the equivalent error code.
*
* @param status An HTTP error response status ("FAILED_PRECONDITION",
* "UNKNOWN", etc.)
* @returns The equivalent Code. Non-matching responses are mapped to
* Code.UNKNOWN.
*/
export function mapCodeFromHttpResponseErrorStatus(status: string): Code {
const serverError = status.toLowerCase().replace('_', '-');
return Object.values(Code).indexOf(serverError as Code) >= 0
? (serverError as Code)
: Code.UNKNOWN;
}