Skip to content

Commit

Permalink
[fix] Handle throw error/redirect in +server.js (#6028)
Browse files Browse the repository at this point in the history
Fixes #5993
  • Loading branch information
dummdidumm authored Aug 18, 2022
1 parent b882e1c commit e9b8c65
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .changeset/honest-parrots-compare.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

Handle `throw error/redirect` in `+server.js`
34 changes: 24 additions & 10 deletions packages/kit/src/runtime/server/endpoint.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { HttpError, Redirect } from '../../index/private.js';
import { check_method_names, method_not_allowed } from './utils.js';

/**
Expand Down Expand Up @@ -29,16 +30,29 @@ export async function render_endpoint(event, route) {
return method_not_allowed(mod, method);
}

const response = await handler(
/** @type {import('types').RequestEvent<Record<string, any>>} */ (event)
);

if (!(response instanceof Response)) {
return new Response(
`Invalid response from route ${event.url.pathname}: handler should return a Response object`,
{ status: 500 }
try {
const response = await handler(
/** @type {import('types').RequestEvent<Record<string, any>>} */ (event)
);
}

return response;
if (!(response instanceof Response)) {
return new Response(
`Invalid response from route ${event.url.pathname}: handler should return a Response object`,
{ status: 500 }
);
}

return response;
} catch (error) {
if (error instanceof HttpError) {
return new Response(error.message, { status: error.status });
} else if (error instanceof Redirect) {
return new Response(undefined, {
status: error.status,
headers: { Location: error.location }
});
} else {
throw error;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { error } from '@sveltejs/kit';

export function GET() {
throw error(401, 'You shall not pass');
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { redirect } from '@sveltejs/kit';

export function GET() {
throw redirect(302, '/');
}
20 changes: 20 additions & 0 deletions packages/kit/test/apps/basics/test/server.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,26 @@ test.describe('Errors', () => {
'This is your custom error page saying: "Cannot read properties of undefined (reading \'toUpperCase\')"'
);
});

test('throw error(..) in endpoint', async ({ page, read_errors }) => {
const res = await page.goto('/errors/endpoint-throw-error');

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

expect(await res?.text()).toBe('You shall not pass');
expect(res?.status()).toBe(401);
});

test('throw redirect(..) in endpoint', async ({ page, read_errors }) => {
const res = await page.goto('/errors/endpoint-throw-redirect');
expect(res?.status()).toBe(200); // redirects are opaque to the browser

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

expect(await page.textContent('h1')).toBe('the answer is 42');
});
});

test.describe('Load', () => {
Expand Down

0 comments on commit e9b8c65

Please sign in to comment.