Skip to content

Commit b5b559b

Browse files
committed
Extract creating the response with error status into the CreateErrorResponse method.
1 parent 816e8be commit b5b559b

File tree

1 file changed

+19
-36
lines changed

1 file changed

+19
-36
lines changed

RestSharp/Http.Async.cs

+19-36
Original file line numberDiff line numberDiff line change
@@ -110,15 +110,26 @@ private HttpWebRequest GetStyleMethodInternalAsync(string method, Action<HttpRes
110110
}
111111
catch(Exception ex)
112112
{
113-
var response = new HttpResponse();
114-
response.ErrorMessage = ex.Message;
115-
response.ErrorException = ex;
116-
response.ResponseStatus = ResponseStatus.Error;
117-
ExecuteCallback(response, callback);
113+
ExecuteCallback(CreateErrorResponse(ex), callback);
118114
}
119115
return webRequest;
120116
}
121117

118+
private HttpResponse CreateErrorResponse(Exception ex)
119+
{
120+
var response = new HttpResponse();
121+
if (ex is WebException && ((WebException)ex).Status == WebExceptionStatus.RequestCanceled)
122+
{
123+
response.ResponseStatus = _timeoutState.TimedOut ? ResponseStatus.TimedOut : ResponseStatus.Aborted;
124+
return response;
125+
}
126+
127+
response.ErrorMessage = ex.Message;
128+
response.ErrorException = ex;
129+
response.ResponseStatus = ResponseStatus.Error;
130+
return response;
131+
}
132+
122133
private HttpWebRequest PutPostInternalAsync(string method, Action<HttpResponse> callback)
123134
{
124135
HttpWebRequest webRequest = null;
@@ -130,11 +141,7 @@ private HttpWebRequest PutPostInternalAsync(string method, Action<HttpResponse>
130141
}
131142
catch(Exception ex)
132143
{
133-
var response = new HttpResponse();
134-
response.ErrorMessage = ex.Message;
135-
response.ErrorException = ex;
136-
response.ResponseStatus = ResponseStatus.Error;
137-
ExecuteCallback(response, callback);
144+
ExecuteCallback(CreateErrorResponse(ex), callback);
138145
}
139146

140147
return webRequest;
@@ -221,21 +228,7 @@ private void RequestStreamCallback(IAsyncResult result, Action<HttpResponse> cal
221228
}
222229
catch (Exception ex)
223230
{
224-
HttpResponse response;
225-
if (ex is WebException && ((WebException)ex).Status == WebExceptionStatus.RequestCanceled)
226-
{
227-
response = new HttpResponse {ResponseStatus = _timeoutState.TimedOut ? ResponseStatus.TimedOut : ResponseStatus.Aborted};
228-
ExecuteCallback (response, callback);
229-
return;
230-
}
231-
232-
response = new HttpResponse
233-
{
234-
ErrorMessage = ex.Message,
235-
ErrorException = ex,
236-
ResponseStatus = ResponseStatus.Error
237-
};
238-
ExecuteCallback(response, callback);
231+
ExecuteCallback(CreateErrorResponse(ex), callback);
239232
return;
240233
}
241234

@@ -328,17 +321,7 @@ private void ResponseCallback(IAsyncResult result, Action<HttpResponse> callback
328321
}
329322
catch(Exception ex)
330323
{
331-
if(ex is WebException && ((WebException)ex).Status == WebExceptionStatus.RequestCanceled)
332-
{
333-
response.ResponseStatus = ResponseStatus.Aborted;
334-
ExecuteCallback(response, callback);
335-
return;
336-
}
337-
338-
response.ErrorMessage = ex.Message;
339-
response.ErrorException = ex;
340-
response.ResponseStatus = ResponseStatus.Error;
341-
ExecuteCallback(response, callback);
324+
ExecuteCallback(CreateErrorResponse(ex), callback);
342325
}
343326
}
344327

0 commit comments

Comments
 (0)