@sveltejs/kit 2.34.1 → 2.35.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/runtime/server/index.js +9 -2
- package/src/runtime/server/utils.js +55 -1
- package/src/version.js +1 -1
package/package.json
CHANGED
|
@@ -3,6 +3,7 @@ import { set_private_env, set_public_env } from '../shared-server.js';
|
|
|
3
3
|
import { options, get_hooks } from '__SERVER__/internal.js';
|
|
4
4
|
import { DEV } from 'esm-env';
|
|
5
5
|
import { filter_env } from '../../utils/env.js';
|
|
6
|
+
import { format_server_error } from './utils.js';
|
|
6
7
|
import { set_read_implementation, set_manifest } from '__sveltekit/server';
|
|
7
8
|
import { set_app } from './app.js';
|
|
8
9
|
|
|
@@ -87,8 +88,14 @@ export class Server {
|
|
|
87
88
|
handle: module.handle || (({ event, resolve }) => resolve(event)),
|
|
88
89
|
handleError:
|
|
89
90
|
module.handleError ||
|
|
90
|
-
(({ status, error }) =>
|
|
91
|
-
|
|
91
|
+
(({ status, error, event }) => {
|
|
92
|
+
const error_message = format_server_error(
|
|
93
|
+
status,
|
|
94
|
+
/** @type {Error} */ (error),
|
|
95
|
+
event
|
|
96
|
+
);
|
|
97
|
+
console.error(error_message);
|
|
98
|
+
}),
|
|
92
99
|
handleFetch: module.handleFetch || (({ request, fetch }) => fetch(request)),
|
|
93
100
|
handleValidationError:
|
|
94
101
|
module.handleValidationError ||
|
|
@@ -100,7 +100,8 @@ export async function handle_fatal_error(event, state, options, error) {
|
|
|
100
100
|
*/
|
|
101
101
|
export async function handle_error_and_jsonify(event, state, options, error) {
|
|
102
102
|
if (error instanceof HttpError) {
|
|
103
|
-
|
|
103
|
+
// @ts-expect-error custom user errors may not have a message field if App.Error is overwritten
|
|
104
|
+
return { message: 'Unknown Error', ...error.body };
|
|
104
105
|
}
|
|
105
106
|
|
|
106
107
|
if (__SVELTEKIT_DEV__ && typeof error == 'object') {
|
|
@@ -186,6 +187,59 @@ export function has_prerendered_path(manifest, pathname) {
|
|
|
186
187
|
);
|
|
187
188
|
}
|
|
188
189
|
|
|
190
|
+
/**
|
|
191
|
+
* Formats the error into a nice message with sanitized stack trace
|
|
192
|
+
* @param {number} status
|
|
193
|
+
* @param {Error} error
|
|
194
|
+
* @param {import('@sveltejs/kit').RequestEvent} event
|
|
195
|
+
*/
|
|
196
|
+
export function format_server_error(status, error, event) {
|
|
197
|
+
const formatted_text = `\n\x1b[1;31m[${status}] ${event.request.method} ${event.url.pathname}\x1b[0m\n`;
|
|
198
|
+
|
|
199
|
+
if (status === 404) {
|
|
200
|
+
return formatted_text + error.message;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
return formatted_text + (DEV ? clean_up_stack_trace(error) : error.stack);
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* In dev, tidy up stack traces by making paths relative to the current project directory
|
|
208
|
+
* @param {string} file
|
|
209
|
+
*/
|
|
210
|
+
let relative = (file) => file;
|
|
211
|
+
|
|
212
|
+
if (DEV) {
|
|
213
|
+
try {
|
|
214
|
+
const path = await import('node:path');
|
|
215
|
+
const process = await import('node:process');
|
|
216
|
+
|
|
217
|
+
relative = (file) => path.relative(process.cwd(), file);
|
|
218
|
+
} catch {
|
|
219
|
+
// do nothing
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/**
|
|
224
|
+
* Provides a refined stack trace by excluding lines following the last occurrence of a line containing +page. +layout. or +server.
|
|
225
|
+
* @param {Error} error
|
|
226
|
+
*/
|
|
227
|
+
export function clean_up_stack_trace(error) {
|
|
228
|
+
const stack_trace = (error.stack?.split('\n') ?? []).map((line) => {
|
|
229
|
+
return line.replace(/\((.+)(:\d+:\d+)\)$/, (_, file, loc) => `(${relative(file)}${loc})`);
|
|
230
|
+
});
|
|
231
|
+
|
|
232
|
+
// progressive enhancement for people who haven't configured kit.files.src to something else
|
|
233
|
+
const last_line_from_src_code = stack_trace.findLastIndex((line) => /\(src[\\/]/.test(line));
|
|
234
|
+
|
|
235
|
+
if (last_line_from_src_code === -1) {
|
|
236
|
+
// default to the whole stack trace
|
|
237
|
+
return error.stack;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
return stack_trace.slice(0, last_line_from_src_code + 1).join('\n');
|
|
241
|
+
}
|
|
242
|
+
|
|
189
243
|
/**
|
|
190
244
|
* Returns the filename without the extension. e.g., `+page.server`, `+page`, etc.
|
|
191
245
|
* @param {string | undefined} node_id
|
package/src/version.js
CHANGED