mirror of
https://github.com/hedgedoc/hedgedoc.git
synced 2025-05-14 07:04:45 -04:00
fix(repository): Move backend code into subdirectory
Signed-off-by: Tilman Vatteroth <git@tilmanvatteroth.de>
This commit is contained in:
parent
86584e705f
commit
bf30cbcf48
272 changed files with 87 additions and 67 deletions
100
backend/src/errors/error-mapping.ts
Normal file
100
backend/src/errors/error-mapping.ts
Normal file
|
@ -0,0 +1,100 @@
|
|||
/*
|
||||
* SPDX-FileCopyrightText: 2022 The HedgeDoc developers (see AUTHORS file)
|
||||
*
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
import {
|
||||
ArgumentsHost,
|
||||
BadRequestException,
|
||||
Catch,
|
||||
ConflictException,
|
||||
InternalServerErrorException,
|
||||
NotFoundException,
|
||||
PayloadTooLargeException,
|
||||
UnauthorizedException,
|
||||
} from '@nestjs/common';
|
||||
import { HttpException } from '@nestjs/common/exceptions/http.exception';
|
||||
import { BaseExceptionFilter } from '@nestjs/core';
|
||||
|
||||
import {
|
||||
buildHttpExceptionObject,
|
||||
HttpExceptionObject,
|
||||
} from './http-exception-object';
|
||||
|
||||
type HttpExceptionConstructor = (object: HttpExceptionObject) => HttpException;
|
||||
|
||||
const mapOfHedgeDocErrorsToHttpErrors: Map<string, HttpExceptionConstructor> =
|
||||
new Map([
|
||||
['NotInDBError', (object): HttpException => new NotFoundException(object)],
|
||||
[
|
||||
'AlreadyInDBError',
|
||||
(object): HttpException => new ConflictException(object),
|
||||
],
|
||||
[
|
||||
'ForbiddenIdError',
|
||||
(object): HttpException => new BadRequestException(object),
|
||||
],
|
||||
['ClientError', (object): HttpException => new BadRequestException(object)],
|
||||
[
|
||||
'PermissionError',
|
||||
(object): HttpException => new UnauthorizedException(object),
|
||||
],
|
||||
[
|
||||
'TokenNotValidError',
|
||||
(object): HttpException => new UnauthorizedException(object),
|
||||
],
|
||||
[
|
||||
'TooManyTokensError',
|
||||
(object): HttpException => new BadRequestException(object),
|
||||
],
|
||||
[
|
||||
'PermissionsUpdateInconsistentError',
|
||||
(object): HttpException => new BadRequestException(object),
|
||||
],
|
||||
[
|
||||
'MediaBackendError',
|
||||
(object): HttpException => new InternalServerErrorException(object),
|
||||
],
|
||||
[
|
||||
'PrimaryAliasDeletionForbiddenError',
|
||||
(object): HttpException => new BadRequestException(object),
|
||||
],
|
||||
[
|
||||
'InvalidCredentialsError',
|
||||
(object): HttpException => new UnauthorizedException(object),
|
||||
],
|
||||
[
|
||||
'NoLocalIdentityError',
|
||||
(object): HttpException => new BadRequestException(object),
|
||||
],
|
||||
[
|
||||
'PasswordTooWeakError',
|
||||
(object): HttpException => new BadRequestException(object),
|
||||
],
|
||||
[
|
||||
'MaximumDocumentLengthExceededError',
|
||||
(object): HttpException => new PayloadTooLargeException(object),
|
||||
],
|
||||
]);
|
||||
|
||||
@Catch()
|
||||
export class ErrorExceptionMapping extends BaseExceptionFilter<Error> {
|
||||
catch(error: Error, host: ArgumentsHost): void {
|
||||
super.catch(ErrorExceptionMapping.transformError(error), host);
|
||||
}
|
||||
|
||||
private static transformError(error: Error): Error {
|
||||
const httpExceptionConstructor = mapOfHedgeDocErrorsToHttpErrors.get(
|
||||
error.name,
|
||||
);
|
||||
if (httpExceptionConstructor === undefined) {
|
||||
// We don't know how to map this error and just leave it be
|
||||
return error;
|
||||
}
|
||||
const httpExceptionObject = buildHttpExceptionObject(
|
||||
error.name,
|
||||
error.message,
|
||||
);
|
||||
return httpExceptionConstructor(httpExceptionObject);
|
||||
}
|
||||
}
|
61
backend/src/errors/errors.ts
Normal file
61
backend/src/errors/errors.ts
Normal file
|
@ -0,0 +1,61 @@
|
|||
/*
|
||||
* SPDX-FileCopyrightText: 2022 The HedgeDoc developers (see AUTHORS file)
|
||||
*
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
|
||||
export class NotInDBError extends Error {
|
||||
name = 'NotInDBError';
|
||||
}
|
||||
|
||||
export class AlreadyInDBError extends Error {
|
||||
name = 'AlreadyInDBError';
|
||||
}
|
||||
|
||||
export class ForbiddenIdError extends Error {
|
||||
name = 'ForbiddenIdError';
|
||||
}
|
||||
|
||||
export class ClientError extends Error {
|
||||
name = 'ClientError';
|
||||
}
|
||||
|
||||
export class PermissionError extends Error {
|
||||
name = 'PermissionError';
|
||||
}
|
||||
|
||||
export class TokenNotValidError extends Error {
|
||||
name = 'TokenNotValidError';
|
||||
}
|
||||
|
||||
export class TooManyTokensError extends Error {
|
||||
name = 'TooManyTokensError';
|
||||
}
|
||||
|
||||
export class PermissionsUpdateInconsistentError extends Error {
|
||||
name = 'PermissionsUpdateInconsistentError';
|
||||
}
|
||||
|
||||
export class MediaBackendError extends Error {
|
||||
name = 'MediaBackendError';
|
||||
}
|
||||
|
||||
export class PrimaryAliasDeletionForbiddenError extends Error {
|
||||
name = 'PrimaryAliasDeletionForbiddenError';
|
||||
}
|
||||
|
||||
export class InvalidCredentialsError extends Error {
|
||||
name = 'InvalidCredentialsError';
|
||||
}
|
||||
|
||||
export class NoLocalIdentityError extends Error {
|
||||
name = 'NoLocalIdentityError';
|
||||
}
|
||||
|
||||
export class PasswordTooWeakError extends Error {
|
||||
name = 'PasswordTooWeakError';
|
||||
}
|
||||
|
||||
export class MaximumDocumentLengthExceededError extends Error {
|
||||
name = 'MaximumDocumentLengthExceededError';
|
||||
}
|
20
backend/src/errors/http-exception-object.ts
Normal file
20
backend/src/errors/http-exception-object.ts
Normal file
|
@ -0,0 +1,20 @@
|
|||
/*
|
||||
* SPDX-FileCopyrightText: 2022 The HedgeDoc developers (see AUTHORS file)
|
||||
*
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
|
||||
export interface HttpExceptionObject {
|
||||
name: string;
|
||||
message: string;
|
||||
}
|
||||
|
||||
export function buildHttpExceptionObject(
|
||||
name: string,
|
||||
message: string,
|
||||
): HttpExceptionObject {
|
||||
return {
|
||||
name: name,
|
||||
message: message,
|
||||
};
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue