From 2ddb851cab41295e818cf032e3e9fb646fabbfbe Mon Sep 17 00:00:00 2001 From: YLSDev Date: Wed, 7 Feb 2024 22:02:39 +0300 Subject: [PATCH] add JSON errors to Admin.ts --- Server/Source/Modules/Errors.ts | 136 ++++++++++++++++++++++++++++++++ Server/Source/Routes/Admin.ts | 18 +++-- 2 files changed, 147 insertions(+), 7 deletions(-) create mode 100644 Server/Source/Modules/Errors.ts diff --git a/Server/Source/Modules/Errors.ts b/Server/Source/Modules/Errors.ts new file mode 100644 index 0000000..b3d1fb2 --- /dev/null +++ b/Server/Source/Modules/Errors.ts @@ -0,0 +1,136 @@ +export interface ApiError { + errorCode: string + errorMessage: string +} + +export const errorCode = Symbol("errorCode"); + +export abstract class ApiError { + static [errorCode]?: string; + static [Symbol.hasInstance](instance: unknown) { + if (typeof instance !== "object" || instance === null) { + return false; + } + + if (!("errorCode" in instance) || !("errorMessage" in instance) || typeof instance.errorCode !== "string" || typeof instance.errorMessage !== "string") { + return false; + } + + if (this === ApiError) { + return true; + } + + if (!this[errorCode]) { + this[errorCode] = new (this as any)().errorCode; + } + + return this[errorCode] === instance.errorCode; + } +} + +const errorPrefix = `errors.dev.mcthe.partypacker` + +export class InternalError extends ApiError { + errorCode: string; + errorMessage: string; + + constructor(errorMessage: string) { + super(); + this.errorMessage = errorMessage; + this.errorCode = `${errorPrefix}.internal_error`; + } + errorString() { + return `${this.errorCode}: ${this.errorMessage}`; + } + + errorJSON() { + return { + errorCode: this.errorCode, + errorMessage: this.errorMessage + }; + } +} + +export class MissingPermissions extends ApiError { + errorCode: string; + errorMessage: string; + + constructor() { + super(); + this.errorCode = `${errorPrefix}.missing_permissions`; + this.errorMessage = "You don't have permission to access this endpoint."; + } + + errorString() { + return `${this.errorCode}: ${this.errorMessage}`; + } + + errorJSON() { + return { + errorCode: this.errorCode, + errorMessage: this.errorMessage + }; + } +} + +export class MisconfiguredDiscordBot extends ApiError { + /*errorCode: string; + errorMessage: string*/ + + constructor() { + super(); + this.errorCode = `${errorPrefix}.instance.invalid_discord_bot`; + this.errorMessage = "This Partypack instance has a misconfigured Discord bot.";; + } + + errorString() { + return `${this.errorCode}: ${this.errorMessage}`; + } + + errorJSON() { + return { + errorCode: this.errorCode, + errorMessage: this.errorMessage + }; + } +} + +export class MissingServerRole extends ApiError { + constructor() { + super(); + this.errorCode = `${errorPrefix}.missing_discord_server_role`; + this.errorMessage = "This role does not exist in the Discord server."; + } + + errorString() { + return `${this.errorCode}: ${this.errorMessage}`; + } + + errorJSON() { + return { + errorCode: this.errorCode, + errorMessage: this.errorMessage + }; + } +} + +export class MissingDatabaseRole extends ApiError { + constructor() { + super(); + this.errorCode = `${errorPrefix}.missing_database_role`; + this.errorMessage = "This role does not exist in the database."; + } + + errorString() { + return `${this.errorCode}: ${this.errorMessage}`; + } + + errorJSON() { + return { + errorCode: this.errorCode, + errorMessage: this.errorMessage + }; + } +} + +//console.log(new MisconfiguredDiscordBot().errorJSON()) \ No newline at end of file diff --git a/Server/Source/Routes/Admin.ts b/Server/Source/Routes/Admin.ts index a5d9730..e0cb1f1 100644 --- a/Server/Source/Routes/Admin.ts +++ b/Server/Source/Routes/Admin.ts @@ -8,6 +8,7 @@ import { ForcedCategory } from "../Schemas/ForcedCategory"; import { DiscordRole } from "../Schemas/DiscordRole"; import { Bot } from "../Handlers/DiscordBot"; import { DISCORD_SERVER_ID } from "../Modules/Constants"; +import { MisconfiguredDiscordBot, MissingDatabaseRole, MissingServerRole, MissingPermissions } from "../Modules/Errors"; const App = Router(); @@ -19,8 +20,8 @@ App.use(RequireAuthentication()); App.use((req, res, next) => { if (req.user!.PermissionLevel! < UserPermissions.Administrator) - return res.status(403).send("You don't have permission to access this endpoint."); - + //return res.status(403).send("You don't have permission to access this endpoint."); + return res.status(403).json(new MissingPermissions().errorJSON()).set('X-PartyPacker-ErrorString', new MissingPermissions().errorString()); next(); }); @@ -32,10 +33,11 @@ ValidateBody(j.object({ })), async (req, res) => { if (!Bot.isReady()) - return res.status(500).send("This Partypack instance has a misconfigured Discord bot."); - + //return res.status(503).send("This Partypack instance has a misconfigured Discord bot."); + return res.status(503).json(new MisconfiguredDiscordBot().errorJSON()).set('X-PartyPacker-ErrorString', new MisconfiguredDiscordBot().errorString()); if (!Bot.guilds.cache.get(DISCORD_SERVER_ID as string)?.roles.cache.has(req.body.ID)) - return res.status(404).send("This role does not exist in the Discord server."); + //return res.status(400).send("This role does not exist in the Discord server."); + return res.status(400).json(new MissingServerRole().errorJSON()).set('X-PartyPacker-ErrorString', new MissingServerRole().errorString()); const Existing = await DiscordRole.findOne({ where: { ID: req.body.ID } }); if (Existing) { @@ -61,10 +63,12 @@ ValidateBody(j.object({ async (req, res) => { const RoleData = await DiscordRole.findOne({ where: { ID: req.body.ID } }); if (!RoleData) - return res.status(404).send("This role does not exist in the database."); + //return res.status(404).json(new MissingDatabaseRole()); + return res.status(404).json(new MissingDatabaseRole().errorJSON()).set('X-PartyPacker-ErrorString', new MissingDatabaseRole().errorString()); + await RoleData.remove(); - res.send("Removed role successfully."); + res.send.status(204); }) App.get("/roles", async (_, res) => res.json((await DiscordRole.find()).map(x => x.Package(true))));