Skip to content

Commit 99b8079

Browse files
fix: allow HandleServerError hook to access the getRequestEvent (#13666)
* fix: allow usage of `getRequestEvent` inside `handleError` * chore: format and add changeset * fix: add test for `getReqeuestEvent` in `handleError` * Update packages/kit/types/index.d.ts * Update packages/kit/src/runtime/app/server/event.js * Update packages/kit/src/runtime/app/server/event.js --------- Co-authored-by: Tee Ming <[email protected]>
1 parent e4c672f commit 99b8079

File tree

8 files changed

+30
-4
lines changed

8 files changed

+30
-4
lines changed

.changeset/ninety-planets-remember.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/kit': patch
3+
---
4+
5+
allow `HandleServerError` hook to access `getRequestEvent`

packages/kit/src/runtime/app/server/event.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import('node:async_hooks')
1515
});
1616

1717
/**
18-
* Returns the current `RequestEvent`. Can be used inside `handle`, `load` and actions (and functions called by them).
18+
* Returns the current `RequestEvent`. Can be used inside server hooks, server `load` functions, actions, and endpoints (and functions called by them).
1919
*
2020
* In environments without [`AsyncLocalStorage`](https://nodejs.org/api/async_context.html#class-asynclocalstorage), this must be called synchronously (i.e. not after an `await`).
2121
* @since 2.20.0
@@ -25,7 +25,7 @@ export function getRequestEvent() {
2525

2626
if (!event) {
2727
let message =
28-
'Can only read the current request event inside functions invoked during `handle`, such as server `load` functions, actions, and server endpoints.';
28+
'Can only read the current request event inside functions invoked during `handle`, such as server `load` functions, actions, endpoints, and other server hooks.';
2929

3030
if (!als) {
3131
message +=

packages/kit/src/runtime/server/utils.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { HttpError } from '../control.js';
66
import { fix_stack_trace } from '../shared-server.js';
77
import { ENDPOINT_METHODS } from '../../constants.js';
88
import { escape_html } from '../../utils/escape.js';
9+
import { with_event } from '../app/server/event.js';
910

1011
/** @param {any} body */
1112
export function is_pojo(body) {
@@ -107,7 +108,11 @@ export async function handle_error_and_jsonify(event, options, error) {
107108
const status = get_status(error);
108109
const message = get_message(error);
109110

110-
return (await options.hooks.handleError({ error, event, status, message })) ?? { message };
111+
return (
112+
(await with_event(event, () =>
113+
options.hooks.handleError({ error, event, status, message })
114+
)) ?? { message }
115+
);
111116
}
112117

113118
/**

packages/kit/test/apps/basics/src/hooks.server.js

+5
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ export const handleError = ({ event, error: e, status, message }) => {
3838
errors[event.url.pathname] = error_to_pojo(error);
3939
fs.writeFileSync('test/errors.json', JSON.stringify(errors));
4040

41+
if (event.url.pathname.startsWith('/get-request-event/')) {
42+
const ev = getRequestEvent();
43+
message = ev.locals.message;
44+
}
45+
4146
return event.url.pathname.endsWith('404-fallback')
4247
? undefined
4348
: { message: `${error.message} (${status} ${message})` };
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<script>
2+
import { page } from '$app/state';
3+
</script>
4+
5+
<h1>{page.error.message}</h1>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export const load = async () => {
2+
throw new Error('Crashing now');
3+
};

packages/kit/test/apps/basics/test/test.js

+3
Original file line numberDiff line numberDiff line change
@@ -1544,5 +1544,8 @@ test.describe('getRequestEvent', () => {
15441544
await page.click('button');
15451545

15461546
expect(await page.textContent('h1')).toBe('from form: hello');
1547+
1548+
await page.goto('/get-request-event/with-error');
1549+
expect(await page.textContent('h1')).toBe('Crashing now (500 hello from hooks.server.js)');
15471550
});
15481551
});

packages/kit/types/index.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -2417,7 +2417,7 @@ declare module '$app/server' {
24172417
*/
24182418
export function read(asset: string): Response;
24192419
/**
2420-
* Returns the current `RequestEvent`. Can be used inside `handle`, `load` and actions (and functions called by them).
2420+
* Returns the current `RequestEvent`. Can be used inside server hooks, server `load` functions, actions, and endpoints (and functions called by them).
24212421
*
24222422
* In environments without [`AsyncLocalStorage`](https://nodejs.org/api/async_context.html#class-asynclocalstorage), this must be called synchronously (i.e. not after an `await`).
24232423
* @since 2.20.0

0 commit comments

Comments
 (0)