[Foundation] Do expose server errors. (#8990)

Creating a good API is hard. The delegate DOES return two different
errors.

1. Client errors in the error variable in the delegate method.
2. Server errros in the error in the task in the delegate method.

We need to expose both of them to the user in case there is an issue in
any of them. An exception should be thrown ig any is not null.

PD: As per apple docs:

> The only errors your delegate receives through the error parameter are
> client-side errors, such as being unable to resolve the hostname or
> connect to the host. To check for server-side errors, inspect the
> response property of the task parameter received by this callback.
This commit is contained in:
Manuel de la Pena 2020-07-06 10:51:06 -04:00 коммит произвёл GitHub
Родитель 9a17e053b7
Коммит e1d537107f
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
1 изменённых файлов: 3 добавлений и 2 удалений

Просмотреть файл

@ -637,6 +637,7 @@ namespace Foundation {
public override void DidCompleteWithError (NSUrlSession session, NSUrlSessionTask task, NSError error)
{
var inflight = GetInflightData (task);
var serverError = task.Error;
// this can happen if the HTTP request times out and it is removed as part of the cancellation process
if (inflight != null) {
@ -644,12 +645,12 @@ namespace Foundation {
inflight.Stream.TrySetReceivedAllData ();
// send the error or send the response back
if (error != null) {
if (error != null || serverError != null) {
// got an error, cancel the stream operatios before we do anything
inflight.CancellationTokenSource.Cancel ();
inflight.Errored = true;
var exc = inflight.Exception ?? createExceptionForNSError (error);
var exc = inflight.Exception ?? createExceptionForNSError (error ?? serverError); // client errors wont happen if we get server errors
inflight.CompletionSource.TrySetException (exc);
inflight.Stream.TrySetException (exc);
} else {