Skip to content

Commit

Permalink
refactor(#80): use exceptions from tsed
Browse files Browse the repository at this point in the history
  • Loading branch information
Bartholomé Gili authored and barthofu committed Oct 11, 2022
1 parent c3ea248 commit 91cbac3
Show file tree
Hide file tree
Showing 11 changed files with 14 additions and 128 deletions.
19 changes: 9 additions & 10 deletions src/api/controllers/bot.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import { Authenticated, BotOnline } from "@api/middlewares"
import { NotFoundError, UnauthorizedError } from "@api/utils"
import { generalConfig } from "@config"
import { Guild, User } from '@entities'
import { Database } from "@services"
import { Controller, BodyParams, Delete, Get, PathParams, Post, UseBefore } from "@tsed/common"
import { BadRequest } from "@tsed/exceptions"
import { BodyParams, Controller, Delete, Get, PathParams, Post, UseBefore } from "@tsed/common"
import { NotFound, Unauthorized } from "@tsed/exceptions"
import { Required } from "@tsed/schema"
import { BaseController } from "@utils/classes"
import { getDevs, isDev, isInMaintenance, setMaintenance, resolveDependencies } from "@utils/functions"
import { BaseGuildTextChannel, BaseGuildVoiceChannel, ChannelType, NewsChannel, PermissionsBitField, Guild as DGuild } from "discord.js"
import { getDevs, isDev, isInMaintenance, resolveDependencies, setMaintenance } from "@utils/functions"
import { BaseGuildTextChannel, BaseGuildVoiceChannel, ChannelType, Guild as DGuild, NewsChannel, PermissionsBitField } from "discord.js"
import { Client, MetadataStorage } from "discordx"

@Controller('/bot')
Expand Down Expand Up @@ -99,7 +98,7 @@ export class BotController extends BaseController {

} catch (err) {

throw new NotFoundError('Guild not found')
throw new NotFound('Guild not found')
}
}

Expand All @@ -119,7 +118,7 @@ export class BotController extends BaseController {

} catch (err) {

throw new NotFoundError('Guild not found')
throw new NotFound('Guild not found')
}
}

Expand All @@ -130,7 +129,7 @@ export class BotController extends BaseController {
try {
guild = await this.client.guilds.fetch(id)
} catch (err) {
throw new NotFoundError('Guild not found')
throw new NotFound('Guild not found')
}

if (guild) {
Expand All @@ -152,7 +151,7 @@ export class BotController extends BaseController {

if (invite) return invite.toJSON()
else {
throw new UnauthorizedError('Missing permission to create an invite in this guild')
throw new Unauthorized('Missing permission to create an invite in this guild')
}
}
}
Expand Down Expand Up @@ -209,7 +208,7 @@ export class BotController extends BaseController {

} catch (err) {

throw new NotFoundError('User not found')
throw new NotFound('User not found')
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/api/controllers/database.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Authenticated } from "@api/middlewares"
import { InternalServerError } from "@api/utils"
import { databaseConfig } from "@config"
import { Database } from "@services"
import { BodyParams, Controller, Get, Post, UseBefore } from "@tsed/common"
import { InternalServerError } from "@tsed/exceptions"
import { Required } from "@tsed/schema"
import { BaseController } from "@utils/classes"
import { formatDate, resolveDependencies } from "@utils/functions"
Expand Down
10 changes: 4 additions & 6 deletions src/api/middlewares/authenticated.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import { isDev, resolveDependency } from "@utils/functions"
import { Store } from "@services"
import { BadRequestError, UnauthorizedError } from "@api/utils"
import { BadRequest } from "@tsed/exceptions"
import { Context, Middleware, PlatformContext } from "@tsed/common"
import { BadRequest, Unauthorized } from "@tsed/exceptions"
import { isDev, resolveDependency } from "@utils/functions"
import DiscordOauth2 from "discord-oauth2"
import { container } from "tsyringe"

const discordOauth2 = new DiscordOauth2()

Expand Down Expand Up @@ -61,11 +59,11 @@ export class Authenticated {
}, timeout)

} else {
throw new UnauthorizedError('Unauthorized')
throw new Unauthorized('Unauthorized')
}
})
.catch(async (err) => {
throw new BadRequestError('Invalid token')
throw new BadRequest('Invalid token')
})
}
}
1 change: 0 additions & 1 deletion src/api/middlewares/log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { Logger } from "@services"
import { Context, Middleware, PlatformContext } from "@tsed/common"
import { resolveDependency } from "@utils/functions"
import chalk from "chalk"
import { container } from "tsyringe"

@Middleware()
export class Log {
Expand Down
17 changes: 0 additions & 17 deletions src/api/utils/errors/BadRequestError.ts

This file was deleted.

17 changes: 0 additions & 17 deletions src/api/utils/errors/ForbiddenError.ts

This file was deleted.

19 changes: 0 additions & 19 deletions src/api/utils/errors/HttpError.ts

This file was deleted.

17 changes: 0 additions & 17 deletions src/api/utils/errors/InternalServerError.ts

This file was deleted.

17 changes: 0 additions & 17 deletions src/api/utils/errors/NotFoundError.ts

This file was deleted.

17 changes: 0 additions & 17 deletions src/api/utils/errors/UnauthorizedError.ts

This file was deleted.

6 changes: 0 additions & 6 deletions src/api/utils/index.ts

This file was deleted.

0 comments on commit 91cbac3

Please sign in to comment.