Incorrect response `status code` when throwing error from `+server.ts`
See original GitHub issueDescribe 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:
- Created a year ago
- Reactions:1
- Comments:8 (3 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
This is the intended behavior.
throw error()
orthrow redirect()
are intended for use inload
functions. In a+server.js
endpoint handler, you should just directly return theResponse
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.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 doreturn new Response('Shortcode not found', { status: 404 })
.