Skip to content

Commit

Permalink
Prod mode page errors (#3151)
Browse files Browse the repository at this point in the history
* add error getters in prod mode, at least for now

* changeset
  • Loading branch information
Rich-Harris committed Dec 30, 2021
1 parent c1f5eb2 commit 42a55eb
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 52 deletions.
5 changes: 5 additions & 0 deletions .changeset/ten-hairs-perform.md
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

Add path/query error getters in prod mode
32 changes: 15 additions & 17 deletions packages/kit/src/runtime/client/renderer.js
Expand Up @@ -445,24 +445,22 @@ export class Renderer {
if (!this.current.url || url.href !== this.current.url.href) {
result.props.page = { url, params };

if (import.meta.env.DEV) {
// TODO remove this for 1.0
/**
* @param {string} property
* @param {string} replacement
*/
const print_error = (property, replacement) => {
Object.defineProperty(result.props.page, property, {
get: () => {
throw new Error(`$page.${property} has been replaced by $page.url.${replacement}`);
}
});
};
// TODO remove this for 1.0
/**
* @param {string} property
* @param {string} replacement
*/
const print_error = (property, replacement) => {
Object.defineProperty(result.props.page, property, {
get: () => {
throw new Error(`$page.${property} has been replaced by $page.url.${replacement}`);
}
});
};

print_error('origin', 'origin');
print_error('path', 'pathname');
print_error('query', 'searchParams');
}
print_error('origin', 'origin');
print_error('path', 'pathname');
print_error('query', 'searchParams');
}

const leaf = filtered[filtered.length - 1];
Expand Down
32 changes: 15 additions & 17 deletions packages/kit/src/runtime/server/index.js
Expand Up @@ -43,24 +43,22 @@ export async function respond(incoming, options, state = {}) {
locals: {}
};

if (options.dev) {
// TODO remove this for 1.0
/**
* @param {string} property
* @param {string} replacement
*/
const print_error = (property, replacement) => {
Object.defineProperty(request, property, {
get: () => {
throw new Error(`request.${property} has been replaced by request.url.${replacement}`);
}
});
};
// TODO remove this for 1.0
/**
* @param {string} property
* @param {string} replacement
*/
const print_error = (property, replacement) => {
Object.defineProperty(request, property, {
get: () => {
throw new Error(`request.${property} has been replaced by request.url.${replacement}`);
}
});
};

print_error('origin', 'origin');
print_error('path', 'pathname');
print_error('query', 'searchParams');
}
print_error('origin', 'origin');
print_error('path', 'pathname');
print_error('query', 'searchParams');

try {
return await options.hooks.handle({
Expand Down
34 changes: 16 additions & 18 deletions packages/kit/src/runtime/server/page/render.js
Expand Up @@ -72,24 +72,22 @@ export async function render_response({
components: branch.map(({ node }) => node.module.default)
};

if (options.dev) {
// TODO remove this for 1.0
/**
* @param {string} property
* @param {string} replacement
*/
const print_error = (property, replacement) => {
Object.defineProperty(props.page, property, {
get: () => {
throw new Error(`$page.${property} has been replaced by $page.url.${replacement}`);
}
});
};

print_error('origin', 'origin');
print_error('path', 'pathname');
print_error('query', 'searchParams');
}
// TODO remove this for 1.0
/**
* @param {string} property
* @param {string} replacement
*/
const print_error = (property, replacement) => {
Object.defineProperty(props.page, property, {
get: () => {
throw new Error(`$page.${property} has been replaced by $page.url.${replacement}`);
}
});
};

print_error('origin', 'origin');
print_error('path', 'pathname');
print_error('query', 'searchParams');

// props_n (instead of props[n]) makes it easy to avoid
// unnecessary updates for layout components
Expand Down

0 comments on commit 42a55eb

Please sign in to comment.