From cd41de691a83d592fd3da6a5eea6498134748b62 Mon Sep 17 00:00:00 2001 From: Jarno Rantanen Date: Wed, 29 Apr 2020 12:49:47 +0300 Subject: [PATCH] Improve error descriptions around S3 operations and storage. --- src/backend/app.ts | 33 ++++++++++++++++++++------------- src/backend/main.ts | 22 ++++++++++++++++------ src/index-backend.ts | 2 +- 3 files changed, 37 insertions(+), 20 deletions(-) diff --git a/src/backend/app.ts b/src/backend/app.ts index 44ca2c7d..1ab0d742 100644 --- a/src/backend/app.ts +++ b/src/backend/app.ts @@ -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) { diff --git a/src/backend/main.ts b/src/backend/main.ts index 0dc25ef9..8c00926f 100644 --- a/src/backend/main.ts +++ b/src/backend/main.ts @@ -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 @@ -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) { diff --git a/src/index-backend.ts b/src/index-backend.ts index 1f0c8275..43737c05 100644 --- a/src/index-backend.ts +++ b/src/index-backend.ts @@ -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) : '',