ErrorHandling.cs
1 using Microsoft.Rest;
2 using System;
3 using System.Collections;
4 using System.Collections.Generic;
5 using System.Linq;
6 using System.Text;
7 using System.Threading.Tasks;
8 
9 namespace Microsoft.Bot.Connector
10 {
11  public static class ErrorHandling
12  {
13  public static async Task HandleErrorAsync(this HttpOperationResponse<object> result)
14  {
15  if (!result.Response.IsSuccessStatusCode)
16  {
17  ErrorResponse errorMessage = result.Body as ErrorResponse;
18 
19  string _requestContent = null;
20  if (result.Request != null && result.Request.Content != null)
21  {
22  try
23  {
24  _requestContent = await result.Request.Content.ReadAsStringAsync().ConfigureAwait(false);
25  }
26  catch (ObjectDisposedException)
27  {
28  //result.Request.Content is disposed.
29  _requestContent = null;
30  }
31  }
32 
33  string _responseContent = null;
34  if (result.Response != null && result.Response.Content != null)
35  {
36  try
37  {
38  _responseContent = await result.Response.Content.ReadAsStringAsync().ConfigureAwait(false);
39  }
40  catch (ObjectDisposedException)
41  {
42  _responseContent = null;
43  }
44  }
45 
46  throw new HttpOperationException(String.IsNullOrEmpty(errorMessage?.Error?.Message) ? result.Response.ReasonPhrase : errorMessage?.Error?.Message)
47  {
48  Request = new HttpRequestMessageWrapper(result.Request, _requestContent),
49  Response = new HttpResponseMessageWrapper(result.Response, _responseContent),
50  Body = result.Body
51  };
52  }
53  }
54 
55  public static async Task<ErrorResponse> HandleErrorAsync(this HttpOperationResponse<ErrorResponse> result)
56  {
57  if (!result.Response.IsSuccessStatusCode)
58  {
59  ErrorResponse errorMessage = result.Body as ErrorResponse;
60 
61  string _requestContent = null;
62  if (result.Request != null && result.Request.Content != null)
63  {
64  try
65  {
66  _requestContent = await result.Request.Content.ReadAsStringAsync().ConfigureAwait(false);
67  }
68  catch (ObjectDisposedException)
69  {
70  //result.Request.Content is disposed.
71  _requestContent = null;
72  }
73  }
74 
75  string _responseContent = null;
76  if (result.Response != null && result.Response.Content != null)
77  {
78  try
79  {
80  _responseContent = await result.Response.Content.ReadAsStringAsync().ConfigureAwait(false);
81  }
82  catch (ObjectDisposedException)
83  {
84  _responseContent = null;
85  }
86  }
87 
88  throw new HttpOperationException(String.IsNullOrEmpty(errorMessage?.Error?.Message) ? result.Response.ReasonPhrase : errorMessage?.Error?.Message)
89  {
90  Request = new HttpRequestMessageWrapper(result.Request, _requestContent),
91  Response = new HttpResponseMessageWrapper(result.Response, _responseContent),
92  Body = result.Body
93  };
94  }
95  return result.Body;
96  }
97 
98  public static async Task<ObjectT> HandleErrorAsync<ObjectT>(this HttpOperationResponse<object> result)
99  {
100  if (!result.Response.IsSuccessStatusCode)
101  {
102  ErrorResponse errorMessage = result.Body as ErrorResponse;
103 
104  string _requestContent = null;
105  if (result.Request != null && result.Request.Content != null)
106  {
107  try
108  {
109  _requestContent = await result.Request.Content.ReadAsStringAsync().ConfigureAwait(false);
110  }
111  catch (ObjectDisposedException)
112  {
113  //result.Request.Content is disposed.
114  _requestContent = null;
115  }
116  }
117 
118  string _responseContent = null;
119  if (result.Response != null && result.Response.Content != null)
120  {
121  try
122  {
123  _responseContent = await result.Response.Content.ReadAsStringAsync().ConfigureAwait(false);
124  }
125  catch (ObjectDisposedException)
126  {
127  _responseContent = null;
128  }
129  }
130 
131  throw new HttpOperationException(String.IsNullOrEmpty(errorMessage?.Error?.Message) ? result.Response.ReasonPhrase : errorMessage?.Error?.Message)
132  {
133  Request = new HttpRequestMessageWrapper(result.Request, _requestContent),
134  Response = new HttpResponseMessageWrapper(result.Response, _responseContent),
135  Body = result.Body
136  };
137  }
138  if (typeof(ObjectT).IsArray)
139  {
140  IList list = (IList)result.Body;
141  if (list == null)
142  {
143  return default(ObjectT);
144  }
145  IList array = (IList)Array.CreateInstance(typeof(ObjectT).GetElementType(), list.Count);
146  int i = 0;
147  foreach (var el in list)
148  array[i++] = el;
149  return (ObjectT)array;
150  }
151  return (ObjectT)result.Body;
152  }
153  }
154 }
static async Task< ErrorResponse > HandleErrorAsync(this HttpOperationResponse< ErrorResponse > result)
string Message
Error message
Definition: Error.cs:43
static async Task HandleErrorAsync(this HttpOperationResponse< object > result)