You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -264,7 +264,7 @@ export function GET({ url }) {
264
264
constd= max - min;
265
265
266
266
if (isNaN(d) || d <0) {
267
-
throwerror(400, 'min and max must be numbers, and min must be less than max');
267
+
error(400, 'min and max must be numbers, and min must be less than max');
268
268
}
269
269
270
270
constrandom= min +Math.random() * d;
@@ -277,7 +277,7 @@ The first argument to `Response` can be a [`ReadableStream`](https://developer.m
277
277
278
278
You can use the [`error`](modules#sveltejs-kit-error), [`redirect`](modules#sveltejs-kit-redirect) and [`json`](modules#sveltejs-kit-json) methods from `@sveltejs/kit` for convenience (but you don't have to).
279
279
280
-
If an error is thrown (either `throwerror(...)` or an unexpected error), the response will be a JSON representation of the error or a fallback error page — which can be customised via `src/error.html` — depending on the `Accept` header. The [`+error.svelte`](#error) component will _not_ be rendered in this case. You can read more about error handling [here](errors).
280
+
If an error is thrown (either `error(...)` or an unexpected error), the response will be a JSON representation of the error or a fallback error page — which can be customised via `src/error.html` — depending on the `Accept` header. The [`+error.svelte`](#error) component will _not_ be rendered in this case. You can read more about error handling [here](errors).
281
281
282
282
> When creating an `OPTIONS` handler, note that Vite will inject `Access-Control-Allow-Origin` and `Access-Control-Allow-Methods` headers — these will not be present in production unless you add them.
Copy file name to clipboardExpand all lines: documentation/docs/20-core-concepts/20-load.md
+4-4Lines changed: 4 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -395,11 +395,11 @@ import { error } from '@sveltejs/kit';
395
395
/**@type{import('./$types').LayoutServerLoad}*/
396
396
exportfunctionload({ locals }) {
397
397
if (!locals.user) {
398
-
throwerror(401, 'not logged in');
398
+
error(401, 'not logged in');
399
399
}
400
400
401
401
if (!locals.user.isAdmin) {
402
-
throwerror(403, 'not an admin');
402
+
error(403, 'not an admin');
403
403
}
404
404
}
405
405
```
@@ -428,12 +428,12 @@ import { redirect } from '@sveltejs/kit';
428
428
/**@type{import('./$types').LayoutServerLoad}*/
429
429
exportfunctionload({ locals }) {
430
430
if (!locals.user) {
431
-
throwredirect(307, '/login');
431
+
redirect(307, '/login');
432
432
}
433
433
}
434
434
```
435
435
436
-
> Don't use `throw redirect()`from within a try-catch block, as the redirect will immediately trigger the catch statement.
436
+
> Don't use `redirect()`inside a `try {...}` block, as the redirect will immediately trigger the catch statement.
437
437
438
438
In the browser, you can also navigate programmatically outside of a `load` function using [`goto`](modules#$app-navigation-goto) from [`$app.navigation`](modules#$app-navigation).
// we have to repeat the JSDoc because the display for function overloads is broken
20
+
// see https://github.com/microsoft/TypeScript/issues/55056
21
+
22
+
/**
23
+
* Throws an error with a HTTP status code and an optional message.
24
+
* When called during request handling, this will cause SvelteKit to
25
+
* return an error response without invoking `handleError`.
26
+
* Make sure you're not catching the thrown error, which would prevent SvelteKit from handling it.
27
+
* @param {NumericRange<400, 599>} status The [HTTP status code](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status#client_error_responses). Must be in the range 400-599.
28
+
* @param {App.Error} body An object that conforms to the App.Error type. If a string is passed, it will be used as the message property.
8
29
* @overload
9
-
* @param {number} status
30
+
* @param {NumericRange<400, 599>} status
10
31
* @param {App.Error} body
11
-
* @return {HttpError}
32
+
* @return {never}
33
+
* @throws {HttpError} This error instructs SvelteKit to initiate HTTP error handling.
34
+
* @throws {Error} If the provided status is invalid (not between 400 and 599).
12
35
*/
13
-
14
36
/**
37
+
* Throws an error with a HTTP status code and an optional message.
38
+
* When called during request handling, this will cause SvelteKit to
39
+
* return an error response without invoking `handleError`.
40
+
* Make sure you're not catching the thrown error, which would prevent SvelteKit from handling it.
41
+
* @param {NumericRange<400, 599>} status The [HTTP status code](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status#client_error_responses). Must be in the range 400-599.
42
+
* @param {{ message: string } extends App.Error ? App.Error | string | undefined : never} [body] An object that conforms to the App.Error type. If a string is passed, it will be used as the message property.
* @throws {HttpError} This error instructs SvelteKit to initiate HTTP error handling.
48
+
* @throws {Error} If the provided status is invalid (not between 400 and 599).
19
49
*/
20
-
21
50
/**
22
-
* Creates an `HttpError` object with an HTTP status code and an optional message.
23
-
* This object, if thrown during request handling, will cause SvelteKit to
51
+
* Throws an error with a HTTP status code and an optional message.
52
+
* When called during request handling, this will cause SvelteKit to
24
53
* return an error response without invoking `handleError`.
25
54
* Make sure you're not catching the thrown error, which would prevent SvelteKit from handling it.
26
-
* @param {number} status The [HTTP status code](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status#client_error_responses). Must be in the range 400-599.
55
+
* @param {NumericRange<400, 599>} status The [HTTP status code](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status#client_error_responses). Must be in the range 400-599.
27
56
* @param {{ message: string } extends App.Error ? App.Error | string | undefined : never} body An object that conforms to the App.Error type. If a string is passed, it will be used as the message property.
57
+
* @return {never}
58
+
* @throws {HttpError} This error instructs SvelteKit to initiate HTTP error handling.
59
+
* @throws {Error} If the provided status is invalid (not between 400 and 599).
thrownewError(`HTTP error status codes must be between 400 and 599 — ${status} is invalid`);
32
64
}
33
65
34
-
returnnewHttpError(status,body);
66
+
thrownewHttpError(status,body);
35
67
}
36
68
37
69
/**
38
-
* Create a `Redirect` object. If thrown during request handling, SvelteKit will return a redirect response.
70
+
* Checks whether this is an error thrown by {@link error}.
71
+
* @template {number} T
72
+
* @param {unknown} e
73
+
* @param {T} [status] The status to filter for.
74
+
* @return {e is (HttpError & { status: T extends undefined ? never : T })}
75
+
*/
76
+
exportfunctionisHttpError(e,status){
77
+
if(!(einstanceofHttpError))returnfalse;
78
+
return!status||e.status===status;
79
+
}
80
+
81
+
/**
82
+
* Redirect a request. When called during request handling, SvelteKit will return a redirect response.
39
83
* Make sure you're not catching the thrown redirect, which would prevent SvelteKit from handling it.
40
-
* @param {300 | 301 | 302 | 303 | 304 | 305 | 306 | 307 | 308} status The [HTTP status code](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status#redirection_messages). Must be in the range 300-308.
84
+
* @param {NumericRange<300, 308>} status The [HTTP status code](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status#redirection_messages). Must be in the range 300-308.
41
85
* @param {string | URL} location The location to redirect to.
86
+
* @throws {Redirect} This error instructs SvelteKit to redirect to the specified location.
87
+
* @throws {Error} If the provided status is invalid.
0 commit comments