klippa-app / nativescript-http

The best way to do HTTP requests in NativeScript, a drop-in replacement for the core HTTP with important improvements and additions like proper connection pooling, form data support and certificate pinning
MIT License
35 stars 14 forks source link

Access to native error / request error type to differentiate network errors #61

Open dpattou opened 2 years ago

dpattou commented 2 years ago

Hello, I might have missed something, but it doesn't seem possible to retrieve the original / native errors thrown on iOS and Android by their respective networking libraries in order to determine whether the error is a network error, a timeout error or another kind of error.

Is there a way to get this information ? Or an easy way around the fact that errors returned by the @klippa/nativescript-http plugin are strings ?

For my use case, I managed to naively parse the returned strings and deduce the error but it turns out the iOS implementation returns an Error(error.localizedDescription) so parsing won't work in that case :)

Any tip would be much appreciated.

Thanks, great plugin otherwise !

jerbob92 commented 2 years ago

Hey!

At this moment, I don't think it's possible to get the original native error. I'm also not sure whether NativeScript has something to provide in that. It's probably better to transform these errors in the plugin to standardized TypeScript errors. I would accept Pull Requests for that.

dpattou commented 2 years ago

Thanks for the answer.

We cannot get the actual native error indeed, I meant the 'wrapped' Javascript version.

On iOS the error returned by session.dataTaskWithRequestCompletionHandler(urlRequest, function (data, response, error) {...} seems to be a 'wrapped' Javascript NSError. On Android though, I'm not sure what errors are mapped to (probably some subclass of a java Exception ?).

I guess it would be somewhat complicated to map iOS NSErrors and Android Exceptions to various error types (no network, timeout etc...) but it might be nice to have a way to retrieve the 'original' (but not native :) ) error.

I'm not super familiar with TypeScript errors, but it looks like one would have to create a subclass for that ? (And add an nativeError / underlyingError property to the Error class).

I don't have a lot of time to delve more into this right now but may try to make a pull request later.