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

Marshalling: Exception subclasses #205

Open
james-hu opened this issue Feb 6, 2024 · 5 comments
Open

Marshalling: Exception subclasses #205

james-hu opened this issue Feb 6, 2024 · 5 comments
Labels
enhancement New feature or request marshalling
Milestone

Comments

@james-hu
Copy link

james-hu commented Feb 6, 2024

For example, if in .NET I have this:

throw new FormatException("invalid input");

then in Node, can I know that the Error is caused by "FormatException"?

It seems this feature is missing so that the only way is to check the "message" property of the Error object?

@jasongin jasongin added the enhancement New feature or request label Feb 8, 2024
@jasongin
Copy link
Member

jasongin commented Feb 8, 2024

While the .NET exception message and stack trace are propagated to the JS Error, the exception type currently is not.

Would you expect a subclass of JS Error for every .NET exception type? Or perhaps a property on the error object that is the (string) type of .NET exception?

@james-hu
Copy link
Author

james-hu commented Feb 9, 2024

Due to the weak-type nature of Javascript, and the existence of popular tools trying to minimise Javascript code size, I think having additional properties would be more robust. And I guess that is also easier to implement.

@james-hu
Copy link
Author

james-hu commented Feb 9, 2024

But that also depends on the ambition of this project. If it is about "calling .NET from Node.js without noticing it", probably 1:1 mapping between Exception/Error types would be better. Personally I feel too good usually can't be true. Personally I am quite happy to be aware that I am writing the glue code to connect .NET and Node.js.

@luanmm
Copy link

luanmm commented Feb 15, 2024

I think that would be great if it could be a 1:1 Exception/Error in the generated code, because it would match the logic in Task/Promise and other "native" mappings.

For instance, a FormatException that inherits Exception, could generate something like this:

class FormatError extends Error {
   ...
}

That way, one could use it like this:

try {
   ...
}
catch (error) {
    if (error instanceof FormatError) { // IMHO, this is the best way to work with errors in JavaScript (seems more "strict" to language semantics, even JS being a very dynamic language)
       ...
    }
}

With custom properties, seems that the library would be using a "workaround" to allow error identification, which I don't think is a great way to do it (although it seems to be easier for now).

@jasongin
Copy link
Member

jasongin commented Mar 7, 2024

For instance, a FormatException that inherits Exception, could generate something like this:

class FormatError extends Error {
   ...
}

Something like this should be possible (though definitely more work). Another advantage is it would allow JS to access any other properties or methods on the .NET Exception subclass, which could be very important in certain error-handling scenarios.

I'm adding this feature to the roadmap.

@jasongin jasongin changed the title Is the class name of dotnet Exception propagated to Node? Marshalling: Exception subclasses Mar 7, 2024
@jasongin jasongin added this to the Backlog milestone May 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request marshalling
Projects
Status: 🔖 Ready
Development

No branches or pull requests

3 participants