Skip to content

Commit

Permalink
make 404 more helpful if paths.base is missing (#5622)
Browse files Browse the repository at this point in the history
* make 404 more helpful if paths.base is missing - closes #5465

* Update packages/kit/src/core/prerender/prerender.js

Co-authored-by: Ben McCann <322311+benmccann@users.noreply.github.com>

* just pass config as an argument, silly

* Update packages/kit/src/core/prerender/prerender.js

Co-authored-by: Simon H <5968653+dummdidumm@users.noreply.github.com>

* tidy up

Co-authored-by: Ben McCann <322311+benmccann@users.noreply.github.com>
Co-authored-by: Simon H <5968653+dummdidumm@users.noreply.github.com>
  • Loading branch information
3 people authored Jul 20, 2022
1 parent 11d7b85 commit 5675c93
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 11 deletions.
5 changes: 5 additions & 0 deletions .changeset/clean-schools-doubt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

Make 404 error more helpful if paths.base is missing
33 changes: 22 additions & 11 deletions packages/kit/src/core/prerender/prerender.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,39 @@ import { escape_html_attr } from '../../utils/escape.js';

/**
* @typedef {import('types').PrerenderErrorHandler} PrerenderErrorHandler
* @typedef {import('types').PrerenderOnErrorValue} OnError
* @typedef {import('types').Logger} Logger
*/

/** @type {(details: Parameters<PrerenderErrorHandler>[0] ) => string} */
function format_error({ status, path, referrer, referenceType }) {
return `${status} ${path}${referrer ? ` (${referenceType} from ${referrer})` : ''}`;
/**
* @param {Parameters<PrerenderErrorHandler>[0]} details
* @param {import('types').ValidatedKitConfig} config
*/
function format_error({ status, path, referrer, referenceType }, config) {
const message =
status === 404 && !path.startsWith(config.paths.base)
? `${path} does not begin with \`base\`, which is configured in \`paths.base\` and can be imported from \`$app/paths\``
: path;

return `${status} ${message}${referrer ? ` (${referenceType} from ${referrer})` : ''}`;
}

/** @type {(log: Logger, onError: OnError) => PrerenderErrorHandler} */
function normalise_error_handler(log, onError) {
switch (onError) {
/**
* @param {Logger} log
* @param {import('types').ValidatedKitConfig} config
* @returns {PrerenderErrorHandler}
*/
function normalise_error_handler(log, config) {
switch (config.prerender.onError) {
case 'continue':
return (details) => {
log.error(format_error(details));
log.error(format_error(details, config));
};
case 'fail':
return (details) => {
throw new Error(format_error(details));
throw new Error(format_error(details, config));
};
default:
return onError;
return config.prerender.onError;
}
}

Expand Down Expand Up @@ -127,7 +138,7 @@ export async function prerender({ config, entries, files, log }) {

const server = new Server(manifest);

const error = normalise_error_handler(log, config.prerender.onError);
const error = normalise_error_handler(log, config);

const q = queue(config.prerender.concurrency);

Expand Down

0 comments on commit 5675c93

Please sign in to comment.