Skip to content

Commit

Permalink
Merge pull request #47 from moreal/PDX-150-uncaught-error-slack
Browse files Browse the repository at this point in the history
Notify uncaught error to slack
  • Loading branch information
moreal authored Nov 21, 2023
2 parents 9602a50 + 097a09f commit 6b08853
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 9 deletions.
19 changes: 10 additions & 9 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ import { AppStopEvent } from "./slack/messages/app-stop-event";
import { Sqlite3MonitorStateStore } from "./sqlite3-monitor-state-store";
import { Planet } from "./types/registry";

const slackBot = new SlackBot(
getRequiredEnv("SLACK__BOT_USERNAME"),
new SlackChannel(
getRequiredEnv("SLACK__CHANNEL"),
new WebClient(getRequiredEnv("SLACK__BOT_TOKEN")),
),
);

(async () => {
const [upstreamPlanet, downstreamPlanet]: Planet[] =
await new PreloadHandler().preparePlanets();
Expand Down Expand Up @@ -81,14 +89,6 @@ import { Planet } from "./types/registry";
new AssetDownstreamObserver(upstreamTransfer, downstreamBurner),
);

const slackBot = new SlackBot(
getRequiredEnv("SLACK__BOT_USERNAME"),
new SlackChannel(
getRequiredEnv("SLACK__CHANNEL"),
new WebClient(getRequiredEnv("SLACK__BOT_TOKEN")),
),
);

await slackBot.sendMessage(
new AppStartEvent(
await upstreamAccount.getAddress(),
Expand All @@ -114,7 +114,8 @@ import { Planet } from "./types/registry";
upstreamAssetsTransferredMonitorMonitor.run();
downstreamAssetsTransferredMonitorMonitor.run();
garageMonitor.run();
})().catch((error) => {
})().catch(async (error) => {
console.error(error);
await slackBot.sendMessage(new AppStopEvent(error));
process.exit(-1);
});
26 changes: 26 additions & 0 deletions src/slack/messages/app-stop-event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,33 @@ import { ISlackMessage, SlackMessageSymbol } from ".";
export class AppStopEvent implements ISlackMessage {
[SlackMessageSymbol] = true as const;

constructor(private readonly error?: Error) {}

render() {
if (this.error) {
return {
text: "App stopped with uncaught error",
attachments: [
{
title: "Type",
text: this.error.constructor.name,
},
{
title: "Name",
text: this.error.name,
},
{
title: "Message",
text: this.error.message,
},
{
title: "Stack",
text: this.error.stack,
},
],
};
}

return {
text: "App Stopped",
fallback: "App Stopped",
Expand Down

0 comments on commit 6b08853

Please sign in to comment.