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

prefer JSON error responses over HTML in fetch case #6497

Merged
merged 3 commits into from
Sep 1, 2022
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
5 changes: 5 additions & 0 deletions .changeset/odd-months-grow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

Prefer JSON responses when returning errors if accept header is `*/*`
4 changes: 2 additions & 2 deletions packages/kit/src/runtime/server/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,8 @@ export function handle_fatal_error(event, options, error) {

// ideally we'd use sec-fetch-dest instead, but Safari — quelle surprise — doesn't support it
const type = negotiate(event.request.headers.get('accept') || 'text/html', [
'text/html',
'application/json'
'application/json',
'text/html'
]);

if (event.url.pathname.endsWith(DATA_SUFFIX) || type === 'application/json') {
Expand Down
75 changes: 62 additions & 13 deletions packages/kit/test/apps/basics/test/server.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,15 +149,39 @@ test.describe('Errors', () => {
});

test('throw error(...) in endpoint', async ({ request, read_errors }) => {
const res = await request.get('/errors/endpoint-throw-error');
// HTML
{
const res = await request.get('/errors/endpoint-throw-error', {
headers: {
accept: 'text/html'
}
});

const error = read_errors('/errors/endpoint-throw-error');
expect(error).toBe(undefined);

expect(res.status()).toBe(401);
expect(await res.text()).toContain(
'This is the static error page with the following message: You shall not pass'
);
}

const error = read_errors('/errors/endpoint-throw-error');
expect(error).toBe(undefined);
// JSON (default)
{
const res = await request.get('/errors/endpoint-throw-error');

expect(await res?.text()).toContain(
'This is the static error page with the following message: You shall not pass'
);
expect(res?.status()).toBe(401);
const error = read_errors('/errors/endpoint-throw-error');
expect(error).toBe(undefined);

expect(res.status()).toBe(401);
expect(await res.json()).toEqual({
status: 401,
message: 'You shall not pass',

// TODO this is gross, fix it
__is_http_error: true
});
}
});

test('throw redirect(...) in endpoint', async ({ page, read_errors }) => {
Expand All @@ -170,13 +194,38 @@ test.describe('Errors', () => {
expect(await page.textContent('h1')).toBe('the answer is 42');
});

test('error thrown in handle results in a rendered error page', async ({ request }) => {
const res = await request.get('/errors/error-in-handle');
test('error thrown in handle results in a rendered error page or JSON response', async ({
request
}) => {
// HTML
{
const res = await request.get('/errors/error-in-handle', {
headers: {
accept: 'text/html'
}
});

expect(res.status()).toBe(500);
expect(await res.text()).toContain(
'This is the static error page with the following message: Error in handle'
);
}

// JSON (default)
{
const res = await request.get('/errors/error-in-handle');

expect(await res.text()).toContain(
'This is the static error page with the following message: Error in handle'
);
expect(res.status()).toBe(500);
const error = await res.json();

expect(typeof error.stack).toBe('string');
delete error.stack;

expect(res.status()).toBe(500);
expect(error).toEqual({
name: 'Error',
message: 'Error in handle'
});
}
});
});

Expand Down