Skip to content

Update error() and PageError types to include details property for more descriptive error messages #6669

@daleweaver777

Description

@daleweaver777

Describe the problem

When throwing an error, it would be nice to pass more info to the Error Page than just a single message property. Kit already allows me to pass an object to the error() function and access it in +error.svelte. However, I get type errors.

+page.server.ts

throw error(401, {
    message: 'Not Logged In',
    details: 'Please login to access this page.'
});

+error.svelte

<h1>
    {$paged.error?.message}
</h1>
<p>
    {$page.error?.details ?? ''}
</p>

Example Error Page

Screen Shot 2022-09-08 at 11 54 13 AM

Describe the proposed solution

Update types for PageError interface and error() function to include an optional property details.

ambient.d.ts

export interface PageError {
    message: string;
    details?: string;
}

index.d.ts

export function error(
    status: number,
    // this overload ensures you can omit the argument or pass in a string if App.PageError is of type { message: string }
    body?: { message: string, details?: string } extends App.PageError ? App.PageError | string | undefined : never
): HttpError;

Alternatives considered

Would be nice to include any # of optional properties but I'm not sure how to type that.

Importance

nice to have

Additional Information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    documentationImprovements or additions to documentation

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions