Skip to content

Add unknown class name to deserialization error#303

Merged
Skn0tt merged 3 commits into
flightcontrolhq:mainfrom
marcinciarka:marcinciarka/better-deserialization-error
Dec 5, 2024
Merged

Add unknown class name to deserialization error#303
Skn0tt merged 3 commits into
flightcontrolhq:mainfrom
marcinciarka:marcinciarka/better-deserialization-error

Conversation

@marcinciarka

@marcinciarka marcinciarka commented Nov 7, 2024

Copy link
Copy Markdown
Contributor

This pull request introduces a new test case and improves error messaging in the SuperJSON deserialization process. The changes enhance the developer experience by providing more descriptive error messages and ensuring the robustness of the code.

Enhancements to error messaging and testing:

  • src/transformer.ts: Updated the error message to include the name of the unknown class being deserialized, making it easier to identify the issue.
  • src/transformer.test.ts: Added a new test case to verify that an appropriate error is thrown when attempting to deserialize an unregistered class, ensuring that the error message includes missing class name.

@marcinciarka marcinciarka requested a review from Skn0tt as a code owner November 7, 2024 10:14
@piotrwitek

piotrwitek commented Nov 7, 2024

Copy link
Copy Markdown

This is awesome change, would help debug various issues when deserialization fails when class or other element is not registered or it's module not imported. Would love to have that merged! ❤️

@Skn0tt Skn0tt left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

wonderful change, thank you! could you add a test for it?

Comment thread src/transformer.ts Outdated
Co-authored-by: Simon Knott <info@simonknott.de>
@marcinciarka

Copy link
Copy Markdown
Contributor Author

wonderful change, thank you! could you add a test for it?

@Skn0tt tried for a while now and I can't seem to make it throw that error in tests, can I get some help with this? :)

@Skn0tt

Skn0tt commented Nov 25, 2024

Copy link
Copy Markdown
Collaborator

Try this:

❯ git diff src
diff --git a/src/index.test.ts b/src/index.test.ts
index e314728..ee49e72 100644
--- a/src/index.test.ts
+++ b/src/index.test.ts
@@ -810,6 +810,9 @@ describe('stringify & parse', () => {
       );
       expect(deserialized.s7).toBeInstanceOf(Train);
       expect(typeof deserialized.s7.brag()).toBe('string');
+
+      meta.values.s7 = [['class', 'Plane']];
+      expect(() => SuperJSON.deserialize(JSON.parse(JSON.stringify({ json, meta })))).toThrowError(`Trying to deserialize unknown class 'Plane' - check https://github.com/blitz-js/superjson/issues/116#issuecomment-773996564`)
     });
 
     describe('with accessor attributes', () => {

@marcinciarka

Copy link
Copy Markdown
Contributor Author

Try this:

❯ git diff src
diff --git a/src/index.test.ts b/src/index.test.ts
index e314728..ee49e72 100644
--- a/src/index.test.ts
+++ b/src/index.test.ts
@@ -810,6 +810,9 @@ describe('stringify & parse', () => {
       );
       expect(deserialized.s7).toBeInstanceOf(Train);
       expect(typeof deserialized.s7.brag()).toBe('string');
+
+      meta.values.s7 = [['class', 'Plane']];
+      expect(() => SuperJSON.deserialize(JSON.parse(JSON.stringify({ json, meta })))).toThrowError(`Trying to deserialize unknown class 'Plane' - check https://github.com/blitz-js/superjson/issues/116#issuecomment-773996564`)
     });
 
     describe('with accessor attributes', () => {

Thank you, got it working.

@Skn0tt Skn0tt merged commit 4e670bd into flightcontrolhq:main Dec 5, 2024
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.

3 participants