Skip to content

Commit

Permalink
fix: default "Content-Type" header not being set on the v2 error re…
Browse files Browse the repository at this point in the history
…sponses
  • Loading branch information
SeanCassiere committed Jun 24, 2024
1 parent e7dd804 commit 8799f38
Showing 1 changed file with 6 additions and 11 deletions.
17 changes: 6 additions & 11 deletions src/utils/server-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,19 +59,15 @@ export const v2_serviceValidation = honoFactory.createMiddleware(async (c, next)

if (!serviceId) {
throw new HTTPException(401, {
res: createV2ErrResponse(
JSON.stringify({ success: false, message: ENDPOINT_MESSAGES.ServiceIdHeaderNotProvided }),
),
res: createV2ErrResponse(ENDPOINT_MESSAGES.ServiceIdHeaderNotProvided),
});
}

const service = await getService(serviceId);

if (!service) {
throw new HTTPException(403, {
res: createV2ErrResponse(
JSON.stringify({ success: false, message: ENDPOINT_MESSAGES.ServiceDoesNotExistOrDoesNotHaveNecessaryRights }),
),
res: createV2ErrResponse(ENDPOINT_MESSAGES.ServiceDoesNotExistOrDoesNotHaveNecessaryRights),
});
}

Expand All @@ -86,10 +82,9 @@ export const adminServiceValidation = honoFactory.createMiddleware(async (c, nex
const service = c.var.service;

if (!service || !service.isAdmin) {
console.log("service", service);
throw new HTTPException(403, {
res: createV2ErrResponse(
JSON.stringify({ success: false, message: ENDPOINT_MESSAGES.ServiceDoesNotExistOrDoesNotHaveNecessaryRights }),
),
res: createV2ErrResponse(ENDPOINT_MESSAGES.ServiceDoesNotExistOrDoesNotHaveNecessaryRights),
});
}

Expand All @@ -112,9 +107,9 @@ export function getUserServerUrl(): string {
export function createV2ErrResponse(message: string, headers: Record<string, string> = {}): Response {
const responseHeaders = new Headers(headers);

if (!responseHeaders.get("Content-Type")) {
if (!responseHeaders.has("Content-Type")) {
responseHeaders.set("Content-Type", "application/json");
}

return new Response(message, { headers });
return new Response(JSON.stringify({ success: false, message }), { headers: responseHeaders });
}

0 comments on commit 8799f38

Please sign in to comment.