diff --git a/packages/cli/src/decorators/controller.registry.ts b/packages/cli/src/decorators/controller.registry.ts index 3a22090db1..24661e0ed2 100644 --- a/packages/cli/src/decorators/controller.registry.ts +++ b/packages/cli/src/decorators/controller.registry.ts @@ -8,6 +8,7 @@ import type { ZodClass } from 'zod-class'; import { AuthService } from '@/auth/auth.service'; import { inProduction, RESPONSE_ERROR_MESSAGES } from '@/constants'; +import { ResponseError } from '@/errors/response-errors/abstract/response.error'; import { UnauthenticatedError } from '@/errors/response-errors/unauthenticated.error'; import type { BooleanLicenseFeature } from '@/interfaces'; import { License } from '@/license'; @@ -102,7 +103,15 @@ export class ControllerRegistry { } } else throw new ApplicationError('Unknown arg type: ' + arg.type); } - return await controller[handlerName](...args); + try { + return await controller[handlerName](...args); + } catch (error) { + if (route.usesTemplates && error instanceof ResponseError) { + res.writeHead(error.httpStatusCode, { 'Content-Type': 'text/plain' }); + return res.end(error.message); + } + throw error; + } }; router[route.method]( diff --git a/packages/cli/src/sso/saml/routes/saml.controller.ee.ts b/packages/cli/src/sso/saml/routes/saml.controller.ee.ts index d51adec3a2..0f3b72fc62 100644 --- a/packages/cli/src/sso/saml/routes/saml.controller.ee.ts +++ b/packages/cli/src/sso/saml/routes/saml.controller.ee.ts @@ -1,7 +1,6 @@ import { validate } from 'class-validator'; import express from 'express'; import querystring from 'querystring'; -import type { PostBindingContext } from 'samlify/types/src/entity'; import url from 'url'; import { AuthService } from '@/auth/auth.service'; @@ -10,7 +9,6 @@ import { AuthError } from '@/errors/response-errors/auth.error'; import { BadRequestError } from '@/errors/response-errors/bad-request.error'; import { EventService } from '@/events/event.service'; import { AuthenticatedRequest } from '@/requests'; -import { sendErrorResponse } from '@/response-helper'; import { UrlService } from '@/services/url.service'; import { @@ -149,8 +147,7 @@ export class SamlController { userEmail: loginResult.attributes.email ?? 'unknown', authenticationMethod: 'saml', }); - // Need to manually send the error response since we're using templates - return sendErrorResponse(res, new AuthError('SAML Authentication failed')); + throw new AuthError('SAML Authentication failed'); } catch (error) { if (isConnectionTestRequest(req)) { return res.render('sso/saml-connection-test-failed', { message: (error as Error).message }); @@ -159,11 +156,7 @@ export class SamlController { userEmail: 'unknown', authenticationMethod: 'saml', }); - // Need to manually send the error response since we're using templates - return sendErrorResponse( - res, - new AuthError('SAML Authentication failed: ' + (error as Error).message), - ); + throw new AuthError('SAML Authentication failed: ' + (error as Error).message); } }