Skip to content

Commit

Permalink
fix: Add more error capturing
Browse files Browse the repository at this point in the history
  • Loading branch information
schw4rzlicht committed Jun 13, 2020
1 parent 48b855c commit 5b576a6
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 15 deletions.
10 changes: 9 additions & 1 deletion src/lib/Config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@ import Ajv = require("ajv");
import configSchema = require("../resources/config.schema.json");
import _ = require("lodash");

export class ConfigError extends Error {
constructor(message: string) {
super(message);
Object.setPrototypeOf(this, ConfigError.prototype);
this.name = ConfigError.name;
}
}

export class Config {
public readonly timeout: number;
public readonly ma: MaConfig;
Expand All @@ -20,7 +28,7 @@ export class Config {
ajv.validate(configSchema, config);

if(_.isArray(ajv.errors)) {
throw new Error(`Config file is invalid: ${ajv.errorsText(ajv.errors, {dataVar: "config"})}!`);
throw new ConfigError(`Config file is invalid: ${ajv.errorsText(ajv.errors, {dataVar: "config"})}!`);
}

this.timeout = config.timeout;
Expand Down
6 changes: 4 additions & 2 deletions src/lib/Twitch2Ma.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import CooldownPermission from "./permissions/CooldownPermission";
import OwnerPermission from "./permissions/OwnerPermission";
import ModeratorPermission from "./permissions/ModeratorPermission";

import sentry from "./sentry";

import type Telnet from "telnet-client";

import SourceMapSupport = require("source-map-support");
Expand Down Expand Up @@ -68,7 +70,7 @@ export default class Twitch2Ma extends EventEmitter {
ors: "\r\n",
})
.catch(() => {
throw new TelnetError("Could not connect to desk!")
throw new TelnetError("Could not connect to desk!");
})
.then(() => this.telnetLogin())
.then(() => this.initTwitch());
Expand Down Expand Up @@ -156,7 +158,7 @@ export default class Twitch2Ma extends EventEmitter {
this.chatClient.say(channel, reason.viewerMessage);
this.emit(this.onPermissionDenied, channel, user, reason.name);
})
.catch(() => this.stopWithError(new TelnetError("Sending telnet command failed!")));
.catch(error => sentry(error, () => this.stopWithError(new TelnetError("Sending telnet command failed!"))));
}
}
}
Expand Down
12 changes: 7 additions & 5 deletions src/lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {Command} from "commander";
import Twitch2Ma from "./Twitch2Ma";
import {Config} from "./Config";
import Twitch2Ma, {TelnetError} from "./Twitch2Ma";
import {Config, ConfigError} from "./Config";
import sentry from "./sentry";

import Fs = require("fs");
import YAML = require("yaml");
Expand All @@ -20,7 +21,7 @@ export async function main() {
return require("libnpm")
.manifest(`${packageInformation.name}@latest`)
.then(notifyUpdate)
.catch(() => warning("Could not get update information!"))
.catch((error: Error) => sentry(error, () => warning("Could not get update information!")))
.then(init);
}

Expand All @@ -35,7 +36,8 @@ function init(): void {
.then(config => new Twitch2Ma(config))
.then(attachEventHandlers)
.then(twitch2Ma => twitch2Ma.start())
.catch(exitWithError);
.catch((error: ConfigError | TelnetError) => exitWithError(error))
.catch(error => sentry(error, exitWithError));
})
.parse(process.argv);
}
Expand Down Expand Up @@ -94,7 +96,7 @@ export async function loadConfig(configFile: string): Promise<Config> {
try {
rawConfigObject = YAML.parse(rawConfigFile);
} catch (ignored) {
throw new Error(`Config file ${configFile} is not a valid JSON or YAML file!`);
throw new ConfigError(`Config file ${configFile} is not a valid JSON or YAML file!`);
}
}

Expand Down
17 changes: 10 additions & 7 deletions src/lib/sentry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,21 @@ export function init(packageInformation: any) {
version: process.version
});

if(process.env.NODE_ENV === "development" || process.env.NODE_ENV === "staging") {
if (process.env.NODE_ENV === "development" || process.env.NODE_ENV === "staging") {
Sentry.setUser({username: os.userInfo().username});
}
}
} catch(ignored){
} catch (ignored) {
}
}

export default async function sentry(error: Error, messageHandler?: (error: Error) => any) {
export default function sentry(error: Error, messageHandler?: (error: Error) => any) {
Sentry.captureException(error);
await Sentry.flush();
if(messageHandler) {
messageHandler(error);
}
return Sentry.flush()
.catch(() => Promise.resolve())
.then(() => {
if (messageHandler) {
messageHandler(error);
}
});
}

0 comments on commit 5b576a6

Please sign in to comment.