Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(cli): ensure fatal errors set process exit code to 1 #842

Merged
merged 1 commit into from
Jun 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions packages/cli/src/cli/base.cli.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env node
import { Env, LogConfig } from '@basemaps/shared';
import { Env, LogConfig, LoggerFatalError } from '@basemaps/shared';
import { GitTag } from '@basemaps/shared/build/cli/git.tag';
import { CommandLineParser } from '@rushstack/ts-command-line';
import { PrettyTransform } from 'pretty-json-log';
Expand Down Expand Up @@ -57,7 +57,11 @@ export abstract class BaseCommandLine extends CommandLineParser {

public run(): void {
this.executeWithoutErrorHandling().catch((err) => {
LogConfig.get().fatal({ err }, 'Failed to run command');
if (err instanceof LoggerFatalError) {
LogConfig.get().fatal(err.obj, err.message);
} else {
LogConfig.get().fatal({ err }, 'Failed to run command');
}
process.exit(1);
});
}
Expand Down
26 changes: 14 additions & 12 deletions packages/cli/src/cli/cogify/action.cog.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Env, FileOperator, LogConfig, LogType, ProjectionTileMatrixSet } from '@basemaps/shared';
import { Env, FileOperator, LogConfig, LoggerFatalError, ProjectionTileMatrixSet } from '@basemaps/shared';
import {
CommandLineAction,
CommandLineFlagParameter,
Expand All @@ -8,8 +8,8 @@ import {
import { createReadStream, promises as fs } from 'fs';
import { FeatureCollection } from 'geojson';
import { buildCogForName } from '../../cog/cog';
import { Cutline } from '../../cog/cutline';
import { CogVrt } from '../../cog/cog.vrt';
import { Cutline } from '../../cog/cutline';
import { CogJob } from '../../cog/types';
import { GdalCogBuilder } from '../../gdal/gdal';
import { CliId, CliInfo } from '../base.cli';
Expand All @@ -30,16 +30,15 @@ export class ActionCogCreate extends CommandLineAction {
});
}

getName(job: CogJob, logger: LogType): string | null {
getName(job: CogJob): string | null {
const batchIndex = Env.getNumber(Env.BatchIndex, -1);
if (batchIndex > -1) {
const { name } = job.files[batchIndex];
if (name == null) {
logger.fatal(
throw new LoggerFatalError(
{ cogIndex: batchIndex, tileMax: job.files.length - 1 },
'Failed to find cog name from batch index',
);
return null;
}
return name;
}
Expand All @@ -48,15 +47,19 @@ export class ActionCogCreate extends CommandLineAction {
if (cogIndex != null) {
const { name } = job.files[cogIndex];
if (name == null) {
logger.fatal({ cogIndex, tileMax: job.files.length - 1 }, 'Failed to find cog name from index');
return null;
throw new LoggerFatalError(
{ cogIndex, tileMax: job.files.length - 1 },
'Failed to find cog name from index',
);
}
return name;
}
const name = this.name?.value;
if (name == null || !job.files.find((r) => r.name === name)) {
logger.fatal({ name, names: job.files.map((r) => r.name).join(', ') }, 'Name does not exist inside job');
return null;
throw new LoggerFatalError(
{ name, names: job.files.map((r) => r.name).join(', ') },
'Name does not exist inside job',
);
}
return name;
}
Expand All @@ -69,8 +72,7 @@ export class ActionCogCreate extends CommandLineAction {

const jobVersion = SemVer.compare(job.generated?.version ?? '', CliInfo.version);
if (jobVersion !== 0) {
LogConfig.get().fatal({ jobInfo: job.generated, cli: CliInfo }, 'Version mismatch');
return;
throw new LoggerFatalError({ jobInfo: job.generated, cli: CliInfo }, 'Version mismatch');
}

const isCommit = this.commit?.value ?? false;
Expand All @@ -81,7 +83,7 @@ export class ActionCogCreate extends CommandLineAction {
const gdalVersion = await GdalCogBuilder.getVersion(logger);
logger.info({ version: gdalVersion }, 'GdalVersion');

const name = this.getName(job, logger);
const name = this.getName(job);
if (name == null) {
return;
}
Expand Down
14 changes: 11 additions & 3 deletions packages/cli/src/cog/builder.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { Epsg, GeoJson, Bounds } from '@basemaps/geo';
import { CompositeError, FileOperatorSimple, LogType, ProjectionTileMatrixSet } from '@basemaps/shared';
import {
CompositeError,
FileOperatorSimple,
LogType,
ProjectionTileMatrixSet,
LoggerFatalError,
} from '@basemaps/shared';
import { CogSource, CogTiff, TiffTag, TiffTagGeo } from '@cogeotiff/core';
import { CogSourceFile } from '@cogeotiff/source-file';
import { createHash } from 'crypto';
Expand Down Expand Up @@ -170,8 +176,10 @@ export class CogBuilder {
const noDataNum = parseInt(noData);

if (isNaN(noDataNum) || noDataNum < 0 || noDataNum > 256) {
this.logger.fatal({ tiff: tiff.source.name, noData }, 'Failed converting GDAL_NODATA, defaulting to 255');
throw new Error(`Invalid GDAL_NODATA: ${noData}`);
throw new LoggerFatalError(
{ tiff: tiff.source.name, noData },
'Failed converting GDAL_NODATA, defaulting to 255',
);
}

return noDataNum;
Expand Down
1 change: 1 addition & 0 deletions packages/shared/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export { tileFromPath, TileType, TileData, TileDataWmts, TileDataXyz } from './a
export { V, VNode, VNodeElement, VNodeText } from './vdom';
export { VNodeParser } from './vdom.parse';
export { CompositeError } from './composite.error';
export { LoggerFatalError } from './logger.fatal.error';
export * from './tms/projection.tile.matrix.set';

export * from './aws/tile.metadata.base';
Expand Down
10 changes: 10 additions & 0 deletions packages/shared/src/logger.fatal.error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* Utility error to throw expecting that Logger will fatal log its contents
*/
export class LoggerFatalError extends Error {
obj: Record<string, unknown>;
constructor(obj: Record<string, unknown>, msg: string) {
super(msg);
this.obj = obj;
}
}