Skip to content

Commit 2871719

Browse files
committed
Better error handling for failed requests (transport errors)
Added parameter validation for int range and string length
1 parent a2d488d commit 2871719

File tree

5 files changed

+59
-2
lines changed

5 files changed

+59
-2
lines changed

RestSharp/Enum.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,11 @@ public enum DateFormat
5656
{
5757

5858
}
59+
60+
public enum ResponseStatus
61+
{
62+
None,
63+
Success,
64+
Error
65+
}
5966
}

RestSharp/RestClient.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,10 +117,14 @@ private RestResponse GetResponse(RestRequest request) {
117117
response = _http.Options(request.GetUri(), @params);
118118
break;
119119
}
120+
121+
response.ResponseStatus = ResponseStatus.Success;
120122
}
121123
catch (Exception ex) {
122-
// TODO: handle transport errors better
123-
response = new RestResponse { Content = ex.Message };
124+
response = new RestResponse {
125+
ErrorMessage = ex.Message,
126+
ResponseStatus = ResponseStatus.Error
127+
};
124128
}
125129

126130
return response;

RestSharp/RestResponse.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,17 @@ public class RestResponse
2727
public string StatusDescription { get; set; }
2828
public Uri ResponseUri { get; set; }
2929
public string Server { get; set; }
30+
31+
private ResponseStatus _responseStatus = ResponseStatus.None;
32+
public ResponseStatus ResponseStatus {
33+
get {
34+
return _responseStatus;
35+
}
36+
set {
37+
_responseStatus = value;
38+
}
39+
}
40+
41+
public string ErrorMessage { get; set; }
3042
}
3143
}

RestSharp/RestSharp.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
<Compile Include="RestClient.cs" />
6464
<Compile Include="RestRequest.cs" />
6565
<Compile Include="Authenticators\SimpleAuthenticator.cs" />
66+
<Compile Include="Validation\Validate.cs" />
6667
<Compile Include="Validation\Require.cs" />
6768
</ItemGroup>
6869
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />

RestSharp/Validation/Validate.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Copyright 2009 John Sheehan
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
using System;
16+
17+
namespace RestSharp.Validation
18+
{
19+
public class Validate
20+
{
21+
public static void IsBetween(int value, int min, int max) {
22+
if (value < min || value > max) {
23+
throw new ArgumentException(string.Format("Value ({0}) is not between {1} and {2}.", value, min, max));
24+
}
25+
}
26+
27+
public static void IsValidLength(string value, int maxSize) {
28+
if (value.Length > maxSize) {
29+
throw new ArgumentException(string.Format("String is longer than max allowed size ({0}).", maxSize));
30+
}
31+
}
32+
}
33+
}

0 commit comments

Comments
 (0)