From 815559241608f593df3466999e95557820bbcf8f Mon Sep 17 00:00:00 2001 From: Wentao Kuang Date: Mon, 25 Sep 2023 12:01:29 +1300 Subject: [PATCH] Catch the error code while reading the config file in api --- .../src/util/__test__/config.loader.test.ts | 13 +++++++++++++ packages/lambda-tiler/src/util/config.loader.ts | 14 +++++++++----- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/packages/lambda-tiler/src/util/__test__/config.loader.test.ts b/packages/lambda-tiler/src/util/__test__/config.loader.test.ts index b86e8f070..e1ef4cdeb 100644 --- a/packages/lambda-tiler/src/util/__test__/config.loader.test.ts +++ b/packages/lambda-tiler/src/util/__test__/config.loader.test.ts @@ -113,4 +113,17 @@ o.spec('ConfigLoader', () => { o(await provider.Imagery.get('topographic')).deepEquals(await expectedConfig.Imagery.get('topographic')); }); + + const deletedLocation = 'memory://linz-basemaps/config-deleted.json'; + o('should Error 404 if config file not exists', async () => { + const error = await ConfigLoader.load( + mockUrlRequest('/v1/tiles/🦄 🌈/NZTM2000Quad/tile.json', `?config=${deletedLocation}`, Api.header), + ) + .then(() => null) + .catch((e) => e); + + o(error instanceof LambdaHttpResponse).equals(true); + o((error as LambdaHttpResponse).status).equals(404); + o((error as LambdaHttpResponse).statusDescription).equals(`Config not found at ${deletedLocation}`); + }); }); diff --git a/packages/lambda-tiler/src/util/config.loader.ts b/packages/lambda-tiler/src/util/config.loader.ts index c367c2132..0b5c87fbf 100644 --- a/packages/lambda-tiler/src/util/config.loader.ts +++ b/packages/lambda-tiler/src/util/config.loader.ts @@ -46,10 +46,14 @@ export class ConfigLoader { req.set('config', configLocation); req.timer.start('config:load'); - return CachedConfig.get(configLocation).then((f) => { - req.timer.end('config:load'); - if (f == null) throw new LambdaHttpResponse(404, `Config not found at ${configLocation}`); - return f; - }); + return CachedConfig.get(configLocation) + .then((f) => { + req.timer.end('config:load'); + if (f == null) throw new LambdaHttpResponse(404, `Config not found at ${configLocation}`); + return f; + }) + .catch((e) => { + throw new LambdaHttpResponse(e.status, e.statusDescription); + }); } }