diff --git a/src/Server.ts b/src/Server.ts index fada416..8f16877 100644 --- a/src/Server.ts +++ b/src/Server.ts @@ -44,8 +44,8 @@ import { fileURLToPath } from "node:url"; import { ExpressRateLimitTypeOrmStore } from "typeorm-rate-limit-store"; import { ExpressRateLimitStoreModel } from "./model/db/ExpressRateLimitStore.model.js"; import { Exception, TooManyRequests } from "@tsed/exceptions"; -import { DefaultRenderObj } from "./engine/impl/index.js"; import { Logger } from "@tsed/logger"; +import { DefaultRenderException } from "./model/rest/DefaultRenderException.js"; const opts: Partial = { ...config, @@ -240,7 +240,7 @@ export class Server implements BeforeRoutesInit { } } - private parseError(error: Exception): DefaultRenderObj { + private parseError(error: Exception): DefaultRenderException { return { name: error.origin?.name ?? error.name, message: error.message, diff --git a/src/controllers/rest/impl/FileUploadController.ts b/src/controllers/rest/impl/FileUploadController.ts index df6272f..2fd624b 100644 --- a/src/controllers/rest/impl/FileUploadController.ts +++ b/src/controllers/rest/impl/FileUploadController.ts @@ -2,7 +2,7 @@ import { Controller, Inject } from "@tsed/di"; import { Delete, Description, Example, Examples, Get, Name, Patch, Put, Returns, Summary } from "@tsed/schema"; import { StatusCodes } from "http-status-codes"; import { FileUploadResponseDto } from "../../../model/dto/FileUploadResponseDto.js"; -import { BadRequest, Forbidden, UnsupportedMediaType } from "@tsed/exceptions"; +import { BadRequest } from "@tsed/exceptions"; import { MultipartFile, PathParams, type PlatformMulterFile, QueryParams, Req, Res } from "@tsed/common"; import { BodyParams } from "@tsed/platform-params"; import { FileService } from "../../../services/FileService.js"; @@ -11,11 +11,12 @@ import { BaseRestController } from "../BaseRestController.js"; import { Logger } from "@tsed/logger"; import { EntryModificationDto } from "../../../model/dto/EntryModificationDto.js"; import type { Request, Response } from "express"; +import { DefaultRenderException } from "../../../model/rest/DefaultRenderException.js"; @Controller("/") @Description("This is the API documentation for uploading and sharing files.") @Name("File Uploader") -@Returns(StatusCodes.FORBIDDEN, Forbidden).Description("If your IP has been blocked") +@Returns(StatusCodes.FORBIDDEN, DefaultRenderException).Description("If your IP has been blocked") export class FileUploadController extends BaseRestController { public constructor( @Inject() private fileUploadService: FileService, @@ -26,9 +27,9 @@ export class FileUploadController extends BaseRestController { @Put() @Returns(StatusCodes.CREATED, FileUploadResponseDto).Description("If the file was stored successfully") - @Returns(StatusCodes.BAD_REQUEST, BadRequest).Description("If the request was malformed") + @Returns(StatusCodes.BAD_REQUEST, DefaultRenderException).Description("If the request was malformed") @Returns(StatusCodes.OK, FileUploadResponseDto).Description("If the file already exists") - @Returns(StatusCodes.UNSUPPORTED_MEDIA_TYPE, UnsupportedMediaType).Description( + @Returns(StatusCodes.UNSUPPORTED_MEDIA_TYPE, DefaultRenderException).Description( "If the media type of the file specified was blocked", ) @Example({ @@ -121,7 +122,7 @@ export class FileUploadController extends BaseRestController { @Get("/:token") @Returns(StatusCodes.OK, FileUploadResponseDto) - @Returns(StatusCodes.BAD_REQUEST, BadRequest) + @Returns(StatusCodes.BAD_REQUEST, DefaultRenderException) @Description("Get entry info such as when it will expire and the URL") @Summary("Get entry info via token") public getInfo( @@ -141,7 +142,7 @@ export class FileUploadController extends BaseRestController { @Patch("/:token") @Returns(StatusCodes.OK, FileUploadResponseDto) - @Returns(StatusCodes.BAD_REQUEST, BadRequest) + @Returns(StatusCodes.BAD_REQUEST, DefaultRenderException) @Description("Modify an entry such as password, expiry and other settings") @Summary("Modify components of an entry") public modifyEntry( @@ -158,7 +159,7 @@ export class FileUploadController extends BaseRestController { @Delete("/:token") @Returns(StatusCodes.OK, Boolean) - @Returns(StatusCodes.BAD_REQUEST, BadRequest) + @Returns(StatusCodes.BAD_REQUEST, DefaultRenderException) @Description("Delete a file via the token") @Summary("Delete a file from a token") public async deleteEntry(@PathParams("token") token: string): Promise { diff --git a/src/controllers/rest/impl/security/AdminController.ts b/src/controllers/rest/impl/security/AdminController.ts index f4acf05..fb78bcc 100644 --- a/src/controllers/rest/impl/security/AdminController.ts +++ b/src/controllers/rest/impl/security/AdminController.ts @@ -4,14 +4,15 @@ import { AdminService } from "../../../../services/AdminService.js"; import { Authorize } from "@tsed/passport"; import { BodyParams } from "@tsed/platform-params"; import { PlatformResponse, QueryParams, Res } from "@tsed/common"; -import { Forbidden, NotFound } from "@tsed/exceptions"; +import { NotFound } from "@tsed/exceptions"; import { BaseRestController } from "../../BaseRestController.js"; import { StatusCodes } from "http-status-codes"; import type { DatatableColumn, DatatableOrder, DatatableSearch } from "../../../../utils/typeings.js"; +import { DefaultRenderException } from "../../../../model/rest/DefaultRenderException.js"; @Hidden() @Controller("/admin") -@Returns(StatusCodes.FORBIDDEN, Forbidden).Description("If your IP has been blocked") +@Returns(StatusCodes.FORBIDDEN, DefaultRenderException).Description("If your IP has been blocked") @Authorize("loginAuthProvider") export class AdminController extends BaseRestController { public constructor(@Inject() private adminService: AdminService) { diff --git a/src/controllers/rest/impl/security/PassportCtrl.ts b/src/controllers/rest/impl/security/PassportCtrl.ts index 3669daf..26d8072 100644 --- a/src/controllers/rest/impl/security/PassportCtrl.ts +++ b/src/controllers/rest/impl/security/PassportCtrl.ts @@ -9,12 +9,12 @@ import { BaseRestController } from "../../BaseRestController.js"; import { CustomUserInfoModel } from "../../../../model/auth/CustomUserInfoModel.js"; import { UserService } from "../../../../services/UserService.js"; import { CaptchaMiddleWare } from "../../../../middleware/endpoint/CaptchaMiddleWare.js"; -import { Forbidden } from "@tsed/exceptions"; +import { DefaultRenderException } from "../../../../model/rest/DefaultRenderException.js"; @Controller("/auth") @Scope(ProviderScope.SINGLETON) @Hidden() -@Returns(StatusCodes.FORBIDDEN, Forbidden).Description("If your IP has been blocked") +@Returns(StatusCodes.FORBIDDEN, DefaultRenderException).Description("If your IP has been blocked") export class PassportCtrl extends BaseRestController { public constructor(@Inject() private usersService: UserService) { super(); diff --git a/src/engine/impl/HttpErrorRenderers/DefaultHttpRenderEngine.ts b/src/engine/impl/HttpErrorRenderers/DefaultHttpRenderEngine.ts index 6b29f09..24094fc 100644 --- a/src/engine/impl/HttpErrorRenderers/DefaultHttpRenderEngine.ts +++ b/src/engine/impl/HttpErrorRenderers/DefaultHttpRenderEngine.ts @@ -3,19 +3,14 @@ import { Exception } from "@tsed/exceptions"; import type { HttpErrorRenderObj } from "../../../utils/typeings.js"; import { Injectable, ProviderScope } from "@tsed/di"; import { HTTP_RENDER_ENGINE } from "../../../model/di/tokens.js"; - -export type DefaultRenderObj = { - name: string; - message: string; - status: number; -}; +import { DefaultRenderException } from "../../../model/rest/DefaultRenderException.js"; @Injectable({ scope: ProviderScope.SINGLETON, type: HTTP_RENDER_ENGINE, }) -export class DefaultHttpRenderEngine implements IHttpErrorRenderEngine { - public render(obj: HttpErrorRenderObj): Promise { +export class DefaultHttpRenderEngine implements IHttpErrorRenderEngine { + public render(obj: HttpErrorRenderObj): Promise { return Promise.resolve(this.mapError(obj.internalError)); } @@ -24,11 +19,7 @@ export class DefaultHttpRenderEngine implements IHttpErrorRenderEngine