Skip to content

fix: native methods expecting a NSError arg will now throw a JS exception if the error arg is not passed#311

Merged
NathanWalker merged 3 commits intoNativeScript:mainfrom
Akylas:error_throw_js
Mar 11, 2026
Merged

fix: native methods expecting a NSError arg will now throw a JS exception if the error arg is not passed#311
NathanWalker merged 3 commits intoNativeScript:mainfrom
Akylas:error_throw_js

Conversation

@farfromrefug
Copy link
Copy Markdown
Contributor

This is what we discussed on Discord.
If a native obj-c method like this openReturningError(error?) is called without the error argument it will throw a JS Error if an error is "triggered"
The JS Error named "NSError" will have a localizedDescription as the message and code,domain,nativeException properties.

@edusperoni dont hesitate to comment on the actual code. I am no v8 expert and i was helped by AI.
I tested the code and it works.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR enhances NSError handling in the NativeScript runtime by wrapping native NSError objects in JavaScript Error objects with proper stack traces and metadata. When Objective-C methods with NSError** parameters are called without passing an error reference and an error occurs, the runtime now throws a proper JavaScript Error instead of a generic NativeScriptException.

Key changes:

  • Modified error handling in Interop.mm to create JavaScript Error objects with attached NSError properties (code, domain, nativeException)
  • Added comprehensive test coverage for NSError wrapping behavior in both throw and non-throw scenarios

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
NativeScript/runtime/Interop.mm Replaced simple NativeScriptException throw with creation of JavaScript Error object that wraps NSError with proper stack trace and metadata
TestRunner/app/tests/ApiTests.js Added two new test cases to verify NSError wrapping behavior when called with and without error reference parameter

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +1548 to +1549
ObjCDataWrapper* wrapper = new ObjCDataWrapper(error);
Local<Value> nativeWrapper = ArgConverter::CreateJsWrapper(context, wrapper, Local<Object>(), true);
Copy link

Copilot AI Nov 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The ObjCDataWrapper is created with skipGCRegistration=true in CreateJsWrapper, but there's no explicit cleanup if the Error object creation or subsequent Set operations fail. Consider using a smart pointer or ensuring the wrapper is properly cleaned up in the error path to prevent memory leaks.

Copilot uses AI. Check for mistakes.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@NathanWalker NathanWalker merged commit 217873b into NativeScript:main Mar 11, 2026
3 checks passed
@edusperoni
Copy link
Copy Markdown
Collaborator

Wouldn't this mean that calls that now work fine will instead throw errors and crash apps?

@farfromrefug
Copy link
Copy Markdown
Contributor Author

Wouldn't this mean that calls that now work fine will instead throw errors and crash apps?

the code only do something if error is not null so i dont see how it would do something when calls work fine?
I ran the tests and it worked fine. If you see any issue let me know

@farfromrefug
Copy link
Copy Markdown
Contributor Author

@edusperoni sorry to bump but do you really see an issue? as i said it seems to me that i am doing something only if error is not null

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

Successfully merging this pull request may close these issues.

4 participants