Skip to content

Commit

Permalink
fix(lambda-tiler): Catch the error code while reading the config file…
Browse files Browse the repository at this point in the history
… in api. BM-898 (#2969)

#### Description
When failed to get config file from s3, return correct error code in
logs and remove the 500 error in #proj-basemaps-alerts


#### Intention
Catch the error from `fsa.readJson(config)`, and throw correct error
code and message.



#### Checklist
- [X] Tests updated
- [ ] Docs updated
- [X] Issue linked in Title
  • Loading branch information
Wentao-Kuang authored Oct 3, 2023
1 parent 9df4eaa commit c2f3132
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 5 deletions.
17 changes: 15 additions & 2 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 @@ -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 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(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
2 changes: 1 addition & 1 deletion packages/lambda-tiler/src/util/config.loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export class ConfigLoader {
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}`);
if (f == null) throw new LambdaHttpResponse(400, `Invalid config location at ${configLocation}`);
return f;
});
}
Expand Down

0 comments on commit c2f3132

Please sign in to comment.