Skip to content

Commit

Permalink
qol(typed api): manually catch errors in dev mode (#81)
Browse files Browse the repository at this point in the history
* qol(typed api): manually catch errors in dev mode

* add changeset
  • Loading branch information
lilnasy committed Feb 6, 2024
1 parent 903e201 commit 07166b4
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/six-dancers-peel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"astro-typed-api": patch
---

Prevents error overlay from appearing in dev mode for user errors. Astro's error overlay appears whenever a server side error occurs. For server-side rendering html, it's important to pay attention to them. However, for APIs, an error is just another response - it is unintended for it to take over the browser.
12 changes: 11 additions & 1 deletion packages/typed-api/runtime/server-internals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,17 @@ export function createApiRoute(fetch: (input: unknown, content: TypedAPIContext)
output = await fetch(input, context)
} catch (error) {
if (error instanceof TypedAPIError) throw error
throw new ProcedureFailed(error, url)
const procedureFailed = new ProcedureFailed(error, url)
if (import.meta.env.DEV) {
// some errors are thrown intentionally
// until a full error handling api is implemented, manually return 500 responses
// to avoid dev overlay taking over the browser
console.error(procedureFailed)
return new Response(null, { status: 500 })
}
else {
throw procedureFailed
}
}

let outputBody
Expand Down

0 comments on commit 07166b4

Please sign in to comment.