revert back to throwing AuthError, and handle that outside the controller instead

This commit is contained in:
कारतोफ्फेलस्क्रिप्ट™ 2024-10-22 13:41:46 +02:00
parent 0e43286435
commit 39e6432b05
No known key found for this signature in database
2 changed files with 12 additions and 10 deletions

View file

@ -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](

View file

@ -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);
}
}