diff --git a/DeviceAdministration/Web/WebApiControllers/WebApiControllerBase.cs b/DeviceAdministration/Web/WebApiControllers/WebApiControllerBase.cs index 32a875fe..89b17450 100644 --- a/DeviceAdministration/Web/WebApiControllers/WebApiControllerBase.cs +++ b/DeviceAdministration/Web/WebApiControllers/WebApiControllerBase.cs @@ -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 GetServiceResponseAsync(Func getData) { + if (getData == null) + { + throw new ArgumentNullException("getData"); + } + return await GetServiceResponseAsync(async () => { await getData(); @@ -34,6 +40,11 @@ namespace Microsoft.Azure.Devices.Applications.RemoteMonitoring.DeviceAdmin.Web. /// protected async Task GetServiceResponseAsync(Func> 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 response = new ServiceResponse(); + 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 response = new ServiceResponse(); - 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); + } } } \ No newline at end of file