From b7a5f4d4a3e21e7d2484babd33ca60a252085ccf Mon Sep 17 00:00:00 2001 From: Jonathan Jenne Date: Wed, 15 Apr 2020 14:44:42 +0200 Subject: [PATCH] WIP: EDMI: Improve Error Handling --- GUIs.Test.EDMIBenchmark/Form1.vb | 10 ++++++++-- Service.EDMIService/EDMIService.vb | 27 ++++++++++++++++++++++---- Service.EDMIService/EDMIService.vbproj | 1 + 3 files changed, 32 insertions(+), 6 deletions(-) diff --git a/GUIs.Test.EDMIBenchmark/Form1.vb b/GUIs.Test.EDMIBenchmark/Form1.vb index b412c86a..10e41ccf 100644 --- a/GUIs.Test.EDMIBenchmark/Form1.vb +++ b/GUIs.Test.EDMIBenchmark/Form1.vb @@ -115,6 +115,9 @@ Public Class Form1 Private Async Sub ButtonLoadFile_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles ButtonLoadFile.ItemClick Try + Dim oSWTotal As New Stopwatch() + oSWTotal.Start() + If TextboxObejctId.EditValue = "" Then MsgBox("Please enter an object id!", MsgBoxStyle.Exclamation, "Uh oh!") End If @@ -126,9 +129,12 @@ Public Class Form1 oResponse.FileContents.CopyTo(oMemoryStream) oMemoryStream.Position = 0 - listboxLog.Items.Add("Stream read!") - DocumentViewer1.LoadFile(oResponse.FileName, oMemoryStream) + + oSWTotal.Stop() + listboxLog.Items.Add($"File [{oResponse.FileName}] loaded: [{FormatTime(oSWTotal.ElapsedMilliseconds)}]") + Catch ex As FaultException + MsgBox(ex.Reason.ToString, MsgBoxStyle.Critical, "Error from Service") Catch ex As Exception MsgBox(ex.Message, MsgBoxStyle.Critical, "Uh oh!") End Try diff --git a/Service.EDMIService/EDMIService.vb b/Service.EDMIService/EDMIService.vb index 55bd54d5..05a412c1 100644 --- a/Service.EDMIService/EDMIService.vb +++ b/Service.EDMIService/EDMIService.vb @@ -301,6 +301,13 @@ Public Class EDMIService #End Region #Region "Document" + ''' + ''' + ''' + ''' + ''' + ''' + ''' Public Function ImportFile(FileName As String, Contents() As Byte, AddedWho As String) As DocumentResult Implements IEDMIService.ImportFile Dim oDocumentType As String = "DummyDocumentType" Dim oRelativePath As String = EDMIPath.GetRelativePath(oDocumentType, FileName) @@ -350,7 +357,7 @@ Public Class EDMIService Dim oPath As String = MSSQL.GetScalarValue(oSQL) If IsNothing(oPath) Then - Return Nothing + Throw New FaultException($"Object [{Data.ObjectId}] does not exist in database!") End If Dim oFullPath = EDMIPath.GetActivePathFromRelativePath(oPath) @@ -358,17 +365,29 @@ Public Class EDMIService _logger.Debug("GetFileByObjectId: Loading file [{0}]", oFullPath) Dim oFileInfo As New FileInfo(oFullPath) - Dim oSource As FileStream = IO.File.Open(oFullPath, FileMode.Open) + If Not oFileInfo.Exists Then + Throw New FaultException($"Object [{Data.ObjectId}] does not exist on filesystem!") + End If + + Dim oDestination As New MemoryStream() + Using oSource As FileStream = IO.File.OpenRead(oFullPath) + oSource.CopyTo(oDestination) + End Using + + oDestination.Seek(0, SeekOrigin.Begin) Dim oMessage As New Messages.DocumentStreamResponse() With { .FileName = oFileInfo.Name, - .FileContents = oSource + .FileContents = oDestination } Return oMessage + Catch ex As IOException + _logger.Error(ex) + Throw New FaultException($"Object [{Data.ObjectId}] could not be streamed!") Catch ex As Exception _logger.Error(ex) - Return Nothing + Throw New FaultException(ex.Message) End Try End Function diff --git a/Service.EDMIService/EDMIService.vbproj b/Service.EDMIService/EDMIService.vbproj index ad9b533a..316648f8 100644 --- a/Service.EDMIService/EDMIService.vbproj +++ b/Service.EDMIService/EDMIService.vbproj @@ -71,6 +71,7 @@ ..\packages\NLog.4.7.0\lib\net45\NLog.dll +