Address WebApiControllerBase Error Handling Issues
This commit is contained in:
Родитель
9c57b5f51c
Коммит
6bce6d4452
|
@ -1,4 +1,5 @@
|
|||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
|
@ -18,6 +19,11 @@ namespace Microsoft.Azure.Devices.Applications.RemoteMonitoring.DeviceAdmin.Web.
|
|||
{
|
||||
protected async Task<HttpResponseMessage> GetServiceResponseAsync(Func<Task> getData)
|
||||
{
|
||||
if (getData == null)
|
||||
{
|
||||
throw new ArgumentNullException("getData");
|
||||
}
|
||||
|
||||
return await GetServiceResponseAsync<object>(async () =>
|
||||
{
|
||||
await getData();
|
||||
|
@ -34,6 +40,11 @@ namespace Microsoft.Azure.Devices.Applications.RemoteMonitoring.DeviceAdmin.Web.
|
|||
/// <returns></returns>
|
||||
protected async Task<HttpResponseMessage> GetServiceResponseAsync<T>(Func<Task<T>> getData)
|
||||
{
|
||||
if (getData == null)
|
||||
{
|
||||
throw new ArgumentNullException("getData");
|
||||
}
|
||||
|
||||
return await GetServiceResponseAsync(getData, true);
|
||||
}
|
||||
|
||||
|
@ -49,24 +60,41 @@ namespace Microsoft.Azure.Devices.Applications.RemoteMonitoring.DeviceAdmin.Web.
|
|||
{
|
||||
ServiceResponse<T> response = new ServiceResponse<T>();
|
||||
|
||||
if (getData == null)
|
||||
{
|
||||
throw new ArgumentNullException("getData");
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
response.Data = await getData();
|
||||
response.Data = await getData();
|
||||
}
|
||||
catch (ValidationException ex)
|
||||
{
|
||||
foreach (string error in ex.Errors)
|
||||
if (ex.Errors == null)
|
||||
{
|
||||
response.Error.Add(new Error(error));
|
||||
response.Error.Add(new Error(ex.Message));
|
||||
}
|
||||
else
|
||||
{
|
||||
foreach (string error in ex.Errors)
|
||||
{
|
||||
response.Error.Add(new Error(error));
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (DeviceAdministrationExceptionBase ex)
|
||||
{
|
||||
response.Error.Add(new Error(ex.Message));
|
||||
}
|
||||
catch (HttpResponseException ex)
|
||||
{
|
||||
throw ex;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
response.Error.Add(new Error(ex));
|
||||
Debug.Write(FormatExceptionMessage(ex), " GetServiceResponseAsync Exception");
|
||||
}
|
||||
|
||||
// if there's an error or we've been asked to use a service response, then return a service response
|
||||
|
@ -76,7 +104,7 @@ namespace Microsoft.Azure.Devices.Applications.RemoteMonitoring.DeviceAdmin.Web.
|
|||
response.Error != null && response.Error.Any() ? HttpStatusCode.BadRequest : HttpStatusCode.OK,
|
||||
response);
|
||||
}
|
||||
|
||||
|
||||
// otherwise there's no error and we need to return the data at the root of the response
|
||||
return Request.CreateResponse(HttpStatusCode.OK, response.Data);
|
||||
}
|
||||
|
@ -93,15 +121,38 @@ namespace Microsoft.Azure.Devices.Applications.RemoteMonitoring.DeviceAdmin.Web.
|
|||
{
|
||||
ServiceResponse<T> response = new ServiceResponse<T>();
|
||||
|
||||
string errorMessage =
|
||||
string errorMessage =
|
||||
String.Format(
|
||||
CultureInfo.CurrentCulture,
|
||||
Strings.RequestFormatError,
|
||||
Strings.RequestFormatError,
|
||||
parameterName, type);
|
||||
|
||||
response.Error.Add(new Error(errorMessage));
|
||||
|
||||
return Request.CreateResponse(HttpStatusCode.BadRequest, response);
|
||||
}
|
||||
|
||||
protected void TerminateProcessingWithMessage(HttpStatusCode statusCode, string message)
|
||||
{
|
||||
HttpResponseMessage responseMessage = new HttpResponseMessage()
|
||||
{
|
||||
StatusCode = statusCode,
|
||||
ReasonPhrase = message
|
||||
};
|
||||
|
||||
throw new HttpResponseException(responseMessage);
|
||||
}
|
||||
|
||||
private static string FormatExceptionMessage(Exception ex)
|
||||
{
|
||||
Debug.Assert(ex != null, "ex is a null reference.");
|
||||
|
||||
// TODO: Localize string if neccessary.
|
||||
return string.Format(
|
||||
CultureInfo.CurrentCulture,
|
||||
"{0}{0}*** EXCEPTION ***{0}{0}{1}{0}{0}",
|
||||
Console.Out.NewLine,
|
||||
ex);
|
||||
}
|
||||
}
|
||||
}
|
Загрузка…
Ссылка в новой задаче