Skip to content

Commit

Permalink
chore: shrink error messages shipped to client (#11551)
Browse files Browse the repository at this point in the history
* chore: shrink error messages shipped to client

* format

* skip unreachable code

* remove outdated test

* cast instead of return

* run test only in dev mode
  • Loading branch information
benmccann authored Jan 9, 2024
1 parent e2504f1 commit 8468af5
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 23 deletions.
5 changes: 5 additions & 0 deletions .changeset/green-shrimps-carry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@sveltejs/kit": patch
---

chore: shrink error messages shipped to client
6 changes: 3 additions & 3 deletions packages/kit/src/runtime/client/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -1121,7 +1121,7 @@ function _before_navigate({ url, type, intent, delta }) {
...nav.navigation,
cancel: () => {
should_block = true;
nav.reject(new Error('navigation was cancelled'));
nav.reject(new Error('navigation cancelled'));
}
};

Expand Down Expand Up @@ -1210,7 +1210,7 @@ async function navigate({

// abort if user navigated during update
if (token !== nav_token) {
nav.reject(new Error('navigation was aborted'));
nav.reject(new Error('navigation aborted'));
return false;
}

Expand Down Expand Up @@ -1903,7 +1903,7 @@ function _start_router() {
...nav.navigation,
cancel: () => {
should_block = true;
nav.reject(new Error('navigation was cancelled'));
nav.reject(new Error('navigation cancelled'));
}
};

Expand Down
9 changes: 3 additions & 6 deletions packages/kit/src/utils/routing.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,9 @@ export function parse_route_id(id) {
);
}
const match = param_pattern.exec(content);
if (!match) {
throw new Error(
`Invalid param: ${content}. Params and matcher names can only have underscores and alphanumeric characters.`
);
}
// We know the match cannot be null because manifest generation would have
// if we hit an invalid param/matcher name with non-alphanumeric character.
const match = /** @type {RegExpExecArray} */ (param_pattern.exec(content));
const [, is_optional, is_rest, name, matcher] = match;
// It's assumed that the following invalid route id cases are already checked
Expand Down
5 changes: 0 additions & 5 deletions packages/kit/src/utils/routing.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,6 @@ describe('parse_route_id', () => {
expect(actual.params).toEqual(expected.params);
});
}

test('errors on bad param name', () => {
assert.throws(() => parse_route_id('abc/[b-c]'), /Invalid param: b-c/);
assert.throws(() => parse_route_id('abc/[bc=d-e]'), /Invalid param: bc=d-e/);
});
});

describe('exec', () => {
Expand Down
8 changes: 5 additions & 3 deletions packages/kit/src/utils/url.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BROWSER } from 'esm-env';
import { BROWSER, DEV } from 'esm-env';

/**
* Matches a URI scheme. See https://www.rfc-editor.org/rfc/rfc3986#section-3.1
Expand Down Expand Up @@ -146,7 +146,9 @@ export function make_trackable(url, callback, search_params_callback) {
};
}

disable_hash(tracked);
if (DEV || !BROWSER) {
disable_hash(tracked);
}

return tracked;
}
Expand All @@ -155,7 +157,7 @@ export function make_trackable(url, callback, search_params_callback) {
* Disallow access to `url.hash` on the server and in `load`
* @param {URL} url
*/
export function disable_hash(url) {
function disable_hash(url) {
allow_nodejs_console_log(url);

Object.defineProperty(url, 'hash', {
Expand Down
14 changes: 8 additions & 6 deletions packages/kit/test/apps/basics/test/client.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,14 @@ test.describe('Load', () => {
expect(await page.textContent('h2')).toBe('x: b: 4');
});

test('accessing url.hash from load errors and suggests using page store', async ({ page }) => {
await page.goto('/load/url-hash#please-dont-send-me-to-load');
expect(await page.textContent('#message')).toBe(
'This is your custom error page saying: "Cannot access event.url.hash. Consider using `$page.url.hash` inside a component instead (500 Internal Error)"'
);
});
if (process.env.DEV) {
test('accessing url.hash from load errors and suggests using page store', async ({ page }) => {
await page.goto('/load/url-hash#please-dont-send-me-to-load');
expect(await page.textContent('#message')).toBe(
'This is your custom error page saying: "Cannot access event.url.hash. Consider using `$page.url.hash` inside a component instead (500 Internal Error)"'
);
});
}

test('url instance methods work in load', async ({ page }) => {
await page.goto('/load/url-to-string');
Expand Down

0 comments on commit 8468af5

Please sign in to comment.