Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: cors headers on error #803

Merged
merged 5 commits into from
Aug 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions typegate/src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,10 @@ export class BaseError extends Error {
return this;
}

toResponse(graphqlFormat = true): Response {
toResponse(
headers: Headers = new Headers(),
graphqlFormat = true,
): Response {
const type = this.#type ?? this.constructor.name;
logger.error(
"{}[{}:{}]: {}",
Expand Down Expand Up @@ -98,7 +101,7 @@ export class BaseError extends Error {

return new Response(JSON.stringify(responseObj), {
status: this.code,
headers: { "Content-Type": "application/json" },
headers: { "Content-Type": "application/json", ...headers },
});
}
}
Expand Down
4 changes: 2 additions & 2 deletions typegate/src/services/graphql_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export async function handleGraphQL(
content = await parseRequest(request);
} catch (e) {
if (e instanceof BaseError) {
return e.toResponse();
return e.toResponse(headers);
}
return badRequest(e.message);
}
Expand Down Expand Up @@ -120,7 +120,7 @@ export async function handleGraphQL(
} catch (e) {
// throw e;
if (e instanceof BaseError) {
return e.toResponse();
return e.toResponse(headers);
}
if (e instanceof ResolverError) {
logger.error(`field err: ${e.message}`);
Expand Down
4 changes: 2 additions & 2 deletions typegate/src/services/rest_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export async function handleRest(
`query not found: ${name}`,
404,
).withType("NotFound")
.toResponse(false);
.toResponse(headers, false);
}

const variables = req.method === "GET"
Expand All @@ -92,7 +92,7 @@ export async function handleRest(
} catch (e) {
headers.set("Content-Type", "application/json");
if (e instanceof BaseError) {
return e.toResponse(false);
return e.toResponse(headers, false);
}
if (e instanceof ResolverError) {
logger.error(`field err: ${e.message}`);
Expand Down
Loading