Skip to content

Commit

Permalink
Catch the error from the fsa.readJson
Browse files Browse the repository at this point in the history
  • Loading branch information
Wentao-Kuang committed Oct 3, 2023
1 parent 8155592 commit 9c69baa
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 16 deletions.
10 changes: 5 additions & 5 deletions packages/lambda-tiler/src/util/__test__/config.loader.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down Expand Up @@ -115,15 +115,15 @@ 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),
)
.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}`);
o((error as LambdaHttpResponse).status).equals(400);
o((error as LambdaHttpResponse).statusDescription).equals(`Invalid config location at ${deletedLocation}`);
});
});
7 changes: 5 additions & 2 deletions packages/lambda-tiler/src/util/config.cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ import { SwappingLru } from './swapping.lru.js';
class LruConfig {
configProvider: Promise<ConfigProviderMemory | null>;

constructor(config: Promise<ConfigBundled>) {
constructor(config: Promise<ConfigBundled | null>) {
this.configProvider = config
.then((c) => {
if (c == null) return null;
const configProvider = ConfigProviderMemory.fromJson(c);
configProvider.createVirtualTileSets();
return configProvider;
Expand All @@ -33,7 +34,9 @@ export class ConfigCache {
get(location: string): Promise<ConfigProviderMemory | null> {
const existing = this.cache.get(location)?.configProvider;
if (existing != null) return existing;
const configJson = fsa.readJson<ConfigBundled>(location);
const configJson = fsa.readJson<ConfigBundled>(location).catch(() => {
return null;
});
const config = new LruConfig(configJson);
this.cache.set(location, config);
return config.configProvider;
Expand Down
14 changes: 5 additions & 9 deletions packages/lambda-tiler/src/util/config.loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});
}
}

0 comments on commit 9c69baa

Please sign in to comment.