Skip to content

Commit e8a51d2

Browse files
committed
Documented more about exception handling
1 parent cad950a commit e8a51d2

File tree

2 files changed

+28
-6
lines changed

2 files changed

+28
-6
lines changed

docs/getting-started/README.md

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,16 +49,38 @@ var request = new RestRequest("statuses/home_timeline.json", DataFormat.Json);
4949
var timeline = await client.GetAsync<HomeTimeline>(request, cancellationToken);
5050
```
5151

52+
The most important difference, however, that async methods that are named after
53+
HTTP methods return the `Task<T>` instead of `Task<IRestResponse<T>>`. Because it
54+
means that you won't get an error response if the request fails, those methods
55+
throw an exception.
56+
57+
All `ExecuteAsync` overloads, however, behave in the same way as `Execute` and return
58+
the `IRestResponse` or `IRestResponse<T>`.
59+
60+
See below about how RestSharp handles exceptions.
61+
5262
## Note About Error Handling
5363

64+
If there is a network transport error (network is down, failed DNS lookup, etc), `RestResponse.ResponseStatus` will be set to `ResponseStatus.Error`, otherwise it will be `ResponseStatus.Completed`.
65+
66+
If an API returns a 404, `ResponseStatus` will still be `Completed`. If you need access to the HTTP status code returned you will find it at `RestResponse.StatusCode`.
67+
The `Status` property is an indicator of completion independent of the API error handling.
68+
5469
Normally, RestSharp doesn't throw an exception if the request fails.
5570

5671
However, it is possible to configure RestSharp to throw in different situations, when it normally doesn't throw
5772
in favour of giving you the error as a property.
5873

59-
[TODO] - add exception handling
74+
| Property | Behavior |
75+
| ------------- |:-------------:|
76+
| FailOnDeserialization | Changes the default behavior when failed deserialization results in a successful response with an empty `Data` property of the response. Setting this property to `true` will tell RestSharp to consider failed deserialization as an error and set the `ResponseStatus` to `Error` accordingly. |
77+
| ThrowOnDeserialization | Changes the default behavior when failed deserialization results in empty `Data` property of the response. Setting this property to `true` will tell RestSharp to throw when deserialization fails. |
78+
| ThrowOnAnyError | Setting this property to `true` changes the default behavior and forces RestSharp to throw if any errors occurs when making a request or during deserialization. |
6079

61-
If there is a network transport error (network is down, failed DNS lookup, etc), `RestResponse.ResponseStatus` will be set to `ResponseStatus.Error`, otherwise it will be `ResponseStatus.Completed`.
80+
There are also slight differences on how different overloads handle exceptions.
6281

63-
If an API returns a 404, `ResponseStatus` will still be `Completed`. If you need access to the HTTP status code returned you will find it at `RestResponse.StatusCode`.
64-
The `Status` property is an indicator of completion independent of the API error handling.
82+
Asynchronous generic methods `GetAsync<T>`, `PostAsync<T>` and so on, which aren't a part of `IRestClient` interface
83+
(those methods are extension methods) return `Task<T>`. It means that there's no `IRestResponse` to set the response status to error.
84+
We decided to throw an exception when such a request fails. It is a trade-off between the API
85+
consistency and usability of the library. Usually, you only need the content of `RestResponse` instance to diagnose issues
86+
and most of the time the exception would tell you what's wrong.

src/RestSharp/RestClientExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -407,9 +407,9 @@ static void ThrowIfError(IRestResponse response)
407407
{
408408
var exception = response.ResponseStatus switch
409409
{
410-
ResponseStatus.Aborted => new WebException("Request aborted"),
410+
ResponseStatus.Aborted => new WebException("Request aborted", response.ErrorException),
411411
ResponseStatus.Error => response.ErrorException,
412-
ResponseStatus.TimedOut => new TimeoutException("Request timed out"),
412+
ResponseStatus.TimedOut => new TimeoutException("Request timed out", response.ErrorException),
413413
ResponseStatus.None => null,
414414
ResponseStatus.Completed => null,
415415
_ => throw response.ErrorException ?? new ArgumentOutOfRangeException()

0 commit comments

Comments
 (0)