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

feat(*): unified error log output #601

Merged
merged 11 commits into from
Oct 29, 2024
8 changes: 5 additions & 3 deletions src/Commands/Configure.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { logger } from '../Utils';
import { ErrorMessageFactory, logger } from '../Utils';
import { ConnectivityUrls, Platform, TestType, Options } from '../Wizard';
import container from '../container';
import { Arguments, Argv, CommandModule } from 'yargs';
Expand Down Expand Up @@ -98,8 +98,10 @@ export class Configure implements CommandModule {

process.on('SIGTERM', stop).on('SIGINT', stop).on('SIGHUP', stop);
await app.start({ ping: !!args.ping, traceroute: !!args.traceroute });
} catch (e) {
logger.error(`Error during "configure": ${e.error || e.message}`);
} catch (error) {
logger.error(
ErrorMessageFactory.genericCommandError({ error, command: 'configure' })
);
process.exit(1);
}
}
Expand Down
11 changes: 8 additions & 3 deletions src/Commands/GetEntryPoints.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { EntryPoint, EntryPoints, RestProjectsOptions } from '../EntryPoint';
import { logger } from '../Utils';
import { ErrorMessageFactory, logger } from '../Utils';
import { Arguments, Argv, CommandModule } from 'yargs';
import { container } from 'tsyringe';

Expand Down Expand Up @@ -90,8 +90,13 @@ export class GetEntryPoints implements CommandModule {
);

process.exitCode = 0;
} catch (e) {
logger.error(`Error during "entrypoints:list": ${e.error || e.message}`);
} catch (error) {
logger.error(
ErrorMessageFactory.genericCommandError({
error,
command: 'entrypoints:list'
})
);
process.exitCode = 1;
}
}
Expand Down
15 changes: 10 additions & 5 deletions src/Commands/PollingScanStatus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
PollingFactory,
RestScansOptions
} from '../Scan';
import { Helpers, logger } from '../Utils';
import { ErrorMessageFactory, Helpers, logger } from '../Utils';
import { Arguments, Argv, CommandModule } from 'yargs';
import { container } from 'tsyringe';

Expand Down Expand Up @@ -73,14 +73,19 @@ export class PollingScanStatus implements CommandModule {
await polling.start();

process.exit(0);
} catch (e) {
if (e instanceof BreakpointException) {
} catch (error) {
if (error instanceof BreakpointException) {
logger.error(`The breakpoint has been hit during polling.`);
logger.error(`Breakpoint: ${e.message}`);
logger.error(`Breakpoint: ${error.message}`);
process.exit(50);
}

logger.error(`Error during "scan:polling": ${e.error || e.message}`);
logger.error(
ErrorMessageFactory.genericCommandError({
error,
command: 'scan:polling'
})
);
process.exit(1);
}
}
Expand Down
11 changes: 8 additions & 3 deletions src/Commands/RetestScan.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { RestScansOptions, Scans } from '../Scan';
import { logger } from '../Utils';
import { ErrorMessageFactory, logger } from '../Utils';
import { Arguments, Argv, CommandModule } from 'yargs';
import { container } from 'tsyringe';

Expand Down Expand Up @@ -43,8 +43,13 @@ export class RetestScan implements CommandModule {
console.log(scanId);

process.exit(0);
} catch (e) {
logger.error(`Error during "scan:retest": ${e.error || e.message}`);
} catch (error) {
logger.error(
ErrorMessageFactory.genericCommandError({
error,
command: 'scan:retest'
})
);
process.exit(1);
}
}
Expand Down
10 changes: 6 additions & 4 deletions src/Commands/RunRepeater.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Cert, RequestExecutorOptions } from '../RequestExecutor';
import { Helpers, logger } from '../Utils';
import { ErrorMessageFactory, Helpers, logger } from '../Utils';
import container from '../container';
import { DefaultRepeaterServerOptions, RepeaterLauncher } from '../Repeater';
import { Arguments, Argv, CommandModule } from 'yargs';
Expand Down Expand Up @@ -254,9 +254,11 @@ export class RunRepeater implements CommandModule {
);

await repeaterLauncher.run(args.id as string, args.run as boolean);
} catch (e) {
captureException(e);
logger.error(e);
} catch (error) {
captureException(error);
logger.error(
ErrorMessageFactory.genericCommandError({ error, command: 'repeater' })
);
await repeaterLauncher.close();
process.exitCode = 1;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Commands/RunScan.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ describe('RunScan', () => {

// assert
verify(processSpy.exit(1)).once();
verify(loggerSpy.error(`Error during "scan:run": ${errMessage}`)).once();
verify(loggerSpy.error(`Error during "scan:run": ${errMessage}.`)).once();
});

it('should display warnings when present', async () => {
Expand Down
19 changes: 10 additions & 9 deletions src/Commands/RunScan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@ import {
Scans,
ATTACK_PARAM_LOCATIONS_DEFAULT
} from '../Scan';
import { Helpers, logger } from '../Utils';
import { ErrorMessageFactory, Helpers, logger } from '../Utils';
import { Arguments, Argv, CommandModule } from 'yargs';
import { container } from 'tsyringe';
import { isAxiosError } from 'axios';
import { EOL } from 'node:os';

export class RunScan implements CommandModule {
Expand All @@ -25,7 +24,11 @@ export class RunScan implements CommandModule {

if (!nonEmptyPatterns.length) {
logger.error(
'Error during "scan:run": please make sure that patterns contain at least one regexp.'
ErrorMessageFactory.genericCommandError({
command: 'scan:run',
error:
'please make sure that patterns contain at least one regexp'
})
);
process.exit(1);
}
Expand Down Expand Up @@ -214,12 +217,10 @@ export class RunScan implements CommandModule {
}

process.exit(0);
} catch (e) {
const errMessage =
isAxiosError(e) && typeof e.response?.data === 'string'
? e.response.data
: e.error || e.message;
logger.error(`Error during "scan:run": ${errMessage}`);
} catch (error) {
logger.error(
ErrorMessageFactory.genericCommandError({ error, command: 'scan:run' })
);
process.exit(1);
}
}
Expand Down
8 changes: 5 additions & 3 deletions src/Commands/StopScan.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { RestScansOptions, Scans } from '../Scan';
import { logger } from '../Utils';
import { ErrorMessageFactory, logger } from '../Utils';
import { Arguments, Argv, CommandModule } from 'yargs';
import { container } from 'tsyringe';

Expand Down Expand Up @@ -41,8 +41,10 @@ export class StopScan implements CommandModule {
await scanManager.stop(args.scan as string);

process.exit(0);
} catch (e) {
logger.error(`Error during "scan:stop": ${e.error || e.message}`);
} catch (error) {
logger.error(
ErrorMessageFactory.genericCommandError({ error, command: 'scan:stop' })
);
process.exit(1);
}
}
Expand Down
11 changes: 8 additions & 3 deletions src/Commands/UploadArchive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
Spec,
SpecType
} from '../Archive';
import { Helpers, logger } from '../Utils';
import { ErrorMessageFactory, Helpers, logger } from '../Utils';
import container from '../container';
import { Arguments, Argv, CommandModule } from 'yargs';

Expand Down Expand Up @@ -119,8 +119,13 @@ export class UploadArchive implements CommandModule {
// eslint-disable-next-line no-console
console.log(await archives.upload(spec));
process.exit(0);
} catch (e) {
logger.error(`Error during "archive:upload": ${e.message}`);
} catch (error) {
logger.error(
ErrorMessageFactory.genericCommandError({
error,
command: 'archive:upload'
})
);
process.exit(1);
}
}
Expand Down
45 changes: 45 additions & 0 deletions src/Utils/ErrorMessageFactory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { isAxiosError } from 'axios';

type GenericCommandErrorParams =
| { command: string; error: any }
| { message: string; error: unknown };

export class ErrorMessageFactory {
public static genericCommandError(params: GenericCommandErrorParams): string {
const message = this.getTitle(params);
const details = this.extractErrorDetails(params);

return this.formatFinalMessage(message, details);
}

private static formatFinalMessage(
baseMessage: string,
errorDetails?: string
): string {
return errorDetails
? `${baseMessage}: ${errorDetails}.`
: `${baseMessage}.`;
}

private static getTitle(params: GenericCommandErrorParams): string {
return 'message' in params
? params.message
: `Error during "${params.command}"`;
}

private static extractErrorDetails(
params: GenericCommandErrorParams
): string | null {
if (typeof params.error === 'string') {
return params.error;
}
if (
isAxiosError(params.error) &&
typeof params.error.response?.data === 'string'
) {
params.error.response.data;
}

return (params.error.error || params.error.message) ?? null;
}
}
1 change: 1 addition & 0 deletions src/Utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export * from './Backoff';
export * from './DefaultProxyFactory';
export * from './ErrorMessageFactory';
export * from './Helpers';
export * from './Logger';
export * from './ProxyFactory';
Expand Down
Loading