http
http copied to clipboard
HTTP Get not handling connection errors properly
Describe the bug When a call to HTTP Get results in a connection error (e.g. "ConnectException") the routine Capacitor.fromNative traps trying to invoke
new Capacitor.Exception('')
which does not exist. As a result neither the resolve nor the reject functions of the promise are invoked.
To Reproduce Steps to reproduce the behavior: run the following code
Capacitor.Plugins.Http.get({url: 'https://www.nonexistant.com'}). then( function(response) { console.log('Success:', response.data); }, function (err) { console.log('Error:', err); });
The console displays:
(index):218 native Http.get (#64819015)
undefined
(index):192 result Http.get (#64819015)
(index):200 {message: 'ConnectException'}
(anonymous) @ (index):200
cap.fromNative @ (index):400
(anonymous) @ VM24:1
(index):446 TypeError: cap.Exception is not a constructor
at Object.cap.fromNative ((index):413:32)
at
Expected behavior The reject routine should be invoked.
Setting Capacitor.Exception = function(){ return {message:''}; }; enables the correct response.
native Http.get (#51752015) undefined (index):192 result Http.get (#51752015) testhttp.js:103 Error: {message: 'ConnectException'}
Smartphone (please complete the following information):
- Device:Google Pixel 4
- OS: Android 12
- Using Capacitor Android
Additional context It occurs to me that this might be an attempt to provide an exception traceback, but this is of no use if the reject routine of the promise is never invoked.
This should be fixed upstream. Find:
@capacitor/android/capacitor/src/main/assets/native-bridge.js
inside your node_modules.
Scroll down to:
function initNativeBridge(win) {
const cap = win.Capacitor || {};
And add this line:
if (!cap.Exception) cap.Exception = Error;
Now it should look like this:
(...)
function initNativeBridge(win) {
const cap = win.Capacitor || {};
if (!cap.Exception) cap.Exception = Error;
(...)
Remember to do npx cap sync after that (just in case, don't know if needed). Then rebuild.