Better late than never
Better late than never
This commit is contained in:
commit
54f1f876af
136
Server/Source/Modules/Errors.ts
Normal file
136
Server/Source/Modules/Errors.ts
Normal file
|
@ -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())
|
|
@ -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))));
|
||||
|
|
Loading…
Reference in New Issue
Block a user