Skip to content

Commit d06affc

Browse files
teemingcChew Tee MingRich-Harrisbenmccann
authored
chore: change error helpers to use public interface types (#13036)
closes #12986 This PR changes the `error`, `isHttpError`, `redirect`, and `isRedirect` functions to use the public interface version of the `HttpError` and `Redirect` errors instead of our internal classes. --- ### Please don't delete this checklist! Before submitting the PR, please make sure you do the following: - [x] It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs - [x] This message body should clearly illustrate what problems it solves. - [ ] Ideally, include a test that fails without this PR but passes with it. ### Tests - [ ] Run the tests with `pnpm test` and lint the project with `pnpm lint` and `pnpm check` ### Changesets - [x] If your PR makes a change that should be noted in one or more packages' changelogs, generate a changeset by running `pnpm changeset` and following the prompts. Changesets that add features should be `minor` and those that fix bugs should be `patch`. Please prefix changeset messages with `feat:`, `fix:`, or `chore:`. ### Edits - [x] Please ensure that 'Allow edits from maintainers' is checked. PRs without this option may be closed. --------- Co-authored-by: Chew Tee Ming <chew.tee.ming@nindatech.com> Co-authored-by: Rich Harris <rich.harris@vercel.com> Co-authored-by: Ben McCann <322311+benmccann@users.noreply.github.com>
1 parent df6cf79 commit d06affc

3 files changed

Lines changed: 16 additions & 26 deletions

File tree

.changeset/light-singers-lie.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/kit': major
3+
---
4+
5+
chore: change `error`, `isHttpError`, `redirect`, and `isRedirect` to refer to public type instead of internal class

packages/kit/src/exports/index.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export { VERSION } from '../version.js';
4444
* @param {number} status
4545
* @param {App.Error} body
4646
* @return {never}
47-
* @throws {HttpError} This error instructs SvelteKit to initiate HTTP error handling.
47+
* @throws {import('./public.js').HttpError} This error instructs SvelteKit to initiate HTTP error handling.
4848
* @throws {Error} If the provided status is invalid (not between 400 and 599).
4949
*/
5050
/**
@@ -58,7 +58,7 @@ export { VERSION } from '../version.js';
5858
* @param {number} status
5959
* @param {{ message: string } extends App.Error ? App.Error | string | undefined : never} [body]
6060
* @return {never}
61-
* @throws {HttpError} This error instructs SvelteKit to initiate HTTP error handling.
61+
* @throws {import('./public.js').HttpError} This error instructs SvelteKit to initiate HTTP error handling.
6262
* @throws {Error} If the provided status is invalid (not between 400 and 599).
6363
*/
6464
/**
@@ -69,7 +69,7 @@ export { VERSION } from '../version.js';
6969
* @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.
7070
* @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.
7171
* @return {never}
72-
* @throws {HttpError} This error instructs SvelteKit to initiate HTTP error handling.
72+
* @throws {import('./public.js').HttpError} This error instructs SvelteKit to initiate HTTP error handling.
7373
* @throws {Error} If the provided status is invalid (not between 400 and 599).
7474
*/
7575
export function error(status, body) {
@@ -85,7 +85,7 @@ export function error(status, body) {
8585
* @template {number} T
8686
* @param {unknown} e
8787
* @param {T} [status] The status to filter for.
88-
* @return {e is (HttpError & { status: T extends undefined ? never : T })}
88+
* @return {e is (import('./public.js').HttpError & { status: T extends undefined ? never : T })}
8989
*/
9090
export function isHttpError(e, status) {
9191
if (!(e instanceof HttpError)) return false;
@@ -105,7 +105,7 @@ export function isHttpError(e, status) {
105105
*
106106
* @param {300 | 301 | 302 | 303 | 304 | 305 | 306 | 307 | 308 | ({} & number)} status The [HTTP status code](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status#redirection_messages). Must be in the range 300-308.
107107
* @param {string | URL} location The location to redirect to.
108-
* @throws {Redirect} This error instructs SvelteKit to redirect to the specified location.
108+
* @throws {import('./public.js').Redirect} This error instructs SvelteKit to redirect to the specified location.
109109
* @throws {Error} If the provided status is invalid.
110110
* @return {never}
111111
*/
@@ -124,7 +124,7 @@ export function redirect(status, location) {
124124
/**
125125
* Checks whether this is a redirect thrown by {@link redirect}.
126126
* @param {unknown} e The object to check.
127-
* @return {e is Redirect}
127+
* @return {e is import('./public.js').Redirect}
128128
*/
129129
export function isRedirect(e) {
130130
return e instanceof Redirect;

packages/kit/types/index.d.ts

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2640,7 +2640,7 @@ declare module '@sveltejs/kit' {
26402640
* Make sure you're not catching the thrown error, which would prevent SvelteKit from handling it.
26412641
* @param 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.
26422642
* @param body An object that conforms to the App.Error type. If a string is passed, it will be used as the message property.
2643-
* @throws {HttpError} This error instructs SvelteKit to initiate HTTP error handling.
2643+
* @throws {import('./public.js').HttpError} This error instructs SvelteKit to initiate HTTP error handling.
26442644
* @throws {Error} If the provided status is invalid (not between 400 and 599).
26452645
*/
26462646
export function error(status: number, body: App.Error): never;
@@ -2651,7 +2651,7 @@ declare module '@sveltejs/kit' {
26512651
* Make sure you're not catching the thrown error, which would prevent SvelteKit from handling it.
26522652
* @param 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.
26532653
* @param body An object that conforms to the App.Error type. If a string is passed, it will be used as the message property.
2654-
* @throws {HttpError} This error instructs SvelteKit to initiate HTTP error handling.
2654+
* @throws {import('./public.js').HttpError} This error instructs SvelteKit to initiate HTTP error handling.
26552655
* @throws {Error} If the provided status is invalid (not between 400 and 599).
26562656
*/
26572657
export function error(status: number, body?: {
@@ -2661,7 +2661,7 @@ declare module '@sveltejs/kit' {
26612661
* Checks whether this is an error thrown by {@link error}.
26622662
* @param status The status to filter for.
26632663
* */
2664-
export function isHttpError<T extends number>(e: unknown, status?: T): e is (HttpError_1 & {
2664+
export function isHttpError<T extends number>(e: unknown, status?: T): e is (HttpError & {
26652665
status: T extends undefined ? never : T;
26662666
});
26672667
/**
@@ -2677,15 +2677,15 @@ declare module '@sveltejs/kit' {
26772677
*
26782678
* @param status The [HTTP status code](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status#redirection_messages). Must be in the range 300-308.
26792679
* @param location The location to redirect to.
2680-
* @throws {Redirect} This error instructs SvelteKit to redirect to the specified location.
2680+
* @throws {import('./public.js').Redirect} This error instructs SvelteKit to redirect to the specified location.
26812681
* @throws {Error} If the provided status is invalid.
26822682
* */
26832683
export function redirect(status: 300 | 301 | 302 | 303 | 304 | 305 | 306 | 307 | 308 | ({} & number), location: string | URL): never;
26842684
/**
26852685
* Checks whether this is a redirect thrown by {@link redirect}.
26862686
* @param e The object to check.
26872687
* */
2688-
export function isRedirect(e: unknown): e is Redirect_1;
2688+
export function isRedirect(e: unknown): e is Redirect;
26892689
/**
26902690
* Create a JSON `Response` object from the supplied data.
26912691
* @param data The value that will be serialized as JSON.
@@ -2767,21 +2767,6 @@ declare module '@sveltejs/kit' {
27672767
export type LessThan<TNumber extends number, TArray extends any[] = []> = TNumber extends TArray["length"] ? TArray[number] : LessThan<TNumber, [...TArray, TArray["length"]]>;
27682768
export type NumericRange<TStart extends number, TEnd extends number> = Exclude<TEnd | LessThan<TEnd>, LessThan<TStart>>;
27692769
export const VERSION: string;
2770-
class HttpError_1 {
2771-
2772-
constructor(status: number, body: {
2773-
message: string;
2774-
} extends App.Error ? (App.Error | string | undefined) : App.Error);
2775-
status: number;
2776-
body: App.Error;
2777-
toString(): string;
2778-
}
2779-
class Redirect_1 {
2780-
2781-
constructor(status: 300 | 301 | 302 | 303 | 304 | 305 | 306 | 307 | 308, location: string);
2782-
status: 300 | 301 | 302 | 303 | 304 | 305 | 306 | 307 | 308;
2783-
location: string;
2784-
}
27852770

27862771
export {};
27872772
}

0 commit comments

Comments
 (0)