Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

[Feature Request] Add JS_ThrowError() convenience function #375

Closed
TooTallNate opened this issue Apr 12, 2024 · 6 comments · Fixed by #411
Closed

[Feature Request] Add JS_ThrowError() convenience function #375

TooTallNate opened this issue Apr 12, 2024 · 6 comments · Fixed by #411

Comments

@TooTallNate
Copy link
Contributor

There are convenience functions such as JS_ThrowTypeError() which create a new TypeError using printf syntax, however there is no such function for a regular Error instance. Would it make sense to add that? Or is there some reason why it doesn't exist already?

@saghul
Copy link
Contributor

saghul commented Apr 12, 2024

I guess it was skipped because it's a 2 liner? You can JS_NewError, assign the message and then JS_Throw the error object. That said, the printf syntax is very convenient, I think we should have it.

@TooTallNate
Copy link
Contributor Author

JS_NewError only takes a ctx arg, not a message value, so you need to do the whole JS_DefinePropertyValueStr(ctx, err, "message", JS_NewString(ctx, message), JS_PROP_C_W) dance

@saghul
Copy link
Contributor

saghul commented Apr 12, 2024

Yeah I realized midways writing the reply 😅 . A PR would be welcome!

@Icemic
Copy link
Contributor

Icemic commented May 19, 2024

In fact, static JSValue JS_ThrowError(JSContext *ctx, JSErrorEnum error_num, const char *fmt, va_list ap) is already exists, but it is a un-exported static function inside quickjs.c.

@saghul
Copy link
Contributor

saghul commented May 21, 2024

Not quite, that function doesn't throw plain Error objects.

saghul added a commit that referenced this issue May 21, 2024
It's a helper for doing the following steps:

- Building an Error object
- Attaching a formatted message
- Throwing the object

Fixes: #375
@saghul
Copy link
Contributor

saghul commented May 21, 2024

PR: #411

saghul added a commit that referenced this issue May 27, 2024
It's a helper for doing the following steps:

- Building an Error object
- Attaching a formatted message
- Throwing the object

Fixes: #375
saghul added a commit that referenced this issue May 27, 2024
It's a helper for doing the following steps:

- Building an Error object
- Attaching a formatted message
- Throwing the object

Fixes: #375
bluesky950520 pushed a commit to bluesky950520/quickjs that referenced this issue Mar 14, 2025
It's a helper for doing the following steps:

- Building an Error object
- Attaching a formatted message
- Throwing the object

Fixes: quickjs-ng/quickjs#375
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants