Skip to content

Commit 49cca83

Browse files
refactor: simplify code and improve tests (#2159)
1 parent 5e986db commit 49cca83

File tree

2 files changed

+139
-218
lines changed

2 files changed

+139
-218
lines changed

src/middleware.js

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -172,16 +172,16 @@ function wrapper(context) {
172172
}
173173

174174
/**
175-
* @param {NodeJS.ErrnoException} err ann error
175+
* @param {string} message an error message
176176
* @param {number} status status
177177
* @param {Partial<SendErrorOptions<Request, Response>>=} options options
178178
* @returns {Promise<void>}
179179
*/
180-
async function sendError(err, status, options) {
180+
async function sendError(message, status, options) {
181181
if (forwardError) {
182182
const error =
183183
/** @type {Error & { statusCode: number }} */
184-
(new Error(err.message));
184+
(new Error(message));
185185
error.statusCode = status;
186186

187187
await goNext(error);
@@ -252,12 +252,12 @@ function wrapper(context) {
252252
case "ENAMETOOLONG":
253253
case "ENOENT":
254254
case "ENOTDIR":
255-
await sendError(error, 404, {
255+
await sendError(error.message, 404, {
256256
modifyResponseData: context.options.modifyResponseData,
257257
});
258258
break;
259259
default:
260-
await sendError(error, 500, {
260+
await sendError(error.message, 500, {
261261
modifyResponseData: context.options.modifyResponseData,
262262
});
263263
break;
@@ -512,9 +512,7 @@ function wrapper(context) {
512512
}
513513

514514
await sendError(
515-
extra.errorCode === 400
516-
? new Error("Bad Request")
517-
: new Error("Forbidden"),
515+
extra.errorCode === 400 ? "Bad Request" : "Forbidden",
518516
extra.errorCode,
519517
{
520518
modifyResponseData: context.options.modifyResponseData,
@@ -708,7 +706,7 @@ function wrapper(context) {
708706
// Conditional GET support
709707
if (isConditionalGET()) {
710708
if (isPreconditionFailure()) {
711-
await sendError(new Error("Precondition Failed"), 412, {
709+
await sendError("Precondition Failed", 412, {
712710
modifyResponseData: context.options.modifyResponseData,
713711
});
714712
return;
@@ -760,7 +758,7 @@ function wrapper(context) {
760758
getValueContentRangeHeader("bytes", size),
761759
);
762760

763-
await sendError(new Error("Range Not Satisfiable"), 416, {
761+
await sendError("Range Not Satisfiable", 416, {
764762
headers: {
765763
"Content-Range": getResponseHeader(res, "Content-Range"),
766764
},

0 commit comments

Comments
 (0)