Skip to content

Incorrect response status code when throwing error from +server.ts #5993

@daleweaver777

Description

@daleweaver777

Describe the bug

When throwing an error from a +server.ts route, the status code in the response header is set to 500 instead of the status code specified in the error() helper. The nearest Error Boundary does render and the error property in the $page store does have the correct status code, however the response header doesn't match.

Reproduction

+server.ts

import { error } from '@sveltejs/kit';
import type { RequestHandler } from './$types';

const shortcodes: { [index: string]: string } = {
	microsoft: 'https://www.microsoft.com',
	google: 'https://www.google.com'
};

export const GET: RequestHandler = ({ params }) => {
	if (!params.shortcode || !shortcodes[params.shortcode]) {
		throw error(404, 'Shortcode not found');
	}

	return Response.redirect(shortcodes[params.shortcode], 301);
};

Response Header

Status Code: 500 Internal Server Error

$page store

{
  error: HttpError {
    name: 'HttpError',
    stack: undefined,
    status: 404,
    message: 'Shortcode Not Found'
  },
  params: { shortcode: 'test' },
  routeId: '[shortcode]',
  status: 500,
  url: URL {
    href: 'http://localhost:5173/test',
    origin: 'http://localhost:5173',
    protocol: 'http:',
    username: '',
    password: '',
    host: 'localhost:5173',
    hostname: 'localhost',
    port: '5173',
    pathname: '/test',
    search: '',
    searchParams: URLSearchParams {},
    hash: ''
  },
  data: {}
}

Logs

No response

System Info

System:
    OS: macOS 12.5
    CPU: (10) arm64 Apple M1 Max
    Memory: 5.49 GB / 32.00 GB
    Shell: 5.8.1 - /bin/zsh
  Binaries:
    Node: 18.7.0 - /opt/homebrew/bin/node
    npm: 8.16.0 - /opt/homebrew/bin/npm
  Browsers:
    Brave Browser: 104.1.42.95
    Chrome: 104.0.5112.79
    Firefox Developer Edition: 104.0
    Safari: 15.6
    Safari Technology Preview: 16.0
  npmPackages:
    @sveltejs/adapter-auto: next => 1.0.0-next.64
    @sveltejs/kit: next => 1.0.0-next.417
    svelte: ^3.44.0 => 3.49.0
    vite: ^3.0.0 => 3.0.8

Severity

serious, but I can work around it

Additional Information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingp1-importantSvelteKit cannot be used by a large number of people, basic functionality is missing, etc.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions