mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-16 04:40:32 +00:00
feat(nestjs): migrate to NestJS
This commit is contained in:
1
packages/server/src/common/constants/files.constants.ts
Normal file
1
packages/server/src/common/constants/files.constants.ts
Normal file
@@ -0,0 +1 @@
|
||||
export const MULTER_MODULE_OPTIONS = 'MULTER_MODULE_OPTIONS';
|
||||
24
packages/server/src/common/constants/multer.constants.ts
Normal file
24
packages/server/src/common/constants/multer.constants.ts
Normal file
@@ -0,0 +1,24 @@
|
||||
import type { Multer } from 'multer';
|
||||
import * as multerS3 from 'multer-s3';
|
||||
|
||||
export const multerExceptions = {
|
||||
// from https://github.com/expressjs/multer/blob/master/lib/multer-error.js
|
||||
LIMIT_PART_COUNT: 'Too many parts',
|
||||
LIMIT_FILE_SIZE: 'File too large',
|
||||
LIMIT_FILE_COUNT: 'Too many files',
|
||||
LIMIT_FIELD_KEY: 'Field name too long',
|
||||
LIMIT_FIELD_VALUE: 'Field value too long',
|
||||
LIMIT_FIELD_COUNT: 'Too many fields',
|
||||
LIMIT_UNEXPECTED_FILE: 'Unexpected field',
|
||||
MISSING_FIELD_NAME: 'Field name missing',
|
||||
};
|
||||
|
||||
export const busboyExceptions = {
|
||||
// from https://github.com/mscdex/busboy/blob/master/lib/types/multipart.js
|
||||
MULTIPART_BOUNDARY_NOT_FOUND: 'Multipart: Boundary not found',
|
||||
MULTIPART_MALFORMED_PART_HEADER: 'Malformed part header',
|
||||
MULTIPART_UNEXPECTED_END_OF_FORM: 'Unexpected end of form',
|
||||
MULTIPART_UNEXPECTED_END_OF_FILE: 'Unexpected end of file',
|
||||
};
|
||||
|
||||
|
||||
38
packages/server/src/common/constants/multer.utils.ts
Normal file
38
packages/server/src/common/constants/multer.utils.ts
Normal file
@@ -0,0 +1,38 @@
|
||||
import {
|
||||
BadRequestException,
|
||||
HttpException,
|
||||
PayloadTooLargeException,
|
||||
} from '@nestjs/common';
|
||||
import { multerExceptions, busboyExceptions } from './multer.constants';
|
||||
|
||||
// Multer may add in a 'field' property to the error
|
||||
// https://github.com/expressjs/multer/blob/aa42bea6ac7d0cb8fcb279b15a7278cda805dc63/lib/multer-error.js#L19
|
||||
export function transformException(
|
||||
error: (Error & { field?: string }) | undefined,
|
||||
) {
|
||||
if (!error || error instanceof HttpException) {
|
||||
return error;
|
||||
}
|
||||
switch (error.message) {
|
||||
case multerExceptions.LIMIT_FILE_SIZE:
|
||||
return new PayloadTooLargeException(error.message);
|
||||
case multerExceptions.LIMIT_FILE_COUNT:
|
||||
case multerExceptions.LIMIT_FIELD_KEY:
|
||||
case multerExceptions.LIMIT_FIELD_VALUE:
|
||||
case multerExceptions.LIMIT_FIELD_COUNT:
|
||||
case multerExceptions.LIMIT_UNEXPECTED_FILE:
|
||||
case multerExceptions.LIMIT_PART_COUNT:
|
||||
case multerExceptions.MISSING_FIELD_NAME:
|
||||
if (error.field) {
|
||||
return new BadRequestException(`${error.message} - ${error.field}`);
|
||||
}
|
||||
return new BadRequestException(error.message);
|
||||
case busboyExceptions.MULTIPART_BOUNDARY_NOT_FOUND:
|
||||
return new BadRequestException(error.message);
|
||||
case busboyExceptions.MULTIPART_MALFORMED_PART_HEADER:
|
||||
case busboyExceptions.MULTIPART_UNEXPECTED_END_OF_FORM:
|
||||
case busboyExceptions.MULTIPART_UNEXPECTED_END_OF_FILE:
|
||||
return new BadRequestException(`Multipart: ${error.message}`);
|
||||
}
|
||||
return error;
|
||||
}
|
||||
Reference in New Issue
Block a user