2

i'm trying to handle an exception from native module in react native for windows, i,ve written the native module following the react native for windows docs but when i trigger an error in the native module, i expect the function App_UnhandledException or TaskScheduler_UnobservedTaskException catch the exception but it doesn't.

Are they any way to catch the exception from native modules?

App.xaml.cs

using Microsoft.ReactNative;
using System.Diagnostics;
using System.Threading.Tasks;
using Windows.ApplicationModel.Activation;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;

namespace testprojecthandlederrors
{
sealed partial class App : ReactApplication
{
    public App()
    {
   #if BUNDLE
        JavaScriptBundleFile = "index.windows";
        InstanceSettings.UseWebDebugger = false;
        InstanceSettings.UseFastRefresh = false;
#else
        JavaScriptBundleFile = "index";
        InstanceSettings.UseWebDebugger = true;
        InstanceSettings.UseFastRefresh = true;
#endif

#if DEBUG
        InstanceSettings.UseDeveloperSupport = true;
#else
        InstanceSettings.UseDeveloperSupport = false;
#endif

        Microsoft.ReactNative.Managed.AutolinkedNativeModules.RegisterAutolinkedNativeModulePackages(PackageProviders); // Includes any autolinked modules

        PackageProviders.Add(new Microsoft.ReactNative.Managed.ReactPackageProvider());
        PackageProviders.Add(new ReactPackageProvider());
        this.UnhandledException += App_UnhandledException;
        TaskScheduler.UnobservedTaskException += TaskScheduler_UnobservedTaskException;
        InitializeComponent();
    }

    private void TaskScheduler_UnobservedTaskException(object sender, UnobservedTaskExceptionEventArgs e)
    {
        var e_message = e.Exception.Message;
        Debug.WriteLine("Error Detected from Another thread", e_message);
    }

    private void App_UnhandledException(object sender, UnhandledExceptionEventArgs e)
    {
        var e_message = e.Exception.Message;
        Debug.WriteLine("Error Detected from Main thread", e_message);
    }

    /// <summary>
    /// Invoked when the application is launched normally by the end user.  Other entry points
    /// will be used such as when the application is launched to open a specific file.
    /// </summary>
    /// <param name="e">Details about the launch request and process.</param>
    protected override void OnLaunched(LaunchActivatedEventArgs e)
    {
        base.OnLaunched(e);
        var frame = (Frame)Window.Current.Content;
        frame.Navigate(typeof(MainPage), e.Arguments);
    }



    /// <summary>
    /// Invoked when the application is activated by some means other than normal launching.
    /// </summary>
    protected override void OnActivated(IActivatedEventArgs e)
    {
        var preActivationContent = Window.Current.Content;
        base.OnActivated(e);
        if (preActivationContent == null && Window.Current != null)
        {
            // Display the initial content
            var frame = (Frame)Window.Current.Content;
            frame.Navigate(typeof(MainPage), null);
        }
    }
}
}

CustomModule.cs

using Microsoft.ReactNative.Managed;

namespace testprojecthandlederrors
{
[ReactModule]
class CustomModule
{
    [ReactMethod("saveImage")]
    public void SaveImage(string base64)
    {
       //Throw error
       var base64String = base64.Replace("data:image/png;base64,", string.Empty);
    }
}
}

App.js

...
const testException = () => {
  NativeModules.CustomModule.saveImage();
}
... 

0 Answers0