Skip to content

Commit

Permalink
fix: distinguish better between not found and internal error (#11131)
Browse files Browse the repository at this point in the history
Create dedicated NotFound error class for this
  • Loading branch information
hjhopp authored Dec 12, 2023
1 parent 08982fc commit 1b1274f
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 14 deletions.
5 changes: 5 additions & 0 deletions .changeset/warm-otters-compete.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

fix: distinguish better between not-found and internal-error
7 changes: 5 additions & 2 deletions packages/kit/src/runtime/client/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import * as devalue from 'devalue';
import { compact } from '../../utils/array.js';
import { validate_page_exports } from '../../utils/exports.js';
import { unwrap_promises } from '../../utils/promises.js';
import { HttpError, Redirect } from '../control.js';
import { HttpError, Redirect, NotFound } from '../control.js';
import { INVALIDATED_PARAM, TRAILING_SLASH_PARAM, validate_depends } from '../shared.js';
import { INDEX_KEY, PRELOAD_PRIORITIES, SCROLL_KEY, SNAPSHOT_KEY } from './constants.js';
import { stores } from './singletons.js';
Expand Down Expand Up @@ -1331,7 +1331,10 @@ export function create_client(app, target) {

return (
app.hooks.handleError({ error, event }) ??
/** @type {any} */ ({ message: event.route.id != null ? 'Internal Error' : 'Not Found' })
/** @type {any} */ ({
message:
event.route.id === null && error instanceof NotFound ? 'Not Found' : 'Internal Error'
})
);
}

Expand Down
12 changes: 12 additions & 0 deletions packages/kit/src/runtime/control.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,18 @@ export class Redirect {
}
}

export class NotFound extends Error {
/**
* @param {string} pathname
*/
constructor(pathname) {
super();

this.status = 404;
this.message = `Not found: ${pathname}`;
}
}

/**
* @template {Record<string, unknown> | undefined} [T=undefined]
*/
Expand Down
4 changes: 2 additions & 2 deletions packages/kit/src/runtime/server/respond.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { exec } from '../../utils/routing.js';
import { redirect_json_response, render_data } from './data/index.js';
import { add_cookies_to_headers, get_cookies } from './cookie.js';
import { create_fetch } from './fetch.js';
import { Redirect } from '../control.js';
import { Redirect, NotFound } from '../control.js';
import {
validate_layout_exports,
validate_layout_server_exports,
Expand Down Expand Up @@ -480,7 +480,7 @@ export async function respond(request, options, manifest, state) {
manifest,
state,
status: 404,
error: new Error(`Not found: ${event.url.pathname}`),
error: new NotFound(event.url.pathname),
resolve_opts
});
}
Expand Down
20 changes: 10 additions & 10 deletions packages/kit/src/runtime/server/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { DEV } from 'esm-env';
import { json, text } from '../../exports/index.js';
import { coalesce_to_error } from '../../utils/error.js';
import { negotiate } from '../../utils/http.js';
import { HttpError } from '../control.js';
import { HttpError, NotFound } from '../control.js';
import { fix_stack_trace } from '../shared-server.js';
import { ENDPOINT_METHODS } from '../../constants.js';

Expand Down Expand Up @@ -97,17 +97,17 @@ export async function handle_fatal_error(event, options, error) {
export async function handle_error_and_jsonify(event, options, error) {
if (error instanceof HttpError) {
return error.body;
} else {
if (__SVELTEKIT_DEV__ && typeof error == 'object') {
fix_stack_trace(error);
}
}

return (
(await options.hooks.handleError({ error, event })) ?? {
message: event.route.id != null ? 'Internal Error' : 'Not Found'
}
);
if (__SVELTEKIT_DEV__ && typeof error == 'object') {
fix_stack_trace(error);
}

return (
(await options.hooks.handleError({ error, event })) ?? {
message: event.route.id === null && error instanceof NotFound ? 'Not Found' : 'Internal Error'
}
);
}

/**
Expand Down

0 comments on commit 1b1274f

Please sign in to comment.