Skip to content

Commit

Permalink
feat: improve error handler
Browse files Browse the repository at this point in the history
  • Loading branch information
NedcloarBR committed May 29, 2024
1 parent e30a8ab commit 26c7caa
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 5 deletions.
12 changes: 12 additions & 0 deletions src/lib/node-handler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Logger } from "@nestjs/common";

export function NodeHandler(): void {
const logger = new Logger("NodeHandler");
process.on("uncaughtException", (error, origin) => {
logger.error("Uncaught Exception:", error, "Origin: ", origin);
});

process.on("unhandledRejection", (reason, promise) => {
logger.error("Unhandled Rejection at:", promise, "reason:", reason);
});
}
25 changes: 20 additions & 5 deletions src/lib/sharding.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import path from "node:path";
import { Config } from "@/modules/config/types";
import { Logger } from "@nestjs/common";
import type { ConfigService } from "@nestjs/config";
import { ShardingManager as _ShardingManager } from "discord.js";
import path from "node:path";

export class ShardingManager extends _ShardingManager {
public constructor(private readonly config: ConfigService) {
Expand All @@ -19,10 +20,6 @@ export class ShardingManager extends _ShardingManager {
this.spawn();

this.on("shardCreate", (shard) => {
shard.on("reconnecting", () => {
this.logger.warn(`Reconnecting shard: [${shard.id}]`);
});

shard.on("spawn", () => {
this.logger.log(`Spawned shard: [${shard.id}]`);
});
Expand All @@ -31,6 +28,24 @@ export class ShardingManager extends _ShardingManager {
this.logger.log(`Shard [${shard.id}] is ready`);
});

shard.on("disconnect", () => {
this.logger.error(`Shard [${shard.id}] disconnected`);
});

shard.on("reconnecting", () => {
this.logger.warn(`Reconnecting shard: [${shard.id}]`);
});

shard.on("resume", () => {
this.logger.log(`Shard [${shard.id}] resumed`);
});

shard.on("message", (message) => {
if (this.config.getOrThrow<Config["Debug"]>("Debug").Shard) {
this.logger.verbose(message);
}
});

shard.on("death", () => {
this.logger.fatal(`Died shard: [${shard.id}]`);
});
Expand Down
2 changes: 2 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ import { ConfigService } from "@nestjs/config";
import { NestFactory } from "@nestjs/core";
import { AppModule } from "./app.module";
import { ShardingManager, TopGGAutoPoster } from "./lib";
import { NodeHandler } from "./lib/node-handler";

async function bootstrap() {
NodeHandler();
const app = await NestFactory.create(AppModule);
const configService = app.get<ConfigService>(ConfigService);
const logger = new Logger("Main");
Expand Down
1 change: 1 addition & 0 deletions src/modules/config/Config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export const config = (): Config => ({
},
Debug: {
Client: true,
Shard: false,
Translations: false,
Lavalink: false,
PremiumMusicPlayer: false,
Expand Down
1 change: 1 addition & 0 deletions src/modules/config/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export interface Config {
};
Debug: {
Client: boolean;
Shard: boolean;
Translations: boolean;
Lavalink: boolean;
PremiumMusicPlayer: boolean;
Expand Down

0 comments on commit 26c7caa

Please sign in to comment.