Skip to content
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

fix: parse from HTTP errors and prefer that as the cannonical error code when it is provided #1633

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
10 changes: 8 additions & 2 deletions gax/src/googleError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,14 @@ export class GoogleError extends Error {
new GoogleError(json['error']['message']),
proto3Error
);
// Map Http Status Code to gRPC Status Code
if (json['error']['code']) {
// Get gRPC Status Code
if (
json['error']['status'] &&
Status[json['error']['status'] as keyof typeof Status]
) {
error.code = Status[json['error']['status'] as keyof typeof Status];
} else if (json['error']['code']) {
// Map Http Status Code to gRPC Status Code
error.code = rpcCodeFromHttpStatusCode(json['error']['code']);
} else {
// If error code is absent, proto3 message default value is 0. We should
Expand Down
28 changes: 25 additions & 3 deletions gax/test/unit/googleError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import * as protobuf from 'protobufjs';
import * as path from 'path';
import {GoogleError, GoogleErrorDecoder} from '../../src/googleError';
import {Metadata} from '@grpc/grpc-js';
import {rpcCodeFromHttpStatusCode} from '../../src/status';
import {rpcCodeFromHttpStatusCode, Status} from '../../src/status';

interface MyObj {
type: string;
Expand Down Expand Up @@ -291,17 +291,39 @@ describe('map http status code to gRPC status code', () => {
assert.deepStrictEqual(error.code, rpcCodeFromHttpStatusCode(403));
});

it('error without http status code', () => {
it('error without http error code or status', () => {
const json = {
error: {
message:
'Cloud Translation API has not been used in project 123 before or it is disabled. Enable it by visiting https://console.developers.google.com/apis/api/translate.googleapis.com/overview?project=455411330361 then retry. If you enabled this API recently, wait a few minutes for the action to propagate to our systems and retry.',
status: 'PERMISSION_DENIED',
},
};
const error = GoogleError.parseHttpError(json);
assert.deepStrictEqual(error.code, undefined);
});

it('prefers http error status over error code', () => {
const errorJsonAborted = {
error: {
code: 409,
message: 'This is a placeholder message.',
status: 'ABORTED',
},
};
const errorAborted = GoogleError.parseHttpError(errorJsonAborted);
const errorJsonAlreadyExistss = {
error: {
code: 409,
message: 'This is a placeholder message.',
status: 'ALREADY_EXISTS',
},
};
const errorAlreadyExists = GoogleError.parseHttpError(
errorJsonAlreadyExistss
);
assert.deepStrictEqual(errorAborted.code, Status.ABORTED);
assert.deepStrictEqual(errorAlreadyExists.code, Status.ALREADY_EXISTS);
});
});

describe('http error decoding', () => {
Expand Down
Loading