Skip to content

Commit 7decfd4

Browse files
committed
chore(CloudFunction): Improves logs and image download error handling
1 parent 7a48edb commit 7decfd4

File tree

1 file changed

+11
-8
lines changed
  • cloud_functions/earth_engine_tiler/src

1 file changed

+11
-8
lines changed

cloud_functions/earth_engine_tiler/src/index.ts

+11-8
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {EarthEngineDataset} from "./geeAssets/earth-engine-dataset";
88
import {TileRequestDTO, Tilesets} from "./tile-request.dto";
99
import {default as fetch , Response as FetchResponse} from "node-fetch";
1010
import {pipeline} from "stream/promises";
11+
import * as crypto from "crypto";
1112

1213
//Asset Mapping
1314
const assets: Record<Tilesets, EarthEngineDataset> = {
@@ -22,6 +23,8 @@ exports.eetApp = app;
2223

2324

2425
router.get('/:z/:x/:y', async (req: Request, res: Response) : Promise<void> => {
26+
const logId = crypto.createHash('sha1').update(performance.now().toString()).digest('hex');
27+
2528
///// This block handles CORS
2629
res.set('Access-Control-Allow-Origin', '*');
2730
if (req.method === 'OPTIONS') {
@@ -48,19 +51,18 @@ router.get('/:z/:x/:y', async (req: Request, res: Response) : Promise<void> => {
4851

4952
asset.isYearValid(tileRequestDTO.year); //Year might be required or not depending on the asset
5053
} catch (errors) {
51-
sendErrorResponse(res, 400, errors)
54+
sendErrorResponse(res, logId, 400, errors)
5255
return;
5356
}
5457

5558
const { tileset, x, y, z, year } = tileRequestDTO;
56-
console.log(`Requesting tile for ${tileset} with coordinates ${x}-${y}-${z} and year ${year || 'N/A'}`)
59+
console.log(`${logId} - Requesting tile for ${tileset} with coordinates ${x}-${y}-${z} and year ${year || 'N/A'}`)
5760

5861
try {
5962
await EarthEngineUtils.authenticate();
6063

6164
const tileURL = await asset.getMapUrl(z, x, y, year);
62-
console.log(`Obtained tile URL on ${tileURL}`)
63-
65+
console.log(`${logId} - Obtained tile URL on ${tileURL}`)
6466
//TODO CACHING
6567
// The calculations when requesting the tile URL take the longest, images are not probably going to be very big (not even MBs)
6668
// create new schema on Strapi DB instance, can't use same schema without workarounds, redis on memorystore might be overkill
@@ -71,13 +73,14 @@ router.get('/:z/:x/:y', async (req: Request, res: Response) : Promise<void> => {
7173
const contentType = imageResponse.headers.get('content-type');
7274

7375
if(!imageResponse.ok || !contentType || !imageResponse.body){
74-
throw new Error (`A problem ocurred retrieving the tile on ${tileURL}`)
76+
const errorResponse = imageResponse.body ? JSON.stringify(imageResponse.body) : 'N/A';
77+
throw new Error (`A problem ocurred retrieving the tile on ${tileURL}. Status: ${imageResponse.status} - Error Response: ${errorResponse}`)
7578
}
7679

7780
res.status(200).contentType(contentType);
7881
await pipeline(imageResponse.body, res);
7982
} catch (error) {
80-
sendErrorResponse(res, 500, error);
83+
sendErrorResponse(res, logId, 500, error);
8184
}
8285
});
8386

@@ -96,7 +99,7 @@ async function getAndValidateRequestDTO(req: Request): Promise<TileRequestDTO> {
9699
return result;
97100
}
98101

99-
function sendErrorResponse(res: Response, status: number, errors: any){
102+
function sendErrorResponse(res: Response, logId: string, status: number, errors: any){
100103
// Using class validator's validateOrReject, rejects by throwing a list of ValidationErrors, but native Errors are thrown
101104
// as a single non-array Error object, so this check must be done first to process the errors as a list later
102105
errors = errors.length ? errors : [errors];
@@ -109,7 +112,7 @@ function sendErrorResponse(res: Response, status: number, errors: any){
109112
error instanceof ValidationError ?
110113
error : { message: error.message }
111114
)
112-
console.log('Returning Errors: ' + JSON.stringify(responseErrors))
115+
console.log(`${logId} - Returning Errors: ${JSON.stringify(responseErrors)}`)
113116

114117
res.status( status ).json( { errors: responseErrors } )
115118
}

0 commit comments

Comments
 (0)