Skip to content

Commit

Permalink
Merge branch 'main' into fix-bytes-type-buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
sofisl authored Feb 5, 2025
2 parents 8c66154 + e9b47fc commit d0171e7
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
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

0 comments on commit d0171e7

Please sign in to comment.