diff --git a/src/engine/IHttpErrorRenderEngine.ts b/src/engine/IHttpErrorRenderEngine.ts index 0555f45..818074d 100644 --- a/src/engine/IHttpErrorRenderEngine.ts +++ b/src/engine/IHttpErrorRenderEngine.ts @@ -8,13 +8,11 @@ export interface IHttpErrorRenderEngine { * @param obj * @param response */ - render(obj: HttpErrorRenderObj, response: PlatformResponse): Promise; + render(obj: HttpErrorRenderObj, response?: PlatformResponse): Promise; /** * Returns true if this render engine supports the exception thrown by the system * @param exception */ supportsError(exception: Exception): boolean; - - getTitle(): string | null; } diff --git a/src/engine/impl/HttpErrorRenderers/AbstractEjsHttpRenderEngine.ts b/src/engine/impl/HttpErrorRenderers/AbstractEjsHttpRenderEngine.ts deleted file mode 100644 index 2f25ec0..0000000 --- a/src/engine/impl/HttpErrorRenderers/AbstractEjsHttpRenderEngine.ts +++ /dev/null @@ -1,14 +0,0 @@ -import type { IHttpErrorRenderEngine } from "../../IHttpErrorRenderEngine.js"; -import { Exception } from "@tsed/exceptions"; -import type { HttpErrorRenderObj } from "../../../utils/typeings.js"; -import { PlatformResponse } from "@tsed/common"; - -export abstract class AbstractEjsHttpRenderEngine implements IHttpErrorRenderEngine { - public render(obj: HttpErrorRenderObj, response: PlatformResponse): Promise { - return response.render("errorPage.ejs", obj); - } - - public abstract supportsError(exception: Exception): boolean; - - public abstract getTitle(): string; -} diff --git a/src/engine/impl/HttpErrorRenderers/AuthenticationErrorRenderEngine.ts b/src/engine/impl/HttpErrorRenderers/AuthenticationErrorRenderEngine.ts new file mode 100644 index 0000000..6eaaeed --- /dev/null +++ b/src/engine/impl/HttpErrorRenderers/AuthenticationErrorRenderEngine.ts @@ -0,0 +1,21 @@ +import { Injectable, ProviderScope } from "@tsed/di"; +import { HTTP_RENDER_ENGINE } from "../../../model/di/tokens.js"; +import type { IHttpErrorRenderEngine } from "../../IHttpErrorRenderEngine.js"; +import { AuthenticationError } from "../../../model/exceptions/AuthenticationError.js"; +import { Exception } from "@tsed/exceptions"; +import { HttpErrorRenderObj } from "../../../utils/typeings.js"; +import { PlatformResponse } from "@tsed/common"; + +@Injectable({ + scope: ProviderScope.SINGLETON, + type: HTTP_RENDER_ENGINE, +}) +export class AuthenticationErrorRenderEngine implements IHttpErrorRenderEngine { + public supportsError(exception: Exception): boolean { + return exception instanceof AuthenticationError; + } + + public render(obj: HttpErrorRenderObj, response: PlatformResponse): Promise { + return response.render("login.ejs", obj); + } +} diff --git a/src/engine/impl/HttpErrorRenderers/DefaultHttpRenderEngine.ts b/src/engine/impl/HttpErrorRenderers/DefaultHttpRenderEngine.ts index fe0879e..6b29f09 100644 --- a/src/engine/impl/HttpErrorRenderers/DefaultHttpRenderEngine.ts +++ b/src/engine/impl/HttpErrorRenderers/DefaultHttpRenderEngine.ts @@ -19,7 +19,8 @@ export class DefaultHttpRenderEngine implements IHttpErrorRenderEngine, response: PlatformResponse): Promise { const isEncrypted = obj.internalError.isEncrypted; return response.render("fileLogin.ejs", { diff --git a/src/engine/impl/HttpErrorRenderers/ReCAPTCHALoginExceptionRenderEngine.ts b/src/engine/impl/HttpErrorRenderers/ReCAPTCHALoginExceptionRenderEngine.ts new file mode 100644 index 0000000..6b181a9 --- /dev/null +++ b/src/engine/impl/HttpErrorRenderers/ReCAPTCHALoginExceptionRenderEngine.ts @@ -0,0 +1,24 @@ +import type { IHttpErrorRenderEngine } from "../../IHttpErrorRenderEngine.js"; +import { ReCAPTCHAException } from "../../../model/exceptions/ReCAPTCHAException.js"; +import { Injectable, InjectContext, ProviderScope } from "@tsed/di"; +import { HTTP_RENDER_ENGINE } from "../../../model/di/tokens.js"; +import { HttpErrorRenderObj } from "../../../utils/typeings.js"; +import { type PlatformContext, PlatformResponse } from "@tsed/common"; +import { Exception } from "@tsed/exceptions"; + +@Injectable({ + scope: ProviderScope.SINGLETON, + type: HTTP_RENDER_ENGINE, +}) +export class ReCAPTCHALoginExceptionRenderEngine implements IHttpErrorRenderEngine { + @InjectContext() + protected $ctx?: PlatformContext; + + public render(obj: HttpErrorRenderObj, response: PlatformResponse): Promise { + return response.render("login.ejs", obj); + } + + public supportsError(exception: Exception): boolean { + return this.$ctx?.url.includes("/login") ? exception instanceof ReCAPTCHAException : false; + } +} diff --git a/src/engine/impl/index.ts b/src/engine/impl/index.ts index 38e89d8..fc49f93 100644 --- a/src/engine/impl/index.ts +++ b/src/engine/impl/index.ts @@ -5,6 +5,7 @@ export * from "./FileEngine.js"; export * from "./av/ClamAvEngine.js"; export * from "./av/MsDefenderEngine.js"; -export * from "./HttpErrorRenderers/AbstractEjsHttpRenderEngine.js"; +export * from "./HttpErrorRenderers/AuthenticationErrorRenderEngine.js"; export * from "./HttpErrorRenderers/DefaultHttpRenderEngine.js"; export * from "./HttpErrorRenderers/FileProtectedRenderEngine.js"; +export * from "./HttpErrorRenderers/ReCAPTCHALoginExceptionRenderEngine.js"; diff --git a/src/filters/HttpExceptionFilter.ts b/src/filters/HttpExceptionFilter.ts index 46b7e17..3765d07 100644 --- a/src/filters/HttpExceptionFilter.ts +++ b/src/filters/HttpExceptionFilter.ts @@ -18,7 +18,6 @@ export class HttpExceptionFilter implements ExceptionFilterMethods { status: exception.status, message: exception.message, internalError: exception, - title: renderEngine.getTitle(), }; const response = ctx.response; const template = await renderEngine.render(obj, response); diff --git a/src/filters/PassportExceptionFilter.ts b/src/filters/PassportExceptionFilter.ts index 60c048c..5ee33a9 100644 --- a/src/filters/PassportExceptionFilter.ts +++ b/src/filters/PassportExceptionFilter.ts @@ -3,7 +3,7 @@ import { Catch } from "@tsed/common"; import { PassportException } from "@tsed/passport"; import { Inject } from "@tsed/di"; import { HttpExceptionFilter } from "./HttpExceptionFilter.js"; -import { Unauthorized } from "@tsed/exceptions"; +import { AuthenticationError } from "../model/exceptions/AuthenticationError.js"; @Catch(PassportException) export class PassportExceptionFilter implements ExceptionFilterMethods { @@ -11,7 +11,7 @@ export class PassportExceptionFilter implements ExceptionFilterMethods + <% if(typeof internalError !== "undefined") { %> + + <% } %> diff --git a/src/utils/typeings.ts b/src/utils/typeings.ts index 326d364..0b8abe8 100644 --- a/src/utils/typeings.ts +++ b/src/utils/typeings.ts @@ -3,7 +3,6 @@ import { FileUploadModel } from "../model/db/FileUpload.model.js"; export type HttpErrorRenderObj = { status: number; - title: string | null; message: string; internalError: T; }; diff --git a/tsconfig.json b/tsconfig.json index b5d4d73..d34142f 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -24,6 +24,8 @@ "noEmit": true, "esModuleInterop": true, "resolveJsonModule": true, + "noImplicitOverride": true, + "skipLibCheck": true, "lib": [ "es7", "dom",