From 7d4d70a0500b7996c710c0762907f44bdf27c92b Mon Sep 17 00:00:00 2001 From: jcesarmobile Date: Tue, 17 Jan 2023 15:18:40 +0100 Subject: [PATCH] fix(ios/android): better http error handling (#6208) --- .../src/main/java/com/getcapacitor/plugin/CapacitorHttp.java | 3 +-- ios/Capacitor/Capacitor/Plugins/HttpRequestHandler.swift | 5 +++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/android/capacitor/src/main/java/com/getcapacitor/plugin/CapacitorHttp.java b/android/capacitor/src/main/java/com/getcapacitor/plugin/CapacitorHttp.java index eb9c624ac..01b5d04ab 100644 --- a/android/capacitor/src/main/java/com/getcapacitor/plugin/CapacitorHttp.java +++ b/android/capacitor/src/main/java/com/getcapacitor/plugin/CapacitorHttp.java @@ -34,8 +34,7 @@ public void run() { JSObject response = HttpRequestHandler.request(call, httpMethod); call.resolve(response); } catch (Exception e) { - System.out.println(e.toString()); - call.reject(e.getClass().getSimpleName(), e); + call.reject(e.getLocalizedMessage(), e.getClass().getSimpleName(), e); } } }; diff --git a/ios/Capacitor/Capacitor/Plugins/HttpRequestHandler.swift b/ios/Capacitor/Capacitor/Plugins/HttpRequestHandler.swift index a155990c1..963b16225 100644 --- a/ios/Capacitor/Capacitor/Plugins/HttpRequestHandler.swift +++ b/ios/Capacitor/Capacitor/Plugins/HttpRequestHandler.swift @@ -170,7 +170,7 @@ class HttpRequestHandler { } catch { // Explicitly reject if the http request body was not set successfully, // so as to not send a known malformed request, and to provide the developer with additional context. - call.reject("Error", "REQUEST", error, [:]) + call.reject(error.localizedDescription, (error as NSError).domain, error, nil) return } } @@ -180,7 +180,8 @@ class HttpRequestHandler { let task = urlSession.dataTask(with: urlRequest) { (data, response, error) in urlSession.invalidateAndCancel() - if error != nil { + if let error = error { + call.reject(error.localizedDescription, (error as NSError).domain, error, nil) return }