Skip to content

Commit

Permalink
Refactoring.
Browse files Browse the repository at this point in the history
  • Loading branch information
WondeTadesse committed Nov 20, 2014
1 parent a3b85fd commit d88a943
Showing 1 changed file with 40 additions and 62 deletions.
102 changes: 40 additions & 62 deletions WebAPIDataStreaming/Controllers/StreamFilesController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
}
}
Expand Down Expand Up @@ -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"));
}
}

Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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"));
}
Expand Down Expand Up @@ -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;
}
}
}
}

0 comments on commit d88a943

Please sign in to comment.