From 9c69baafe0e5246947c8f974d6453aaeb112e5c1 Mon Sep 17 00:00:00 2001 From: Wentao Kuang Date: Wed, 4 Oct 2023 09:26:00 +1300 Subject: [PATCH] Catch the error from the fsa.readJson --- .../src/util/__test__/config.loader.test.ts | 10 +++++----- packages/lambda-tiler/src/util/config.cache.ts | 7 +++++-- packages/lambda-tiler/src/util/config.loader.ts | 14 +++++--------- 3 files changed, 15 insertions(+), 16 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 e1ef4cdeb..50abce077 100644 --- a/packages/lambda-tiler/src/util/__test__/config.loader.test.ts +++ b/packages/lambda-tiler/src/util/__test__/config.loader.test.ts @@ -82,8 +82,8 @@ o.spec('ConfigLoader', () => { .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 ${location}`); + o((error as LambdaHttpResponse).status).equals(400); + o((error as LambdaHttpResponse).statusDescription).equals(`Invalid config location at ${location}`); }); o('should get expected config file', async () => { @@ -115,7 +115,7 @@ o.spec('ConfigLoader', () => { }); const deletedLocation = 'memory://linz-basemaps/config-deleted.json'; - o('should Error 404 if config file not exists', async () => { + o('should Error 400 if config file not exists', async () => { const error = await ConfigLoader.load( mockUrlRequest('/v1/tiles/🦄 🌈/NZTM2000Quad/tile.json', `?config=${deletedLocation}`, Api.header), ) @@ -123,7 +123,7 @@ o.spec('ConfigLoader', () => { .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}`); + o((error as LambdaHttpResponse).status).equals(400); + o((error as LambdaHttpResponse).statusDescription).equals(`Invalid config location at ${deletedLocation}`); }); }); diff --git a/packages/lambda-tiler/src/util/config.cache.ts b/packages/lambda-tiler/src/util/config.cache.ts index 2b308c1bb..b03b9004c 100644 --- a/packages/lambda-tiler/src/util/config.cache.ts +++ b/packages/lambda-tiler/src/util/config.cache.ts @@ -5,9 +5,10 @@ import { SwappingLru } from './swapping.lru.js'; class LruConfig { configProvider: Promise; - constructor(config: Promise) { + constructor(config: Promise) { this.configProvider = config .then((c) => { + if (c == null) return null; const configProvider = ConfigProviderMemory.fromJson(c); configProvider.createVirtualTileSets(); return configProvider; @@ -33,7 +34,9 @@ export class ConfigCache { get(location: string): Promise { const existing = this.cache.get(location)?.configProvider; if (existing != null) return existing; - const configJson = fsa.readJson(location); + const configJson = fsa.readJson(location).catch(() => { + return null; + }); const config = new LruConfig(configJson); this.cache.set(location, config); return config.configProvider; diff --git a/packages/lambda-tiler/src/util/config.loader.ts b/packages/lambda-tiler/src/util/config.loader.ts index 0b5c87fbf..117fb950c 100644 --- a/packages/lambda-tiler/src/util/config.loader.ts +++ b/packages/lambda-tiler/src/util/config.loader.ts @@ -46,14 +46,10 @@ 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; - }) - .catch((e) => { - throw new LambdaHttpResponse(e.status, e.statusDescription); - }); + return CachedConfig.get(configLocation).then((f) => { + req.timer.end('config:load'); + if (f == null) throw new LambdaHttpResponse(400, `Invalid config location at ${configLocation}`); + return f; + }); } }