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

v8pp throws strings by default, which is bad practice #205

Open
jcmonnin opened this issue Jan 2, 2024 · 4 comments
Open

v8pp throws strings by default, which is bad practice #205

jcmonnin opened this issue Jan 2, 2024 · 4 comments

Comments

@jcmonnin
Copy link
Contributor

jcmonnin commented Jan 2, 2024

v8pp::throw_ex throws a string by default, which is considered a bad practice by many:

https://stackoverflow.com/questions/11502052/throwing-strings-instead-of-errors

In the application I'm working on, errors from user scripts are formatted in a standard way including stack traces, but it fails if the error is thrown within v8pp. The user is left without stack trace. For that reason I have a patch to default to throwing and Error instead of a string in v8pp.

Would this patch be welcome upstream?

@pmed
Copy link
Owner

pmed commented Jan 2, 2024

Hi Jean-Claude,

You have proposed the right thing, thanks 👍. A possible fix would be just using exception_ctor = v8::Exception::Error default parameter value for throw_ex(), isn't it?

Sure, PR is welcomed!

@jcmonnin
Copy link
Contributor Author

jcmonnin commented Jan 3, 2024

I pushed a PR.

In my own binding code I use following helper functions (which originates from the time where v8pp didn't even have the option to specify an exception type).

inline void throwRangeError(v8::Isolate* isolate, std::string_view message)
{
    isolate->ThrowException(v8::Exception::RangeError(v8pp::to_v8(isolate, message)));
}

inline void throwReferenceError(v8::Isolate* isolate, std::string_view message)
{
    isolate->ThrowException(v8::Exception::ReferenceError(v8pp::to_v8(isolate, message)));
}

inline void throwSyntaxError(v8::Isolate* isolate, std::string_view message)
{
    isolate->ThrowException(v8::Exception::SyntaxError(v8pp::to_v8(isolate, message)));
}

inline void throwTypeError(v8::Isolate* isolate, std::string_view message)
{
    isolate->ThrowException(v8::Exception::TypeError(v8pp::to_v8(isolate, message)));
}

inline void throwError(v8::Isolate* isolate, std::string_view message)
{
    isolate->ThrowException(v8::Exception::Error(v8pp::to_v8(isolate, message)));
}

Given there are only a handful of exception types, a similar API which could be considered. The current v8pp::throw_ex would be an implementation detail and there would be v8pp::throw_error, v8pp::throw_range_error, ,..

Admittedly, the current API with the ctor function pointer is fine too.

@pmed
Copy link
Owner

pmed commented Jan 5, 2024

I like your idea, to have a set of functions for throwing particular exception types. Do you mean to have them one-liners that call throw_ex() like:

inline v8::Local<v8::Value> throw_error(v8::Isolate* isolate, std::string_view message, v8::Local<v8::Value> exception_options = {})
{
    return throw_ex(isolate, message, v8::Exception::Error, exception_options);
}

inline v8::Local<v8::Value> throw_type_error(v8::Isolate* isolate, std::string_view message, v8::Local<v8::Value> exception_options = {})
{
    return throw_ex(isolate, message, v8::Exception::TypeError, exception_options);
}

@jcmonnin
Copy link
Contributor Author

jcmonnin commented Jan 8, 2024

Yes, exactly.

pmed added a commit that referenced this issue Feb 17, 2024
like `throw_error()`, `throw_range_error()`

see #205
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

No branches or pull requests

2 participants