-
Notifications
You must be signed in to change notification settings - Fork 0
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
Integrating JSON serialization and deserialization #2
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
toJSON
andfromJSON
methods 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
toJSON
is used byJSON.stringify
without bothering with thereplacer
, but thereplacer
would still be used by errors outside theAbstractError
class hierarchy such as standard JS errors. And to not bother with thecause
property, and just leave it as is, sinceJSON.stringify
will 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 theany
type and theClass
utility type we are able to makefromJSON
very generic, such that child classes can override it. This is also used by areviver
that still needs to deal with errors outside theAbstractError
hierarchy.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
toJSON
toAbstractError
static fromJSON
toAbstractError
cause
property available on standard JS errors in the tests for serialisation and deserialisation -cause
is actually ES2022, so it is not done https://node.green/Final checklist