Skip to content

[fix] show more descriptive error if data returned from load is a non-POJO - #7386

Merged
dummdidumm merged 3 commits into
sveltejs:masterfrom
errmayank:master
Oct 26, 2022
Merged

[fix] show more descriptive error if data returned from load is a non-POJO#7386
dummdidumm merged 3 commits into
sveltejs:masterfrom
errmayank:master

Conversation

@errmayank

@errmayank errmayank commented Oct 25, 2022

Copy link
Copy Markdown
Contributor

Closes #7262

  • Tested it out with return json

Screenshot 2022-10-25 at 17 44 12

Screenshot 2022-10-25 at 17 42 52

  • with new Error

Screenshot 2022-10-25 at 17 43 58

Screenshot 2022-10-25 at 17 43 15

Please don't delete this checklist! Before submitting the PR, please make sure you do the following:

  • It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs
  • This message body should clearly illustrate what problems it solves.
  • Ideally, include a test that fails without this PR but passes with it.

Tests

  • Run the tests with pnpm test and lint the project with pnpm lint and pnpm check

Changesets

  • If your PR makes a change that should be noted in one or more packages' changelogs, generate a changeset by running pnpm changeset and following the prompts. All changesets should be patch until SvelteKit 1.0

@changeset-bot

changeset-bot Bot commented Oct 25, 2022

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 4cf5e43

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@sveltejs/kit Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@dummdidumm dummdidumm left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, looks mostly good, added some small comments

const nonPojoError = /pojo/i.exec(error.message);

if (nonPojoError) {
const constructorName = branch[0]?.server_data?.data?.constructor?.name;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be from server_data deeper in the load tree, so I think it should be

Suggested change
const constructorName = branch[0]?.server_data?.data?.constructor?.name;
const constructorName = branch.find(({ server_data }) => server_data?.data?.constructor?.name)?.server_data.data.constructor.name;

Comment on lines +192 to +194
`Data returned from \`load\` must be a plain object${
constructorName ? ` rather than ${constructorName} constructor` : ''
}`

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
`Data returned from \`load\` must be a plain object${
constructorName ? ` rather than ${constructorName} constructor` : ''
}`
`Data returned from \`load\` (while rendering ${event.routeId}) must be a plain object${
constructorName ? ` rather than an instance of ${constructorName}` : ''
}`

@errmayank

Copy link
Copy Markdown
Contributor Author

Thanks @dummdidumm, updated.

@errmayank errmayank changed the title [fix] show more descriptive error if data returned from is a non-POJO [fix] show more descriptive error if data returned from load is a non-POJO Oct 25, 2022
@dummdidumm

Copy link
Copy Markdown
Member

Could you add a changeset by running npx changeset in the root and following the prompts (patch release)? Then we're good to go 👍

@errmayank

Copy link
Copy Markdown
Contributor Author

Added changeset

@dummdidumm dummdidumm left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@dummdidumm
dummdidumm merged commit 03dde94 into sveltejs:master Oct 26, 2022
@aypm-dev

aypm-dev commented Dec 21, 2022

Copy link
Copy Markdown

Hey! I was trying to return a response with headers in a load, silly me. Now I understand how load works, but I noticed that I'm getting the old error when returning a response with headers and not the new one. Is this case in the scope of the pull request?

Code: (+page.server.ts)

import type { PageServerLoad } from './$types';

export const load: PageServerLoad = async ({ locals, url, cookies }) => {
  const headers = new Headers()
  headers.append("set-cookie",  " cookies here ")

  return new Response( null, { headers: headers } )
}

Error: Cannot stringify arbitrary non-POJOs (data.headers)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

More informative error if someone returns a Response from load

3 participants