Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(lib): Better error message when message can not be serializable #254

Conversation

demetriusfeijoo
Copy link
Contributor

@demetriusfeijoo demetriusfeijoo commented Aug 18, 2023

What?

Improve exception's message when an object is not cloneable (accepted by postMessage).

Why?

JIRA: EXT-1603

Some object structures are not allowed to be sent thru windows.postMessage() function, raising a DataCloneError exception.

However, the raised exception is not so clear to understand since this error is too generic and doesn't give so much useful information to the developer.

Exemple of a wrong message type:
Screenshot 2023-08-18 at 07 40 17

Output Before:
image

Output After:
Screenshot 2023-08-18 at 07 38 30

@vercel
Copy link

vercel bot commented Aug 18, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
plugin-sandbox ✅ Ready (Inspect) Visit Preview 💬 Add feedback Aug 25, 2023 5:29pm

Co-authored-by: Johannes Lindgren <14206504+johannes-lindgren@users.noreply.github.com>
@eunjae-lee eunjae-lee changed the title refactor(lib): Better error message when message can not be serializable fix(lib): Better error message when message can not be serializable Aug 22, 2023
@eunjae-lee
Copy link
Contributor

This PR is not refactoring the code, but it's more of an improvement on how we handle the error situation. So I renamed the title to "fix".

@demetriusfeijoo
Copy link
Contributor Author

You're right @eunjae-lee .

Thanks for changing it 🎯.

Copy link
Contributor

Choose a reason for hiding this comment

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

I don't know if tests for this function make sense. It contains side-effects, so it will be difficult to test, and tests will tend to test internal implementation. I'd recommend extracting postToContainer outside createFieldPlugin. When you do, pass window.postMessage as an argument, so that you don't need to mock anything. That will be much more testable.

I think we can remove the test in this file. They have some issues, for example:

expect(cleanupFieldPlugin()).toEqual(undefined)

Is not needed because the return value is already checked by TypeCcript. Also, it could start to fail if the cleanup function returned a value, which would be okay with Typescript, as the return signature is void: this is valid:

const fun: () => void = () => 123
expect(onUpdateStateMock).toHaveBeenCalledWith({
      type: 'error',
      error: new Error(
        `The window is not embedded within another window. Did you mean to open the field plugin in the sandbox? ${sandboxUrl()}`,
      ),
    })

This would fail if the message changed slightly, but that is ok.

expect(onUpdateStateMock).toBeCalled()

seems to test something else than what described in the it() function call.

Copy link
Contributor

Choose a reason for hiding this comment

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

Good!

@demetriusfeijoo
Copy link
Contributor Author

Thanks for the detailed explanation @johannes-lindgren. 🙌

I'm going to take a look at these points.

…-be-serialized' of github.com:storyblok/field-plugin into EXT-1603-better-error-message-when-the-object-could-not-be-serialized
@@ -1,2 +1,3 @@
export * from './hasKey'
export * from './isCloneable'
export * from './getRandomString'
Copy link
Contributor Author

Choose a reason for hiding this comment

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

It came from the main branch

@demetriusfeijoo demetriusfeijoo merged commit 715a4b3 into main Aug 25, 2023
12 checks passed
@demetriusfeijoo demetriusfeijoo deleted the EXT-1603-better-error-message-when-the-object-could-not-be-serialized branch August 25, 2023 17:33
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.

None yet

3 participants