Skip to content

Commit

Permalink
feat(validation): hide stacktrace on validation error
Browse files Browse the repository at this point in the history
Introduce the `ConfigError`. If that is thrown, no stacktrace will be shown
  • Loading branch information
nicojs committed Apr 19, 2020
1 parent 7bd1d29 commit 8c5ee88
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
18 changes: 12 additions & 6 deletions packages/core/src/StrykerCli.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { Command } from 'commander';
import { getLogger } from 'log4js';
import { DashboardOptions, StrykerOptions, ALL_REPORT_TYPES } from '@stryker-mutator/api/core';
import { Config } from '@stryker-mutator/api/config';
import { Logger } from '@stryker-mutator/api/logging';

import { initializerFactory } from './initializer';
import LogConfigurator from './logging/LogConfigurator';
import Stryker from './Stryker';
import { defaultOptions } from './config/OptionsValidator';
import { retrieveCause, ConfigError } from './errors';

/**
* Interpret a command line argument and add it to an object.
Expand Down Expand Up @@ -36,13 +37,13 @@ export default class StrykerCli {
constructor(
private readonly argv: string[],
private readonly program: Command = new Command(),
private readonly runMutationTest = (options: Partial<StrykerOptions>) => new Stryker(options).runMutationTest(),
private readonly runMutationTest = async (options: Partial<StrykerOptions>) => new Stryker(options).runMutationTest(),
private readonly log: Logger = getLogger(StrykerCli.name)
) {}

public run() {
const dashboard: Partial<DashboardOptions> = {};
const defaultValues = new Config();
const defaultValues = defaultOptions();
this.program
.version(require('../package.json').version)
.usage('<command> [options] [configFile]')
Expand Down Expand Up @@ -151,9 +152,14 @@ export default class StrykerCli {

if (Object.keys(commands).includes(this.command)) {
commands[this.command]().catch(err => {
this.log.error('an error occurred', err);
if (!this.log.isTraceEnabled()) {
this.log.info('Trouble figuring out what went wrong? Try `npx stryker run --fileLogLevel trace --logLevel debug` to get some more info.');
const error = retrieveCause(err);
if (error instanceof ConfigError) {
this.log.error(error.message);
} else {
this.log.error('an error occurred', err);
if (!this.log.isTraceEnabled()) {
this.log.info('Trouble figuring out what went wrong? Try `npx stryker run --fileLogLevel trace --logLevel debug` to get some more info.');
}
}
process.exitCode = 1;
});
Expand Down
12 changes: 12 additions & 0 deletions packages/core/src/errors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { StrykerError } from '@stryker-mutator/util';
import { InjectionError } from 'typed-inject';

export class ConfigError extends StrykerError {}

export function retrieveCause(error: Error) {
if (error instanceof InjectionError) {
return error.cause;
} else {
return error;
}
}

0 comments on commit 8c5ee88

Please sign in to comment.