question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:1
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
Conduitrycommented, Aug 17, 2022

This is the intended behavior. throw error() or throw redirect() are intended for use in load functions. In a +server.js endpoint handler, you should just directly return the Response object with whatever error response you want. Any thrown exception results in a 500.

Perhaps there could be a dev mode warning that makes this more clear - but I don’t think we want to start having special handling for certain thrown errors in +server.js files.

0reactions
dummdidummcommented, Aug 18, 2022

Since we agreed that error and redirect are valid to throw in +server.js, this should work. However, the obeserved behavior (nearest error page is rendered, page is shown) is also wrong (as a consequence of this bug) in my opinion. Instead you should get a blank page with just the error message. If you are requesting an endpoint directly, it’s not something that is part of the page - and you get the same behavior if you do return new Response('Shortcode not found', { status: 404 }).

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to deal with http status codes other than 200 in Angular 2
Yes you can handle with the catch operator like this and show alert as you want but firstly you have to import Rxjs...
Read more >
Error handling - Apollo GraphQL Docs
There are three ways to change an HTTP status code or set custom response headers, you can: throw an error in a resolver,...
Read more >
HTTP response status codes - MDN Web Docs - Mozilla
HTTP response status codes indicate whether a specific HTTP request has been successfully completed. Responses are grouped in five classes:
Read more >
HTTP Status Codes List | HTTP Error Codes Explained
Learn about all the HTTP status codes. Read about the HTTP status codes and their descriptions. Quickly understand client and server errors.
Read more >
400 Bad Request Error: What It Is and How to Fix It
The 400 Bad Request Error is an HTTP response status code indicating that the server was unable to process the request sent by...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found