Skip to content

Commit

Permalink
refactor: Streamline Error Flow for metadataAccessor
Browse files Browse the repository at this point in the history
  • Loading branch information
danielbankhead committed Nov 30, 2023
1 parent 27f0a12 commit fa55752
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 34 deletions.
54 changes: 23 additions & 31 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,39 +141,31 @@ async function metadataAccessor<T>(
params = options.params || params;
}

try {
const requestMethod = fastFail ? fastFailMetadataRequest : request;
const res = await requestMethod<T>({
url: `${getBaseUrl()}/${metadataKey}`,
headers: {...HEADERS, ...headers},
retryConfig: {noResponseRetries},
params,
responseType: 'text',
timeout: requestTimeout(),
});
// NOTE: node.js converts all incoming headers to lower case.
if (res.headers[HEADER_NAME.toLowerCase()] !== HEADER_VALUE) {
throw new Error(
`Invalid response from metadata service: incorrect ${HEADER_NAME} header.`
);
} else if (!res.data) {
throw new Error('Invalid response from the metadata service');
}
if (typeof res.data === 'string') {
try {
return jsonBigint.parse(res.data);
} catch {
/* ignore */
}
}
return res.data;
} catch (e) {
const err = e as GaxiosError;
if (err.response && err.response.status !== 200) {
err.message = `Unsuccessful response status code. ${err.message}`;
const requestMethod = fastFail ? fastFailMetadataRequest : request;
const res = await requestMethod<T>({
url: `${getBaseUrl()}/${metadataKey}`,
headers: {...HEADERS, ...headers},
retryConfig: {noResponseRetries},
params,
responseType: 'text',
timeout: requestTimeout(),
});
// NOTE: node.js converts all incoming headers to lower case.
if (res.headers[HEADER_NAME.toLowerCase()] !== HEADER_VALUE) {
throw new Error(
`Invalid response from metadata service: incorrect ${HEADER_NAME} header.`
);
}

if (typeof res.data === 'string') {
try {
return jsonBigint.parse(res.data);
} catch {
/* ignore */
}
throw e;
}

return res.data;
}

async function fastFailMetadataRequest<T>(
Expand Down
17 changes: 14 additions & 3 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {SinonSandbox, createSandbox} from 'sinon';

import * as gcp from '../src';
import {GCPResidencyUtil} from './utils/gcp-residency';
import {GaxiosError} from 'gaxios';

// the metadata IP entry:
const HOST = gcp.HOST_ADDRESS;
Expand Down Expand Up @@ -191,9 +192,19 @@ describe('unit test', () => {
scope.done();
});

it('should return error if statusCode is not 200', async () => {
const scope = nock(HOST).get(`${PATH}/${TYPE}`).reply(418, {}, HEADERS);
await assert.rejects(gcp.instance(), /Unsuccessful response status code/);
it('should return the request error', async () => {
const scope = nock(HOST)
.get(`${PATH}/${TYPE}`)
.times(4)
.reply(404, undefined, HEADERS);

try {
await gcp.instance();
} catch (err) {
assert(err instanceof GaxiosError);
assert.strictEqual(err.status, 404);
}

scope.done();
});

Expand Down

0 comments on commit fa55752

Please sign in to comment.