@proventuslabs/nestjs-multipart-form
Version:
A lightweight and efficient NestJS package for handling multipart form data and file uploads with RxJS streaming support and type safety.
56 lines • 2.93 kB
JavaScript
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.MultipartExceptionFilter = void 0;
const common_1 = require("@nestjs/common");
const errors_1 = require("../core/errors");
/**
* Exception filter that catches all MultipartError instances (including subclasses)
* and transforms them into appropriate NestJS HTTP exceptions.
*
* Mapping:
* - PartsLimitError, FilesLimitError, FieldsLimitError → PayloadTooLargeException (HTTP 413)
* - TruncatedFileError, TruncatedFieldError, MissingFilesError, MissingFieldsError → BadRequestException (HTTP 400)
* - Other MultipartError instances → InternalServerErrorException (HTTP 500)
*
* The original error is preserved using the `cause` property.
*/
let MultipartExceptionFilter = class MultipartExceptionFilter {
catch(exception, host) {
if (host.getType() !== "http")
throw exception;
const ctx = host.switchToHttp();
const res = ctx.getResponse();
let httpException;
// map specific multipart errors to appropriate HTTP exceptions
if (exception instanceof errors_1.PartsLimitError ||
exception instanceof errors_1.FilesLimitError ||
exception instanceof errors_1.FieldsLimitError) {
httpException = new common_1.PayloadTooLargeException(exception.message, { cause: exception });
}
else if (exception instanceof errors_1.TruncatedFileError ||
exception instanceof errors_1.TruncatedFieldError ||
exception instanceof errors_1.MissingFilesError ||
exception instanceof errors_1.MissingFieldsError) {
httpException = new common_1.BadRequestException(exception.message, { cause: exception });
}
else {
httpException = new common_1.InternalServerErrorException(exception.message, { cause: exception });
}
res.status(httpException.getStatus()).json({
statusCode: httpException.getStatus(),
message: httpException.message,
error: httpException.name,
});
}
};
exports.MultipartExceptionFilter = MultipartExceptionFilter;
exports.MultipartExceptionFilter = MultipartExceptionFilter = __decorate([
(0, common_1.Catch)(errors_1.MultipartError)
], MultipartExceptionFilter);
//# sourceMappingURL=exception-filter.js.map
;