diff --git a/WebAPIDataStreaming/Controllers/StreamFilesController.cs b/WebAPIDataStreaming/Controllers/StreamFilesController.cs index d0af268..17fb72a 100644 --- a/WebAPIDataStreaming/Controllers/StreamFilesController.cs +++ b/WebAPIDataStreaming/Controllers/StreamFilesController.cs @@ -66,15 +66,9 @@ public HttpResponseMessage GetFileMetaData(string fileName) catch (Exception exception) { // Log exception and return gracefully - - if (string.IsNullOrWhiteSpace(exception.Message)) - { - metaData.FileResponseMessage.Content = string.Concat("Exception : - StackTrace : ", exception.StackTrace); - } - else - { - metaData.FileResponseMessage.Content = string.Concat("Exception : - Message : ", exception.Message, " ", "StackTrace : ", exception.StackTrace); - } + + metaData = new FileMetaData(); + metaData.FileResponseMessage.Content = ProcessException(exception); return Request.CreateResponse(HttpStatusCode.InternalServerError, metaData, new MediaTypeHeaderValue("text/json")); } } @@ -110,35 +104,24 @@ public HttpResponseMessage SearchFileInDownloadDirectory(string fileName) return Request.CreateResponse(HttpStatusCode.OK, filesMetaData, new MediaTypeHeaderValue("text/json")); } - return Request.CreateResponse(HttpStatusCode.NotFound, - new FileMetaData() + filesMetaData.Add(new FileMetaData() { FileResponseMessage = new FileResponseMessage { IsExists = false, Content = string.Format("{0} file is not found !", fileName) } - }, new MediaTypeHeaderValue("text/json")); + }); + + return Request.CreateResponse(HttpStatusCode.NotFound, filesMetaData, new MediaTypeHeaderValue("text/json")); } catch (Exception exception) { - if (!string.IsNullOrWhiteSpace(exception.Message) && !string.IsNullOrWhiteSpace(exception.StackTrace)) - { - metaData.FileResponseMessage.Content = string.Concat(exMessage, " Exception : - Message : ", exception.Message, " ", "StackTrace : ", exception.StackTrace); - } - else if (!string.IsNullOrWhiteSpace(exception.Message) && string.IsNullOrWhiteSpace(exception.StackTrace)) - { - metaData.FileResponseMessage.Content = string.Concat(exMessage, " Exception : - Message : ", exception.Message); - } - else if (string.IsNullOrWhiteSpace(exception.Message) && !string.IsNullOrWhiteSpace(exception.StackTrace)) - { - metaData.FileResponseMessage.Content = string.Concat(exMessage, " Exception : - StackTrace : ", exception.StackTrace); - } - else - { - metaData.FileResponseMessage.Content = exMessage; - } - return Request.CreateResponse(HttpStatusCode.InternalServerError, metaData, new MediaTypeHeaderValue("text/json")); + // Log exception and return gracefully + metaData = new FileMetaData(); + metaData.FileResponseMessage.Content = ProcessException(exception); + filesMetaData.Add(metaData); + return Request.CreateResponse(HttpStatusCode.InternalServerError, filesMetaData, new MediaTypeHeaderValue("text/json")); } } @@ -193,24 +176,10 @@ public HttpResponseMessage DownloadFile(string fileName) } catch (Exception exception) { - if (!string.IsNullOrWhiteSpace(exception.Message) && !string.IsNullOrWhiteSpace(exception.StackTrace)) - { - metaData.FileResponseMessage.Content = string.Concat(exMessage, " Exception : - Message : ", exception.Message, " ", "StackTrace : ", exception.StackTrace); - } - else if (!string.IsNullOrWhiteSpace(exception.Message) && string.IsNullOrWhiteSpace(exception.StackTrace)) - { - metaData.FileResponseMessage.Content = string.Concat(exMessage, " Exception : - Message : ", exception.Message); - } - else if (string.IsNullOrWhiteSpace(exception.Message) && !string.IsNullOrWhiteSpace(exception.StackTrace)) - { - metaData.FileResponseMessage.Content = string.Concat(exMessage, " Exception : - StackTrace : ", exception.StackTrace); - } - else - { - metaData.FileResponseMessage.Content = exMessage; - } + // Log exception and return gracefully + metaData = new FileMetaData(); + metaData.FileResponseMessage.Content = ProcessException(exception); response = Request.CreateResponse(HttpStatusCode.InternalServerError, metaData, new MediaTypeHeaderValue("text/json")); - } return response; } @@ -248,22 +217,7 @@ public HttpResponseMessage UploadFile(bool overWrite) { // Log exception and return gracefully fileResponseMessage = new FileResponseMessage { IsExists = false }; - if (!string.IsNullOrWhiteSpace(exception.Message) && !string.IsNullOrWhiteSpace(exception.StackTrace)) - { - fileResponseMessage.Content = string.Concat(exMessage, " Exception : - Message : ", exception.Message, " ", "StackTrace : ", exception.StackTrace); - } - else if (!string.IsNullOrWhiteSpace(exception.Message) && string.IsNullOrWhiteSpace(exception.StackTrace)) - { - fileResponseMessage.Content = string.Concat(exMessage, " Exception : - Message : ", exception.Message); - } - else if (string.IsNullOrWhiteSpace(exception.Message) && !string.IsNullOrWhiteSpace(exception.StackTrace)) - { - fileResponseMessage.Content = string.Concat(exMessage, " Exception : - StackTrace : ", exception.StackTrace); - } - else - { - fileResponseMessage.Content = exMessage; - } + fileResponseMessage.Content = ProcessException(exception); fileResponseMessages.Add(fileResponseMessage); response = Request.CreateResponse(HttpStatusCode.InternalServerError, fileResponseMessages, new MediaTypeHeaderValue("text/json")); } @@ -358,5 +312,29 @@ private HttpResponseMessage ProcessUploadRequest(bool overWrite) } return response; } + + private string ProcessException(Exception exception) + { + if (exception == null) + { + return exMessage; + } + if (!string.IsNullOrWhiteSpace(exception.Message) && !string.IsNullOrWhiteSpace(exception.StackTrace)) + { + return string.Concat(exMessage, " Exception : - Message : ", exception.Message, " ", "StackTrace : ", exception.StackTrace); + } + else if (!string.IsNullOrWhiteSpace(exception.Message) && string.IsNullOrWhiteSpace(exception.StackTrace)) + { + return string.Concat(exMessage, " Exception : - Message : ", exception.Message); + } + else if (string.IsNullOrWhiteSpace(exception.Message) && !string.IsNullOrWhiteSpace(exception.StackTrace)) + { + return string.Concat(exMessage, " Exception : - StackTrace : ", exception.StackTrace); + } + else + { + return exMessage; + } + } } }