sveltejs / sveltejs/kit

Update `error(4xx)` generic message, `Error: 4xx`

Open
#11,633 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

error handling
Dominant language
JavaScript
Stars
20.8k
Forks
2.3k
Avg merge
1d 16h
Merged PRs (30d)
156

Description

Describe the problem

Currently, SveltKit automatically generates a HTTP error message, if not provided.

https://github.com/sveltejs/kit/blob/22ed677df8ece41fa2ecc4687a0594bff0664f46/packages/kit/src/runtime/control.js#L13

To check if the error message is generated by SvelteKit, regex is necessary.

<script lang="ts">
  import { page } from '$app/stores';
</script>

<!-- Type guard -->
{#if $page.error}
  {@const isGenericMessage = /^Error: [45]\d{2}$/.test($page.error.message)}
  {@const message = !isGenericMessage ? $page.error.message : 'Unknown Error'}
  <h1>{$page.status}</h1>
  <p>{message}</p>
{/if}

If not, the +error.svelte example from the documentation renders this.

<!-- 403: Error: 403 -->
<h1>{$page.status}: {$page.error.message}</h1>
Describe the proposed solution
  1. Provide a blank message for distinction. (e.g. Error: 4xx → '')
  2. Better documentation for handling message in +error.svelte.
<!-- src/routes/+error.svelte -->

<h1>
  {$page.status}:
  {$page.error.message || 'custom error message, hopefully based on the HTTP status code.'}
</h1>

The message that accompanies an error(...) should ideally be understandable (or even actionable) by end users.

While the above statement https://github.com/sveltejs/kit/pull/11623#issuecomment-1889994428 is true,

  • Current Error: 4xx 5xx is not understandable for end users.
  • Providing a message for every generic error() is cumbersome.

To elaborate on the latter, consider this use-case.

To access the user data in server load functions, the event.locals has to be checked every single time.

// src/routes/admin/+page.server.ts
// and many other *.server.ts files.

import { error } from '@sveltejs/kit';

export const load = async ({ locals }) => {
  if (!locals.auth) error(401);
  if (!locals.auth.isAdmin) error(403);
};

Providing a consistent error message every single time error() is called is rather impossible.

The following is an example from the SvelteKit documentation.

import { error } from '@sveltejs/kit';

/** @type {import('./$types').LayoutServerLoad} */
export function load({ locals }) {
  if (!locals.user) {
    error(401, 'not logged in');
  }
  if (!locals.user.isAdmin) {
    error(403, 'not an admin');
  }
}
Alternatives considered
  1. https://github.com/sveltejs/kit/pull/11623
Importance

would make my life easier

Additional Information

Non-existing route generates a Not Found message, which is not consistent.

https://github.com/sveltejs/kit/blob/22ed677df8ece41fa2ecc4687a0594bff0664f46/packages/kit/src/runtime/server/respond.js#L499

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with packages/kit/src/runtime/control.js, then compare the missing-route handling in packages/kit/src/runtime/server/respond.js and the linked error documentation. Check existing tests for generic HTTP errors and routing errors before deciding how the default message should be represented. Done means the behavior is consistent and the +error.svelte guidance explains how to handle an absent message.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, typescript
Domain
web-dev
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.