Skip to content

Commit

Permalink
fix(common): fix catching error on PlatformMulterMiddleware
Browse files Browse the repository at this point in the history
Closes: #1809
  • Loading branch information
Romakita committed Mar 17, 2022
1 parent 05d5f0b commit 796ad58
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import {EndpointMetadata, MulterOptions, MultipartFile, PlatformApplication, Pla
import {Exception} from "@tsed/exceptions";
import {expect} from "chai";
import Sinon from "sinon";
import {MulterError} from "multer";
import {catchAsyncError} from "@tsed/core";

const sandbox = Sinon.createSandbox();

Expand Down Expand Up @@ -51,7 +53,6 @@ describe("PlatformMulterMiddleware", () => {
expect(multer.fields).to.have.been.calledWithExactly([{maxCount: undefined, name: "file1"}]);
expect(multerMiddleware).to.have.been.calledWithExactly(ctx.request.raw, ctx.response.raw);
});

it("should create middleware with storage", async () => {
const {middleware, ctx, multer, app, multerMiddleware} = await build({
storage: "storage"
Expand All @@ -65,28 +66,18 @@ describe("PlatformMulterMiddleware", () => {
expect(multer.fields).to.have.been.calledWithExactly([{maxCount: undefined, name: "file1"}]);
expect(multerMiddleware).to.have.been.calledWithExactly(ctx.request.raw, ctx.response.raw);
});

it("should catch error with code", async () => {
const {middleware, ctx, multer, app, multerMiddleware} = await build();
const {middleware, ctx, multerMiddleware} = await build();
const error = new MulterError("LIMIT_FILE_SIZE", "field");

multerMiddleware.rejects({
code: 400,
message: "message",
field: "field"
});
multerMiddleware.rejects(error);

let actualError: any;
try {
await middleware.use(ctx);
} catch (er) {
actualError = er;
}
const actualError: any | undefined = await catchAsyncError(() => middleware.use(ctx));

expect(actualError).to.be.instanceof(Exception);
expect(actualError.message).to.eq("message field");
expect(actualError.status).to.eq(400);
expect(actualError?.message).to.eq("File too large");
expect(actualError?.status).to.eq(400);
});

it("should throw error without code", async () => {
const {middleware, ctx, multerMiddleware} = await build();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
import {Constant, Inject} from "@tsed/di";
import {Exception} from "@tsed/exceptions";
import {BadRequest} from "@tsed/exceptions";
import {Middleware, MiddlewareMethods} from "@tsed/platform-middlewares";
import {Context} from "@tsed/platform-params";
import {PlatformMulterField, PlatformMulterSettings} from "../config/interfaces/PlatformMulterSettings";
import {PlatformApplication} from "../services/PlatformApplication";
import type {MulterError} from "multer";

export interface MulterInputOptions {
fields: PlatformMulterField[];
}

export class MulterException extends BadRequest {
constructor(er: MulterError) {
super(er.message);
this.origin = er;
this.name = er.code;
}
}

/**
* @middleware
*/
Expand Down Expand Up @@ -37,7 +46,11 @@ export class PlatformMulterMiddleware implements MiddlewareMethods {

return await middleware(ctx.getRequest(), ctx.getResponse());
} catch (er) {
throw er.code ? new Exception(er.code, `${er.message} ${er.field || ""}`.trim()) : er;
if (er.code) {
throw new MulterException(er);
}

throw er;
}
}

Expand Down

0 comments on commit 796ad58

Please sign in to comment.