Skip to content

Commit

Permalink
refactor: merge artifacts
Browse files Browse the repository at this point in the history
  • Loading branch information
barthofu authored Jul 5, 2022
1 parent 7613991 commit dbd1ccc
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 37 deletions.
6 changes: 2 additions & 4 deletions src/commands/Admin/prefix.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { Slash, Discord, SlashOption } from "@decorators"
import { Guard, UserPermissions } from "@guards"
import { Guild } from "@entities"
import { resolveGuild, simpleSuccessEmbed } from "@utils/functions"
import { Database, ErrorHandler } from "@services"
import { Database } from "@services"
import { getLocaleFromInteraction, L } from "@i18n"

import { generalConfig } from '@config'
Expand All @@ -20,7 +20,6 @@ export default class PrefixCommand {

constructor(
private db: Database,
private errorHandler: ErrorHandler
) {}

@Slash('prefix', { description:
Expand Down Expand Up @@ -50,8 +49,7 @@ export default class PrefixCommand {
}))
}
else {
throw new UnknownReplyError(interaction);
//this.errorHandler.unknownErrorReply(interaction)
throw new UnknownReplyError(interaction)
}

}
Expand Down
4 changes: 2 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ async function run() {
container.registerInstance(Client, client)

// init the error handler
container.resolve(ErrorHandler);
container.resolve(ErrorHandler)

// import all the commands and events
await importx(__dirname + "/{events,commands,api}/**/*.{ts,js}")
Expand All @@ -43,7 +43,7 @@ async function run() {

// upload images to imgur if configured
if (process.env.IMGUR_CLIENT_ID && generalConfig.automaticUploadImagesToImgur) {
container.resolve(ImagesUpload).synchroWithDatabase()
container.resolve(ImagesUpload).syncWithDatabase()
}

// start the api server
Expand Down
40 changes: 20 additions & 20 deletions src/services/ErrorHandler.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,42 @@
import { CommandInteraction } from 'discord.js'
import { singleton } from 'tsyringe'
import { parse } from 'stacktrace-parser'

import { Logger } from '@services'
import { BaseError } from '@utils/classes'


@singleton()
export class ErrorHandler {

constructor(
private logger: Logger
) {
// Catch all exeptions
// Ccatch all exeptions
process.on('uncaughtException', (error: Error, origin: string) => {
// Stop if is unhandledRejection
if(origin === "unhandledRejection") return;

// If instance of BaseError, call `handle` method
if(error instanceof BaseError) return error.handle();
// stop in case of unhandledRejection
if (origin === "unhandledRejection") return

// if instance of BaseError, call `handle` method
if (error instanceof BaseError) return error.handle()

// If the error is not a instance of BaseError
const trace = parse(error.stack || "")?.[0];
if(trace) return this.logger.log("error", `Exception at : ${trace?.file}:${trace?.lineNumber}\n\t> ${error.message}`);
// if the error is not a instance of BaseError
const trace = parse(error.stack || "")?.[0]
if (trace) return this.logger.log("error", `Exception at : ${trace?.file}:${trace?.lineNumber}\n\t> ${error.message}`)

this.logger.log("error", "An error as occured in a unknow file\n\t> " + error.message);
});
this.logger.log("error", "An error as occured in a unknow file\n\t> " + error.message)
})

// Catch all Unhandled Rejection (promise)
// catch all Unhandled Rejection (promise)
process.on('unhandledRejection', (error: Error | any, promise: Promise<any>) => {
// If instance of BaseError, call `handle` method
if(error instanceof BaseError) return error.handle();

// If the error is not a instance of BaseError
const trace = parse(error.stack || "")?.[0];
if(trace) return this.logger.log("error", `Unhandled rejection at ${trace?.file}:${trace?.lineNumber}\n\t> ${error.message}`);
// if instance of BaseError, call `handle` method
if(error instanceof BaseError) return error.handle()

// if the error is not a instance of BaseError
const trace = parse(error.stack || "")?.[0]
if(trace) return this.logger.log("error", `Unhandled rejection at ${trace?.file}:${trace?.lineNumber}\n\t> ${error.message}`)

this.logger.log("error", "An unhandled rejection as occured in a unknow file\n\t> " + error);
});
this.logger.log("error", "An unhandled rejection as occured in a unknow file\n\t> " + error)
})
}
}
2 changes: 1 addition & 1 deletion src/services/ImagesUpload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export class ImagesUpload {
return false
}

async synchroWithDatabase() {
async syncWithDatabase() {

// add missing images to the database

Expand Down
12 changes: 7 additions & 5 deletions src/utils/classes/BaseError.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { Logger } from "@services";
import { container } from "tsyringe";
import { Logger } from "@services"
import { container } from "tsyringe"

export abstract class BaseError extends Error {
protected logger: Logger;

protected logger: Logger

constructor(message?: string) {
super(message);
this.logger = container.resolve(Logger);

super(message)
this.logger = container.resolve(Logger)
}

public handle() {}
Expand Down
13 changes: 8 additions & 5 deletions src/utils/errors/UnknownReply.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,19 @@ import { simpleErrorEmbed } from '@utils/functions/embeds'
import { BaseError } from "@utils/classes"

export class UnknownReplyError extends BaseError {
private interaction: CommandInteraction;

private interaction: CommandInteraction

constructor(interaction: CommandInteraction, message?: string) {
super(message);

super(message)

this.interaction = interaction;
this.interaction = interaction
}

public handle() {
const locale = getLocaleFromInteraction(this.interaction);
simpleErrorEmbed(this.interaction, L[locale]['ERRORS']['UNKNOWN']());

const locale = getLocaleFromInteraction(this.interaction)
simpleErrorEmbed(this.interaction, L[locale]['ERRORS']['UNKNOWN']())
}
}

0 comments on commit dbd1ccc

Please sign in to comment.