Skip to content

Commit

Permalink
refactor: capture only specific errors (#487)
Browse files Browse the repository at this point in the history
relates-to: #458
  • Loading branch information
lsndr authored Oct 26, 2023
1 parent 9c7a6b6 commit 0ac6eb5
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 20 deletions.
10 changes: 8 additions & 2 deletions src/Commands/RunRepeater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
} from '../Repeater';
import { Arguments, Argv, CommandModule } from 'yargs';
import { Lifecycle } from 'tsyringe';
import { captureException } from '@sentry/node';
import { normalize } from 'path';

export class RunRepeater implements CommandModule {
Expand Down Expand Up @@ -244,12 +245,16 @@ export class RunRepeater implements CommandModule {

if (args.remove) {
await repeaterLauncher.uninstall();
process.exit(0);
process.exitCode = 0;

return;
}

if (args.daemon) {
await repeaterLauncher.install();
process.exit(0);
process.exitCode = 0;

return;
}

try {
Expand All @@ -262,6 +267,7 @@ export class RunRepeater implements CommandModule {

await repeaterLauncher.run(args.id as string, args.run as boolean);
} catch (e) {
captureException(e);
logger.error(e);
await repeaterLauncher.close();
process.exitCode = 1;
Expand Down
4 changes: 3 additions & 1 deletion src/Repeater/DefaultRepeaterLauncher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { RepeaterCommandHub } from './RepeaterCommandHub';
import { RuntimeDetector } from './RuntimeDetector';
import { gt } from 'semver';
import chalk from 'chalk';
import { captureException } from '@sentry/node';
import { delay, inject, injectable } from 'tsyringe';
import Timer = NodeJS.Timer;

Expand All @@ -40,9 +41,10 @@ export class DefaultRepeaterLauncher implements RepeaterLauncher {
@inject(delay(() => CliInfo)) private readonly info: CliInfo
) {
this.bus.onReconnectionFailure(async (e: Error) => {
captureException(e);
logger.error(e);
await this.close();
process.exit(1);
process.exitCode = 1;
});
}

Expand Down
10 changes: 7 additions & 3 deletions src/Repeater/DefaultRepeaterServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { inject, injectable } from 'tsyringe';
import io, { Socket } from 'socket.io-client';
import parser from 'socket.io-msgpack-parser';
import { SocksProxyAgent } from 'socks-proxy-agent';
import { captureException, captureMessage } from '@sentry/node';
import { once } from 'events';
import { parse } from 'url';
import Timer = NodeJS.Timer;
Expand Down Expand Up @@ -170,9 +171,11 @@ export class DefaultRepeaterServer implements RepeaterServer {
public errorOccurred(
handler: (event: RepeaterServerErrorEvent) => void | Promise<void>
): void {
this.socket.on('error', (payload, callback) =>
this.processEventHandler('error', payload, handler, callback)
);
this.socket.on('error', (payload, callback) => {
captureMessage(payload.message);

return this.processEventHandler('error', payload, handler, callback);
});
}

public reconnectionAttempted(
Expand Down Expand Up @@ -220,6 +223,7 @@ export class DefaultRepeaterServer implements RepeaterServer {

callback(response);
} catch (error) {
captureException(error);
logger.debug(
'Error processing event "%s" with the following payload: %s. Details: %s',
event,
Expand Down
4 changes: 3 additions & 1 deletion src/Repeater/ServerRepeaterLauncher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { CliInfo } from '../Config';
import { RepeaterCommandHub } from './RepeaterCommandHub';
import { delay, inject, injectable } from 'tsyringe';
import chalk from 'chalk';
import { captureException } from '@sentry/node';

@injectable()
export class ServerRepeaterLauncher implements RepeaterLauncher {
Expand Down Expand Up @@ -161,9 +162,10 @@ export class ServerRepeaterLauncher implements RepeaterLauncher {
}

private reconnectionFailed({ error }: RepeaterServerReconnectionFailedEvent) {
captureException(error);
logger.error(error);
this.close().catch(logger.error);
process.exit(1);
process.exitCode = 1;
}

private async testingNetwork(event: RepeaterServerNetworkTestEvent) {
Expand Down
13 changes: 0 additions & 13 deletions src/Utils/Logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,6 @@ export class Logger {
return;
}

const captureContext = {
contexts: {
errorArgs: Object.fromEntries([messageOrArg, ...args].entries())
}
};
let message: string;

if (typeof errorOrMessage === 'string') {
Expand All @@ -54,15 +49,7 @@ export class Logger {
}

message = errorOrMessage;
const formattedMessage = format(message, ...args);

captureMessage(formattedMessage, {
...captureContext,
level: 'error'
});
} else {
captureException(errorOrMessage, captureContext);

message = messageOrArg ?? errorOrMessage.message;
}

Expand Down

0 comments on commit 0ac6eb5

Please sign in to comment.