Skip to content

Commit

Permalink
fix(middleware): return proper 400 response when failing to parse acc…
Browse files Browse the repository at this point in the history
…ept header
  • Loading branch information
DASPRiD committed Oct 10, 2024
1 parent a9878ac commit 95f6220
Showing 1 changed file with 31 additions and 4 deletions.
35 changes: 31 additions & 4 deletions src/middleware.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import contentTypeUtil from "content-type";
import { isHttpError } from "http-errors";
import type { Middleware, ParameterizedContext } from "koa";
import { type JsonApiMediaType, getAcceptableMediaTypes } from "./accept.js";
import { type JsonApiMediaType, ParserError, getAcceptableMediaTypes } from "./accept.js";
import { JsonApiBody, JsonApiErrorBody } from "./body.js";
import { InputValidationError } from "./request.js";

Expand All @@ -13,9 +13,36 @@ type JsonApiState = {

export const jsonApiRequestMiddleware = (): Middleware<JsonApiState> => {
return async (context, next) => {
context.state.jsonApi = {
acceptableTypes: getAcceptableMediaTypes(context.get("Accept")),
};
try {
context.state.jsonApi = {
acceptableTypes: getAcceptableMediaTypes(context.get("Accept")),
};
} catch (error) {
if (!(error instanceof ParserError)) {
throw error;
}

context.status = 400;
context.set(
"Content-Type",
contentTypeUtil.format({ type: "application/vnd.api+json" }),
);
context.body = {
jsonapi: { version: "1.1" },
errors: [
{
status: "400",
code: "bad_request",
title: "Bad Request",
detail: error.message,
source: {
header: "accept",
},
},
],
};
return;
}

await next();
handleResponse(context);
Expand Down

0 comments on commit 95f6220

Please sign in to comment.