Skip to content

Commit e2d75d7

Browse files
authored
fix(tileindex-validate): log errors when validating presets fail (#1180)
#### Motivation When validation fails the errors are logged as a `Error` object, by default the logger does not seralize classes so it gets logged as `"reason": {}` which is very unhelpful to know why something failed #### Modification When failing a validation check, log out the reason as a string, the file that failed and the preset that was being checked at the time. #### Verification Added unit tests for the preset validation
1 parent 4beca25 commit e2d75d7

File tree

2 files changed

+28
-10
lines changed

2 files changed

+28
-10
lines changed

src/commands/tileindex-validate/__test__/tileindex.validate.test.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { FsMemory } from '@chunkd/source-memory';
77
import { BBox } from '@linzjs/geojson';
88
import { FeatureCollection } from 'geojson';
99

10+
import { logger } from '../../../log.js';
1011
import { MapSheetData } from '../../../utils/__test__/mapsheet.data.js';
1112
import { GridSize, MapSheet } from '../../../utils/mapsheet.js';
1213
import { createTiff } from '../../common.js';
@@ -19,6 +20,7 @@ import {
1920
reprojectIfNeeded,
2021
TiffLoader,
2122
validate8BitsTiff,
23+
validatePreset,
2224
} from '../tileindex.validate.js';
2325
import { FakeCogTiff } from './tileindex.validate.data.js';
2426

@@ -329,6 +331,23 @@ describe('is8BitsTiff', () => {
329331
});
330332
});
331333

334+
describe.only('validatePreset', () => {
335+
it('should validate multiple tiffs', async (t) => {
336+
const test16bTiff = await createTiff('./src/commands/tileindex-validate/__test__/data/16b.tiff');
337+
const test8bTiff = await createTiff('./src/commands/tileindex-validate/__test__/data/8b.tiff');
338+
const fatalStub = t.mock.method(logger, 'fatal');
339+
await assert.rejects(validatePreset('webp', [test16bTiff, test16bTiff, test8bTiff]), {
340+
name: 'Error',
341+
message: `Tiff preset:"webp" validation failed`,
342+
});
343+
344+
assert.equal(fatalStub.mock.callCount(), 2); // Should be called per tiff failure
345+
const opts = fatalStub.mock.calls[0]?.arguments[0] as unknown as Record<string, string>;
346+
assert.equal(opts['preset'], 'webp');
347+
assert.ok(opts['reason']?.includes('is not a 8 bits TIFF'));
348+
});
349+
});
350+
332351
describe('TiffFromMisalignedTiff', () => {
333352
it('should properly identify all tiles under a tiff not aligned to our grid', async () => {
334353
const fakeTiffCover4 = FakeCogTiff.fromTileName('CJ09');

src/commands/tileindex-validate/tileindex.validate.ts

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -122,21 +122,20 @@ export const GridSizeFromString: Type<string, GridSize> = {
122122
* @param preset preset to validate against
123123
* @param tiffs tiffs to validate.
124124
*/
125-
async function validatePreset(preset: string, tiffs: Tiff[]): Promise<void> {
126-
let rejected: boolean = false;
125+
export async function validatePreset(preset: string, tiffs: Tiff[]): Promise<void> {
126+
let rejected = false;
127127

128128
if (preset === 'webp') {
129-
const promises = tiffs.map((f) => validate8BitsTiff(f));
130-
const results = await Promise.allSettled(promises);
131-
for (const r of results) {
132-
if (r.status === 'rejected') {
129+
const promises = tiffs.map((f) => {
130+
return validate8BitsTiff(f).catch((err) => {
131+
logger.fatal({ reason: String(err), source: f.source.url.href, preset }, 'Tiff:ValidatePreset:failed');
133132
rejected = true;
134-
logger.fatal({ reason: r.reason as string }, 'Tiff:ValidatePreset:failed');
135-
}
136-
}
133+
});
134+
});
135+
await Promise.allSettled(promises);
137136
}
138137

139-
if (rejected) throw new Error('Tiff preset validation failed');
138+
if (rejected) throw new Error(`Tiff preset:"${preset}" validation failed`);
140139
}
141140

142141
/**

0 commit comments

Comments
 (0)