-
Notifications
You must be signed in to change notification settings - Fork 0
Integrating JSON serialization and deserialization #2
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
Conversation
|
Note that in node v16, normal errors can also have a |
|
I'll update to node v16 here too. |
|
@emmacasolin if you are happy with reviewing the the code, then I'll update to v16 as a separate commit, and then push a release. Then back to MatrixAI/Polykey#304 and we can update to this latest package, and make use of the We will then still need to do the error filtering in PK when going from agent to agent, but that's a separate concern. And I was thinking that the stack is always filtered even when going from agent to client. I'm also wondering if error filtering is related between the server-side logging of the errors vs the filtering of error data being sent over. It's like they are separate concerns as well, and maybe it's really that we need to distinguish client errors from server errors. |
|
All done. BTW @emmacasolin I had to change to |
Description
Previously in #1, I had tried to add generic
toJSONandfromJSONmethods intoAbstractError, however I couldn't figure out the types and I realised that it was an application concern. Now that @emmacasolin has prototyped how it works in PK, I can bring this functionality back into js-errors.The proper insight is to realise that
toJSONis used byJSON.stringifywithout bothering with thereplacer, but thereplacerwould still be used by errors outside theAbstractErrorclass hierarchy such as standard JS errors. And to not bother with thecauseproperty, and just leave it as is, sinceJSON.stringifywill traverse into it anyway.Then on the deserialisation side, we wanted to apply a runtime deserialisation associated with each class, while also inheriting the default deserialisation logic if nothing changed. This was done with a static method
fromJSON. However the static methods are constrained by function-subtyping, but by making use of theanytype and theClassutility type we are able to makefromJSONvery generic, such that child classes can override it. This is also used by areviverthat still needs to deal with errors outside theAbstractErrorhierarchy.I've now created a minimal prototype serialisation/deserialisation logic synthesised from the discussion MatrixAI/Polykey#304 (comment).
The reviver side is much more complicated than the replacer side. Obviously because you have to deal with unknown input.
As written in the comments, there are ultimately 3 choices when dealing with unknown error data that we want to deserialise.
So the example goes with choice 3, and I think for production applications that needs to deal with backwards and forwards compatibility, it will be important to have that unknown error with associated data. However you can see in the tests that this is only ever done at the root of the deserialised error, all other data is returned as-is if it could not be decoded properly.
Issues Fixed
Tasks
toJSONtoAbstractErrorstatic fromJSONtoAbstractErrorcauseproperty available on standard JS errors in the tests for serialisation and deserialisation -causeis actually ES2022, so it is not done https://node.green/Final checklist