mirror of
https://github.com/hedgedoc/hedgedoc.git
synced 2025-05-25 12:34:45 -04:00

Previously, when an error was encountered while validating the request, only an HTTP 400 status code was returned to the client. This adds logging of the error message, so invalid requests can be debugged. Signed-off-by: David Mehren <git@herrmehren.de>
29 lines
906 B
TypeScript
29 lines
906 B
TypeScript
/*
|
|
* SPDX-FileCopyrightText: 2021 The HedgeDoc developers (see AUTHORS file)
|
|
*
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
*/
|
|
import { BadRequestException, ValidationPipe } from '@nestjs/common';
|
|
|
|
import { ConsoleLoggerService } from '../logger/console-logger.service';
|
|
|
|
export function setupValidationPipe(
|
|
logger: ConsoleLoggerService,
|
|
): ValidationPipe {
|
|
return new ValidationPipe({
|
|
forbidUnknownValues: true,
|
|
skipMissingProperties: false,
|
|
transform: true,
|
|
exceptionFactory: (errors): BadRequestException => {
|
|
// strip the trailing newline for cleaner logs
|
|
const errorMessage = errors.toString().trimEnd();
|
|
logger.debug(
|
|
`Errors were encountered while validating a request:\n${errorMessage}`,
|
|
'ValidationPipe',
|
|
);
|
|
return new BadRequestException(
|
|
'Encountered an exception while validating the request.',
|
|
);
|
|
},
|
|
});
|
|
}
|