@@ -8,6 +8,7 @@ import {EarthEngineDataset} from "./geeAssets/earth-engine-dataset";
8
8
import { TileRequestDTO , Tilesets } from "./tile-request.dto" ;
9
9
import { default as fetch , Response as FetchResponse } from "node-fetch" ;
10
10
import { pipeline } from "stream/promises" ;
11
+ import * as crypto from "crypto" ;
11
12
12
13
//Asset Mapping
13
14
const assets : Record < Tilesets , EarthEngineDataset > = {
@@ -22,6 +23,8 @@ exports.eetApp = app;
22
23
23
24
24
25
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
+
25
28
///// This block handles CORS
26
29
res . set ( 'Access-Control-Allow-Origin' , '*' ) ;
27
30
if ( req . method === 'OPTIONS' ) {
@@ -48,19 +51,18 @@ router.get('/:z/:x/:y', async (req: Request, res: Response) : Promise<void> => {
48
51
49
52
asset . isYearValid ( tileRequestDTO . year ) ; //Year might be required or not depending on the asset
50
53
} catch ( errors ) {
51
- sendErrorResponse ( res , 400 , errors )
54
+ sendErrorResponse ( res , logId , 400 , errors )
52
55
return ;
53
56
}
54
57
55
58
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' } ` )
57
60
58
61
try {
59
62
await EarthEngineUtils . authenticate ( ) ;
60
63
61
64
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 } ` )
64
66
//TODO CACHING
65
67
// The calculations when requesting the tile URL take the longest, images are not probably going to be very big (not even MBs)
66
68
// 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> => {
71
73
const contentType = imageResponse . headers . get ( 'content-type' ) ;
72
74
73
75
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 } ` )
75
78
}
76
79
77
80
res . status ( 200 ) . contentType ( contentType ) ;
78
81
await pipeline ( imageResponse . body , res ) ;
79
82
} catch ( error ) {
80
- sendErrorResponse ( res , 500 , error ) ;
83
+ sendErrorResponse ( res , logId , 500 , error ) ;
81
84
}
82
85
} ) ;
83
86
@@ -96,7 +99,7 @@ async function getAndValidateRequestDTO(req: Request): Promise<TileRequestDTO> {
96
99
return result ;
97
100
}
98
101
99
- function sendErrorResponse ( res : Response , status : number , errors : any ) {
102
+ function sendErrorResponse ( res : Response , logId : string , status : number , errors : any ) {
100
103
// Using class validator's validateOrReject, rejects by throwing a list of ValidationErrors, but native Errors are thrown
101
104
// as a single non-array Error object, so this check must be done first to process the errors as a list later
102
105
errors = errors . length ? errors : [ errors ] ;
@@ -109,7 +112,7 @@ function sendErrorResponse(res: Response, status: number, errors: any){
109
112
error instanceof ValidationError ?
110
113
error : { message : error . message }
111
114
)
112
- console . log ( ' Returning Errors: ' + JSON . stringify ( responseErrors ) )
115
+ console . log ( ` ${ logId } - Returning Errors: ${ JSON . stringify ( responseErrors ) } ` )
113
116
114
117
res . status ( status ) . json ( { errors : responseErrors } )
115
118
}
0 commit comments