-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Catch all errors in runSquiggle, give 500 if server error
- Loading branch information
Showing
2 changed files
with
57 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" }, | ||
} | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters