Skip to content
This repository has been archived by the owner on Jun 4, 2024. It is now read-only.

Commit

Permalink
Merge branch 'dev' into rsa_upgrade_gcs_lib_version
Browse files Browse the repository at this point in the history
  • Loading branch information
rsasch committed Apr 7, 2020
2 parents 53c2f3b + d6c6b6c commit 6721309
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 7 deletions.
25 changes: 24 additions & 1 deletion common/api_adapter.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const request = require('superagent');
const {Response} = require('../common/helpers');

const MAX_RETRY_ATTEMPTS = 5;
const INITIAL_BACKOFF_DELAY = 1000;
Expand Down Expand Up @@ -32,10 +33,32 @@ async function getHeaders(url, authorization) {
async function getJsonFrom(url, authorization, retryAttempt = 1, delay = INITIAL_BACKOFF_DELAY) {
try {
const {body} = await get('get', url, authorization);
return body;

/*
handle the case when Martha receives empty JSON body. The reason behind forming a response with status 500
and throwing it in `try` is so that it can be caught locally and be retried.
*/
if (Object.keys(body).length === 0) {
console.log(`Received an empty JSON body while trying to resolve url '${url}'. Attempt ${retryAttempt}. ` +
'Creating a response with status 500.');

const errorMsg = `Something went wrong while trying to resolve url '${url}'. It came back with empty JSON body!`;
const errorResponseObj = {
response: {
status: 500,
text: errorMsg
}
};
throw new Response(500, errorResponseObj);
}
else {
console.log(`Successfully received response from url '${url}'.`);
return body;
}
} catch (error) {
// TODO: capture error on lines 56 and 58 in order to give a more detailed idea of
// what went wrong where (see https://broadworkbench.atlassian.net/browse/WA-13)
console.log(`Received error for url '${url}'. Attempt ${retryAttempt}.`);
console.error(error);

if((error.status >= SERVER_ERROR_CODE && error.status <= NETWORK_AUTH_REQ_CODE) ||
Expand Down
2 changes: 1 addition & 1 deletion common/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ function dataObjectUriToHttps(dataObjectUri) {
};

const output = url.format(resolutionUrlParts);
console.log(`${dataObjectUri} -> ${output}`);
console.log(`Converting DRS URI to HTTPS: ${dataObjectUri} -> ${output}`);
return output;
}

Expand Down
15 changes: 10 additions & 5 deletions martha_v2/martha_v2.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,20 @@ function parseRequest(req) {
}
}

function maybeTalkToBond(req, provider = BondProviders.default) {
let myPromise;
async function maybeTalkToBond(req, provider = BondProviders.default) {
// Currently HCA data access does not require additional credentials.
// The HCA checkout buckets allow object read access for [email protected].
if (req && req.headers && req.headers.authorization && provider !== BondProviders.HCA) {
myPromise = apiAdapter.getJsonFrom(`${bondBaseUrl()}/api/link/v1/${provider}/serviceaccount/key`, req.headers.authorization);
try {
return await apiAdapter.getJsonFrom(`${bondBaseUrl()}/api/link/v1/${provider}/serviceaccount/key`, req.headers.authorization);
} catch (error) {
console.log(`Received error while fetching service account from Bond for provider '${provider}'.`);
console.error(error);
throw error;
}
} else {
myPromise = Promise.resolve();
return Promise.resolve();
}
return myPromise;
}

function aggregateResponses(responses) {
Expand Down Expand Up @@ -59,6 +63,7 @@ function martha_v2_handler(req, res) {
res.status(200).send(aggregateResponses(rawResults));
})
.catch((err) => {
console.log('Received error while either contacting Bond or resolving drs url.');
console.error(err);
res.status(502).send(err);
});
Expand Down

0 comments on commit 6721309

Please sign in to comment.