Skip to content

Commit

Permalink
Catch all errors in runSquiggle, give 500 if server error
Browse files Browse the repository at this point in the history
  • Loading branch information
OAGr committed Dec 31, 2023
1 parent 0dd04ee commit aefb449
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 23 deletions.
64 changes: 41 additions & 23 deletions packages/hub/src/app/api/runSquiggle/route.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,53 @@
import { runSquiggle } from "@/graphql/queries/runSquiggle";
import { NextRequest, NextResponse } from "next/server";

import { runSquiggle } from "@/graphql/queries/runSquiggle";

export async function POST(req: NextRequest) {
// Assuming 'code' is sent in the request body and is a string
const body = await req.json();
if (body.code) {
let response = await runSquiggle(body.code);
if (response.isOk) {
return new NextResponse(
JSON.stringify({
result: response.resultJSON,
bindings: response.bindingsJSON,
}),
{
status: 200,
statusText: "OK",
headers: { "Content-Type": "application/json" },
}
);
try {
const body = await req.json();
if (body.code) {
let response = await runSquiggle(body.code);
if (response.isOk) {
return new NextResponse(
JSON.stringify({
result: response.resultJSON,
bindings: response.bindingsJSON,
}),
{
status: 200,
statusText: "OK",
headers: { "Content-Type": "application/json" },
}
);
} else {
return new NextResponse(
JSON.stringify({ error: response.errorString }),
{
status: 400,
statusText: "ERROR",
headers: { "Content-Type": "application/json" },
}
);
}
} else {
return new NextResponse(JSON.stringify({ error: response.errorString }), {
return new NextResponse(JSON.stringify({ error: "No code provided" }), {
status: 400,
statusText: "ERROR",
headers: { "Content-Type": "application/json" },
});
}
} else {
return new NextResponse(JSON.stringify({ error: "No code provided" }), {
status: 400,
statusText: "ERROR",
headers: { "Content-Type": "application/json" },
});
} catch (error) {
// Catch any errors, including JSON parsing errors
console.error("Error in POST request:", error);
return new NextResponse(
//We could give more information here, but we don't want to leak any information
JSON.stringify({ error: "An internal error occurred" }),
{
status: 500,
statusText: "Internal Server Error",
headers: { "Content-Type": "application/json" },
}
);
}
}
16 changes: 16 additions & 0 deletions packages/hub/src/public/openapi-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,22 @@
}
}
}
},
"500": {
"description": "Internal Server Error.",
"content": {
"application/json": {
"schema": {
"type": "object",
"properties": {
"error": {
"type": "string",
"description": "A generic error message indicating a server problem."
}
}
}
}
}
}
}
}
Expand Down

0 comments on commit aefb449

Please sign in to comment.