Skip to content

Commit

Permalink
[breaking] render raw response when unexpected error occurs in endpoint
Browse files Browse the repository at this point in the history
..instead of failing completely. Also catch possible fetch error (which would occur when network fails).
Closes #4801

There are still some code paths which would bubble up to the handler, but these are of the kind "you use a wrong SvelteKit option"
  • Loading branch information
dummdidumm committed Aug 30, 2022
1 parent 863a546 commit 0cb18cd
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/few-ghosts-attend.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

[breaking] catch and render raw response when unexpected error occurs in endpoint
2 changes: 1 addition & 1 deletion packages/kit/src/runtime/server/endpoint.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export async function render_endpoint(event, mod, state) {
headers: { Location: error.location }
});
} else {
throw error;
return new Response(/** @type {Error} */ (error)?.message || 'Error', { status: 500 });
}
}
}
6 changes: 5 additions & 1 deletion packages/kit/src/runtime/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,11 @@ export async function respond(request, options, state) {

// we can't load the endpoint from our own manifest,
// so we need to make an actual HTTP request
return await fetch(request);
try {
return await fetch(request);
} catch (e) {
return new Response(coalesce_to_error(e).message, { status: 500 });
}
},

// TODO remove for 1.0
Expand Down
2 changes: 1 addition & 1 deletion packages/kit/src/runtime/server/page/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ export async function render_page(event, route, page, options, state, resolve_op
});
} catch (error) {
// if we end up here, it means the data loaded successfull
// but the page failed to render
// but the page failed to render, or that a prerendering error occurred
options.handle_error(/** @type {Error} */ (error), event);

return await respond_with_error({
Expand Down

0 comments on commit 0cb18cd

Please sign in to comment.