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

chore: change error helpers to use public interface types #13036

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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/light-singers-lie.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': major
---

chore: change `error`, `isHttpError`, `redirect`, and `isRedirect` to refer to public type instead of internal class
12 changes: 6 additions & 6 deletions packages/kit/src/exports/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export { VERSION } from '../version.js';
* @param {number} status
* @param {App.Error} body
* @return {never}
* @throws {HttpError} This error instructs SvelteKit to initiate HTTP error handling.
* @throws {import('./public.js').HttpError} This error instructs SvelteKit to initiate HTTP error handling.
* @throws {Error} If the provided status is invalid (not between 400 and 599).
*/
/**
Expand All @@ -47,7 +47,7 @@ export { VERSION } from '../version.js';
* @param {number} status
* @param {{ message: string } extends App.Error ? App.Error | string | undefined : never} [body]
* @return {never}
* @throws {HttpError} This error instructs SvelteKit to initiate HTTP error handling.
* @throws {import('./public.js').HttpError} This error instructs SvelteKit to initiate HTTP error handling.
* @throws {Error} If the provided status is invalid (not between 400 and 599).
*/
/**
Expand All @@ -58,7 +58,7 @@ export { VERSION } from '../version.js';
* @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.
* @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.
* @return {never}
* @throws {HttpError} This error instructs SvelteKit to initiate HTTP error handling.
* @throws {import('./public.js').HttpError} This error instructs SvelteKit to initiate HTTP error handling.
* @throws {Error} If the provided status is invalid (not between 400 and 599).
*/
export function error(status, body) {
Expand All @@ -74,7 +74,7 @@ export function error(status, body) {
* @template {number} T
* @param {unknown} e
* @param {T} [status] The status to filter for.
* @return {e is (HttpError & { status: T extends undefined ? never : T })}
* @return {e is (import('./public.js').HttpError & { status: T extends undefined ? never : T })}
*/
export function isHttpError(e, status) {
if (!(e instanceof HttpError)) return false;
Expand All @@ -86,7 +86,7 @@ export function isHttpError(e, status) {
* Make sure you're not catching the thrown redirect, which would prevent SvelteKit from handling it.
* @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.
* @param {string | URL} location The location to redirect to.
* @throws {Redirect} This error instructs SvelteKit to redirect to the specified location.
* @throws {import('./public.js').Redirect} This error instructs SvelteKit to redirect to the specified location.
* @throws {Error} If the provided status is invalid.
* @return {never}
*/
Expand All @@ -105,7 +105,7 @@ export function redirect(status, location) {
/**
* Checks whether this is a redirect thrown by {@link redirect}.
* @param {unknown} e The object to check.
* @return {e is Redirect}
* @return {e is import('./public.js').Redirect}
*/
export function isRedirect(e) {
return e instanceof Redirect;
Expand Down
25 changes: 5 additions & 20 deletions packages/kit/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1765,7 +1765,7 @@ declare module '@sveltejs/kit' {
* Make sure you're not catching the thrown error, which would prevent SvelteKit from handling it.
* @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.
* @param 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.
* @throws {import('./public.js').HttpError} This error instructs SvelteKit to initiate HTTP error handling.
* @throws {Error} If the provided status is invalid (not between 400 and 599).
*/
export function error(status: number, body: App.Error): never;
Expand All @@ -1776,7 +1776,7 @@ declare module '@sveltejs/kit' {
* Make sure you're not catching the thrown error, which would prevent SvelteKit from handling it.
* @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.
* @param 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.
* @throws {import('./public.js').HttpError} This error instructs SvelteKit to initiate HTTP error handling.
* @throws {Error} If the provided status is invalid (not between 400 and 599).
*/
export function error(status: number, body?: {
Expand All @@ -1786,23 +1786,23 @@ declare module '@sveltejs/kit' {
* Checks whether this is an error thrown by {@link error}.
* @param status The status to filter for.
* */
export function isHttpError<T extends number>(e: unknown, status?: T | undefined): e is HttpError_1 & {
export function isHttpError<T extends number>(e: unknown, status?: T | undefined): e is HttpError & {
status: T extends undefined ? never : T;
};
/**
* Redirect a request. When called during request handling, SvelteKit will return a redirect response.
* Make sure you're not catching the thrown redirect, which would prevent SvelteKit from handling it.
* @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.
* @param location The location to redirect to.
* @throws {Redirect} This error instructs SvelteKit to redirect to the specified location.
* @throws {import('./public.js').Redirect} This error instructs SvelteKit to redirect to the specified location.
* @throws {Error} If the provided status is invalid.
* */
export function redirect(status: 300 | 301 | 302 | 303 | 304 | 305 | 306 | 307 | 308 | ({} & number), location: string | URL): never;
/**
* Checks whether this is a redirect thrown by {@link redirect}.
* @param e The object to check.
* */
export function isRedirect(e: unknown): e is Redirect_1;
export function isRedirect(e: unknown): e is Redirect;
/**
* Create a JSON `Response` object from the supplied data.
* @param data The value that will be serialized as JSON.
Expand Down Expand Up @@ -1834,21 +1834,6 @@ declare module '@sveltejs/kit' {
export type LessThan<TNumber extends number, TArray extends any[] = []> = TNumber extends TArray['length'] ? TArray[number] : LessThan<TNumber, [...TArray, TArray['length']]>;
export type NumericRange<TStart extends number, TEnd extends number> = Exclude<TEnd | LessThan<TEnd>, LessThan<TStart>>;
export const VERSION: string;
class HttpError_1 {

constructor(status: number, body: {
message: string;
} extends App.Error ? (App.Error | string | undefined) : App.Error);
status: number;
body: App.Error;
toString(): string;
}
class Redirect_1 {

constructor(status: 300 | 301 | 302 | 303 | 304 | 305 | 306 | 307 | 308, location: string);
status: 301 | 302 | 303 | 307 | 308 | 300 | 304 | 305 | 306;
location: string;
}

export {};
}
Expand Down
Loading