Skip to content

Commit 60ccb88

Browse files
committed
Moving the error handling
1 parent 074a673 commit 60ccb88

File tree

4 files changed

+35
-29
lines changed

4 files changed

+35
-29
lines changed

docs/.vuepress/config.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ module.exports = {
2727
"/usage/serialization",
2828
"/usage/files",
2929
"/usage/authenticators",
30-
"/usage/parameters"
30+
"/usage/parameters",
31+
"/usage/exceptions"
3132
]
3233
},
3334
{

docs/getting-started/README.md

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -57,32 +57,5 @@ throw an exception.
5757
All `ExecuteAsync` overloads, however, behave in the same way as `Execute` and return
5858
the `IRestResponse` or `IRestResponse<T>`.
5959

60-
See below about how RestSharp handles exceptions.
60+
Read [here](../usage/exceptions.md) about how RestSharp handles exceptions.
6161

62-
## Note About Error Handling
63-
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-
69-
Normally, RestSharp doesn't throw an exception if the request fails.
70-
71-
However, it is possible to configure RestSharp to throw in different situations, when it normally doesn't throw
72-
in favour of giving you the error as a property.
73-
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. |
79-
80-
Those properties are available for the `IRestClient` instance and will be used for all request made with that instance.
81-
82-
There are also slight differences on how different overloads handle exceptions.
83-
84-
Asynchronous generic methods `GetAsync<T>`, `PostAsync<T>` and so on, which aren't a part of `IRestClient` interface
85-
(those methods are extension methods) return `Task<T>`. It means that there's no `IRestResponse` to set the response status to error.
86-
We decided to throw an exception when such a request fails. It is a trade-off between the API
87-
consistency and usability of the library. Usually, you only need the content of `RestResponse` instance to diagnose issues
88-
and most of the time the exception would tell you what's wrong.

docs/usage/exceptions.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Error Handling
2+
3+
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`.
4+
5+
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`.
6+
The `Status` property is an indicator of completion independent of the API error handling.
7+
8+
Normally, RestSharp doesn't throw an exception if the request fails.
9+
10+
However, it is possible to configure RestSharp to throw in different situations, when it normally doesn't throw
11+
in favour of giving you the error as a property.
12+
13+
| Property | Behavior |
14+
| ------------- |-------------|
15+
| `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. |
16+
| `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. |
17+
| `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. |
18+
19+
Those properties are available for the `IRestClient` instance and will be used for all request made with that instance.
20+
21+
There are also slight differences on how different overloads handle exceptions.
22+
23+
Asynchronous generic methods `GetAsync<T>`, `PostAsync<T>` and so on, which aren't a part of `IRestClient` interface
24+
(those methods are extension methods) return `Task<T>`. It means that there's no `IRestResponse` to set the response status to error.
25+
We decided to throw an exception when such a request fails. It is a trade-off between the API
26+
consistency and usability of the library. Usually, you only need the content of `RestResponse` instance to diagnose issues
27+
and most of the time the exception would tell you what's wrong.

docs/usage/serialization.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33
RestSharp has JSON and XML serializers built in without any additional packages
44
or configuration. There are also a few JSON serializers provided as additional packages.
55

6+
:::tip
7+
The default behavior of RestSharp is to swallow deserialization errors and return `null` in the `Data`
8+
property of the response. Read more about it in the [Error Handling](exceptions.md).
9+
:::
10+
611
## Default Serializers
712

813
The default JSON serializer uses the forked version of `SimpleJson`. It is very simplistic and

0 commit comments

Comments
 (0)