Repository: blitz-js/superjson (ts)

Issue:
Custom error serialization broken in 1.4.0
I added `registerClass` in Blitz internals so that blitz custom errors are fully deserialized on the client to work with `instanceof`. This works great in 1.3.0, but breaks in 1.4.0.

In 1.4.0, the custom error is serialized as a plain Error instead of the custom error class.

Also, I noticed that in 1.4.0 extra static properties on the error are not serialized. In 1.3.0 blitz errors have `statusCode` properly serialized, but in `1.4.0` they don't.

Related Blitz PR: https://github.com/blitz-js/blitz/pull/1414

Pull request description:
Fix #80
Closes #80 

Patch:
diff --git a/src/transformer.ts b/src/transformer.ts
index 54dd8c3..0aee497 100644
--- a/src/transformer.ts
+++ b/src/transformer.ts
@@ -240,24 +240,24 @@ const compositeRules = [classRule, symbolRule, customRule];
 export const transformValue = (
   value: any
 ): { value: any; type: TypeAnnotation } | undefined => {
-  const applicableSimpleRule = find(simpleRules, rule =>
+  const applicableCompositeRule = find(compositeRules, rule =>
     rule.isApplicable(value)
   );
-
-  if (applicableSimpleRule) {
+  if (applicableCompositeRule) {
     return {
-      value: applicableSimpleRule.transform(value as never),
-      type: applicableSimpleRule.annotation,
+      value: applicableCompositeRule.transform(value as never),
+      type: applicableCompositeRule.annotation(value),
     };
   }
 
-  const applicableCompositeRule = find(compositeRules, rule =>
+  const applicableSimpleRule = find(simpleRules, rule =>
     rule.isApplicable(value)
   );
-  if (applicableCompositeRule) {
+
+  if (applicableSimpleRule) {
     return {
-      value: applicableCompositeRule.transform(value as never),
-      type: applicableCompositeRule.annotation(value),
+      value: applicableSimpleRule.transform(value as never),
+      type: applicableSimpleRule.annotation,
     };
   }
 


Test patch:
diff --git a/src/index.test.ts b/src/index.test.ts
index 25fd2e2..bfebe31 100644
--- a/src/index.test.ts
+++ b/src/index.test.ts
@@ -716,4 +716,27 @@ describe('stringify & parse', () => {
 
     global.BigInt = oldBigInt;
   });
+
+  test('regression #80: Custom error serialisation isnt overriden', () => {
+    class CustomError extends Error {
+      constructor(public readonly customProperty: number) {
+        super("I'm a custom error");
+        // eslint-disable-next-line es5/no-es6-static-methods
+        Object.setPrototypeOf(this, CustomError.prototype);
+      }
+    }
+
+    expect(new CustomError(10)).toBeInstanceOf(CustomError);
+
+    SuperJSON.registerClass(CustomError);
+
+    const { error } = SuperJSON.deserialize(
+      SuperJSON.serialize({
+        error: new CustomError(10),
+      })
+    ) as any;
+
+    expect(error).toBeInstanceOf(CustomError);
+    expect(error.customProperty).toEqual(10);
+  });
 });