Skip to content

Commit

Permalink
Merge pull request #282 from futurice/improve-error-descriptions
Browse files Browse the repository at this point in the history
Improve error descriptions around S3 operations and storage
  • Loading branch information
jareware authored Apr 29, 2020
2 parents 56e80f3 + cd41de6 commit af446fb
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 20 deletions.
33 changes: 20 additions & 13 deletions src/backend/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,23 +58,30 @@ export function createS3Client() {
// S3 fetch helpers

export async function s3GetJsonHelper(s3: AWS.S3, params: AWS.S3.GetObjectRequest) {
const result = await s3.getObject(params).promise();
if (!result.Body) {
throw Error(`Empty JSON in S3 object '${params.Bucket}/${params.Key}`);
try {
const result = await s3.getObject(params).promise();
if (!result.Body) {
throw Error(`Empty JSON in S3 object "${params.Bucket}/${params.Key}"`);
}
return JSON.parse(result.Body.toString('utf-8'));
} catch (err) {
throw new Error(`Couldn't get JSON from S3 object "${params.Bucket}/${params.Key}" (caused by\n${err}\n)`);
}

return JSON.parse(result.Body.toString('utf-8'));
}

export async function s3PutJsonHelper(s3: AWS.S3, params: AWS.S3.PutObjectRequest) {
return await s3
.putObject({
...params,
ContentType: 'application/json',
CacheControl: 'max-age=15',
Body: JSON.stringify(params.Body, null, 2),
})
.promise();
try {
return await s3
.putObject({
...params,
ContentType: 'application/json',
CacheControl: 'max-age=15',
Body: JSON.stringify(params.Body, null, 2),
})
.promise();
} catch (err) {
throw new Error(`Couldn't put JSON to S3 object "${params.Bucket}/${params.Key}" (caused by\n${err}\n)`);
}
}

export function createS3Sources(appConstants: AppConstants, s3Client: AWS.S3) {
Expand Down
22 changes: 16 additions & 6 deletions src/backend/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,21 @@ export async function storeResponse(
);

console.log('About to store response', r);
const Bucket = app.constants.storageBucket;
const Key = getStorageKey(r);
await app.s3Client
.putObject({
Bucket: app.constants.storageBucket,
Key: getStorageKey(r),
Bucket,
Key,
Body: JSON.stringify(r),
ACL: 'private',
})
.promise();
.promise()
.catch(err =>
Promise.reject(
new Error(`Couldn't store response to bucket "${Bucket}" under key "${Key}" (caused by\n${err}\n)`),
),
);
}

// Takes a response from the frontend, scrubs it clean, and adds fields required for storing it
Expand Down Expand Up @@ -104,9 +111,12 @@ export function hash(input: string, pepper: string) {
}

export async function obfuscateLowPopulationPostalCode(app: App, postalCode: string) {
const lowPopulationPostalCodes = await app.s3Sources.fetchLowPopulationPostalCodes();

return lowPopulationPostalCodes?.data?.[postalCode] || postalCode;
try {
const lowPopulationPostalCodes = await app.s3Sources.fetchLowPopulationPostalCodes();
return lowPopulationPostalCodes?.data?.[postalCode] || postalCode;
} catch (err) {
throw new Error(`Couldn't obfuscate low population postal code "${postalCode}" (caused by\n${err}\n)`);
}
}

export async function exportOpenData(app: App) {
Expand Down
2 changes: 1 addition & 1 deletion src/index-backend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ if (process.argv[0].match(/\/ts-node$/)) {

const response = (statusCode: number, body?: object, logError?: Error) => {
console.log(`Outgoing response: ${statusCode}`);
if (logError) console.error('ERROR (API)', logError);
if (logError) console.error(`API request processing failed (caused by\n${logError}\n)`);
return {
statusCode,
body: body ? JSON.stringify(body, null, 2) : '',
Expand Down

0 comments on commit af446fb

Please sign in to comment.