Skip to content

Commit

Permalink
Merge pull request #455 from gowon-bot/error-reporting
Browse files Browse the repository at this point in the history
Error reporting
  • Loading branch information
abbyfour authored Oct 10, 2023
2 parents 2faf6b0 + c19b322 commit 9090956
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 6 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,7 @@ config.json
ormconfig.json

docker-compose.*.yml
Dockerfile.dev
Dockerfile.dev

# Sentry Auth Token
.sentryclirc
4 changes: 3 additions & 1 deletion config.example.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,7 @@
"lilacWebsocket": "ws://localhost:4000/socket",
"lilacPassword": "this needs to match the 'password' config in Lilac",

"redisHost": "localhost"
"redisHost": "localhost",

"sentryDSN": "http://<sentry_key>@host.docker.internal:8000/1"
}
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
"@absinthe/socket-apollo-link": "^0.2.1",
"@apollo/client": "^3.6.2",
"@discordjs/rest": "^0.3.0",
"@sentry/cli": "^2.21.2",
"@sentry/node": "^7.72.0",
"@types/absinthe__socket": "^0.2.3",
"@types/absinthe__socket-apollo-link": "^0.2.1",
"@types/phoenix": "^1.5.4",
Expand Down Expand Up @@ -48,14 +50,15 @@
},
"scripts": {
"start": "yarn build && node dist/src/index.js",
"build": "tsc --project ./",
"build": "tsc --project ./ && yarn sentry:sourcemaps",
"rebuild": "yarn clean && yarn build",
"clean": "rm -rf dist/",
"build:watch": "tsc -w --project ./",
"start:watch": "nodemon dist/src/index.js",
"psql": "psql gowon",
"test": "jest",
"migrate": "typeorm migration:run"
"migrate": "typeorm migration:run",
"sentry:sourcemaps": "sentry-cli sourcemaps inject --org gowon-bot --project node ./dist && sentry-cli sourcemaps upload --org gowon-bot --project node ./dist"
},
"devDependencies": {
"@types/chance": "^1.1.2",
Expand Down
4 changes: 4 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { GowonContext } from "./lib/context/Context";
import { Payload } from "./lib/context/Payload";
import { displayUserTag } from "./lib/views/displays";
import { MockMessage } from "./mocks/discord";
import { ReportingService } from "./services/analytics/ReportingService";
import { UsersService } from "./services/dbservices/UsersService";
import {
client,
Expand All @@ -37,6 +38,9 @@ async function start() {

const analyticsCollector = ServiceRegistry.get(AnalyticsCollector);
const usersService = ServiceRegistry.get(UsersService);
const reportingService = ServiceRegistry.get(ReportingService);

reportingService.init();

client.client.on("ready", () => {
console.log(
Expand Down
3 changes: 3 additions & 0 deletions src/lib/command/Command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { GowonService } from "../../services/GowonService";
import { NowPlayingEmbedParsingService } from "../../services/NowPlayingEmbedParsingService";
import { ServiceRegistry } from "../../services/ServicesRegistry";
import { TrackingService } from "../../services/TrackingService";
import { ReportingService } from "../../services/analytics/ReportingService";
import { ArgumentParsingService } from "../../services/arguments/ArgumentsParsingService";
import { MentionsService } from "../../services/arguments/mentions/MentionsService";
import {
Expand Down Expand Up @@ -203,6 +204,7 @@ export abstract class Command<ArgumentsType extends ArgumentsMap = {}> {
discordService = ServiceRegistry.get(DiscordService);
settingsService = ServiceRegistry.get(SettingsService);
mentionsService = ServiceRegistry.get(MentionsService);
reportingService = ServiceRegistry.get(ReportingService);
mirrorballService = ServiceRegistry.get(MirrorballService);
lilacUsersService = ServiceRegistry.get(LilacUsersService);
lilacGuildsService = ServiceRegistry.get(LilacGuildsService);
Expand Down Expand Up @@ -345,6 +347,7 @@ export abstract class Command<ArgumentsType extends ArgumentsMap = {}> {
protected async handleRunError(e: any) {
this.logger.logError(e);
this.analyticsCollector.metrics.commandErrors.inc();
this.reportingService.reportError(this.ctx, e);

console.log(e);

Expand Down
2 changes: 2 additions & 0 deletions src/services/ServicesRegistry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { SpotifyService } from "./Spotify/SpotifyService";
import { TimeAndDateService } from "./TimeAndDateService";
import { TrackingService } from "./TrackingService";
import { WordBlacklistService } from "./WordBlacklistService";
import { ReportingService } from "./analytics/ReportingService";
import { ArgumentParsingService } from "./arguments/ArgumentsParsingService";
import { MentionsService } from "./arguments/mentions/MentionsService";
import { BotStatsService } from "./dbservices/BotStatsService";
Expand Down Expand Up @@ -101,6 +102,7 @@ const services: Service[] = [
RedirectsService,
RedisService,
RedisInteractionService,
ReportingService,
SettingsService,
SpotifyService,
SpotifyArguments,
Expand Down
40 changes: 40 additions & 0 deletions src/services/analytics/ReportingService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import * as Sentry from "@sentry/node";
import config from "../../../config.json";
import { ClientError } from "../../errors/errors";
import { GowonContext } from "../../lib/context/Context";
import { BaseService } from "../BaseService";

export class ReportingService extends BaseService {
async init() {
Sentry.init({
dsn: config.sentryDSN,
});
}

public reportError(ctx: GowonContext, e: Error): void {
Sentry.withScope((scope) => {
this.addContextToScope(ctx, scope);

if (e instanceof ClientError) {
scope.setLevel("log");
}

Sentry.captureException(e);
});
}

private addContextToScope(ctx: GowonContext, scope: Sentry.Scope) {
scope.setTag("command-name", ctx.command?.friendlyNameWithParent);

scope.setUser({
id: ctx.author.id,
username: ctx.author.username,
});

scope.setExtra("user", {
discord: { id: ctx.author.id, username: ctx.author.username },
});
scope.setExtra("arguments", ctx.command.parsedArguments ?? {});
scope.setExtra("guild-id", ctx.guild?.id ?? "DM");
}
}
23 changes: 21 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
/* Basic Options */
// "incremental": true, /* Enable incremental compilation */
"target": "es2019" /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019' or 'ESNEXT'. */,

"module": "commonjs" /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */,
"resolveJsonModule": true,

Expand All @@ -13,25 +14,33 @@
"ESNext.String",
"ES2021"
] /* Specify library files to be included in the compilation. */,

"allowJs": true /* Allow javascript files to be compiled. */,

// "checkJs": true, /* Report errors in .js files. */
"jsx": "react" /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */,

// "declaration": true, /* Generates corresponding '.d.ts' file. */
// "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */
"sourceMap": false /* Generates corresponding '.map' file. */,
"sourceMap": true /* Generates corresponding '.map' file. */,

// "outFile": "./", /* Concatenate and emit output to single file. */
"outDir": "dist" /* Redirect output structure to the directory. */,

// "rootDir": "./", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
// "composite": true, /* Enable project compilation */
// "tsBuildInfoFile": "./", /* Specify file to store incremental compilation information */
"removeComments": true /* Do not emit comments to output. */,

// "noEmit": true, /* Do not emit outputs. */
// "importHelpers": true, /* Import emit helpers from 'tslib'. */
"downlevelIteration": true /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */,

// "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */

/* Strict Type-Checking Options */
"strict": true /* Enable all strict type-checking options. */,

// "noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */
// "strictNullChecks": true, /* Enable strict null checks. */
// "strictFunctionTypes": true, /* Enable strict checking of function types. */
Expand All @@ -43,6 +52,7 @@

/* Additional Checks */
"noUnusedLocals": true /* Report errors on unused locals. */,

"noUnusedParameters": true /* Report errors on unused parameters. */,
"noImplicitReturns": true /* Report error when not all code paths in function return a value. */,
"noFallthroughCasesInSwitch": true /* Report errors for fallthrough cases in switch statement. */,
Expand All @@ -56,6 +66,7 @@
// "types": [], /* Type declaration files to be included in compilation. */
// "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
"esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */,

// "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */

Expand All @@ -67,10 +78,18 @@

/* Experimental Options */
"experimentalDecorators": true /* Enables experimental support for ES7 decorators. */,

"emitDecoratorMetadata": true /* Enables experimental support for emitting type metadata for decorators. */,

/* Advanced Options */
"forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */
"forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */,

"inlineSources": true,

// Set `sourceRoot` to "/" to strip the build path prefix
// from generated source code references.
// This improves issue grouping in Sentry.
"sourceRoot": "/"
},
"include": ["src/"]
// "exclude": ["src/tests"] // Unfortunately excluding disables editor import support :(
Expand Down

0 comments on commit 9090956

Please sign in to comment.