Better Logging and Error Handling

This commit is contained in:
Jonathan Jenne
2022-04-21 15:50:30 +02:00
parent 31e0835914
commit 85318a3e81
2 changed files with 101 additions and 44 deletions

View File

@@ -219,15 +219,21 @@ Namespace Winline
Async Function ExportDocumentFromWinline(pDocument As Entities.ExportDocument, pTemplate As Template, pMandator As Mandator, Optional pIsTest As Boolean = False) As Task(Of Boolean)
Dim oWS = Config
Logger.Info("Exporting document from Winline")
' --- Build all teh filenamez and pathz
Dim oBaseFileName As String = FileEx.GetDateTimeString()
Dim oFileName = $"{pTemplate.Name}-{oBaseFileName}.xml"
'Dim oFileName = FileEx.GetFilenameWithPrefix(oBaseFileName, pTemplate.Name, "xml")
Logger.Info("Filename will be [{0}]", oFileName)
' Save the filename to the document
pDocument.FilenameExport = oFileName
RaiseEvent WebServiceProgress(Me, "Dateien schreiben")
RaiseEvent WebServiceProgress(Me, "Abfrage vorbereiten")
Logger.Debug("Preparing WebService call")
' --- Prepare URL and HTTP Client
Dim oTemplateType = 30
@@ -242,6 +248,8 @@ Namespace Winline
oActionCode = 0
End If
Logger.Debug("This is a test: [{0}]", pIsTest)
Dim oURL As String = $"{oWS.BaseUrl}/ewlservice/export?User={oWS.Username}&Password={oWS.Password}&Company={pMandator.Id}&Type={oTemplateType}&Vorlage={oTemplateName}&ActionCode={oActionCode}&Key={oKey}"
Dim oClient As New HttpClient()
@@ -252,6 +260,8 @@ Namespace Winline
' --- Bring the action!
Try
Dim oResponse As HttpResponseMessage = Await oClient.GetAsync(oURL)
Logger.Info("Request ended with code: [{0}]", oResponse.StatusCode)
Await HandleExportResponse(oResponse, pDocument, pTemplate, pMandator, oBaseFileName)
Return True
@@ -268,12 +278,14 @@ Namespace Winline
Private Async Function HandleExportResponse(pResponse As HttpResponseMessage, pDocument As Entities.ExportDocument, pTemplate As Template, pMandator As Mandator, pBaseFileName As String) As Task
pResponse.EnsureSuccessStatusCode()
Dim oResponseBody As String = Await pResponse.Content.ReadAsStringAsync()
Dim oContentType = pResponse.Content.Headers.ContentType.MediaType
Dim oSerializer = Serializer.GetSerializer(GetType(Templates.Entities.MESOWebServiceResult))
RaiseEvent WebServiceProgress(Me, "Antwort verarbeiten")
Logger.Debug("Postprocessing response.")
oResponseBody = ApplyItemFunctionsForExport(pDocument, pTemplate, pMandator, oResponseBody)
Select Case oContentType
@@ -300,6 +312,8 @@ Namespace Winline
oDoc.LoadXml(oResponseBody)
For Each oTable In pTemplate.Tables
Logger.Debug("Processing Table [{0}]", oTable.Name)
For Each oItem As Template.Column In oTable.Columns
Dim oTableName As String = oTable.Name
Dim oItemName As String = oItem.Name
@@ -308,17 +322,21 @@ Namespace Winline
Continue For
End If
Logger.Debug("Processing item [{0}]", oItemName)
Dim oFunction = oItem.Config.Function.Name
Dim oPath = $"//MESOWebService/{oTableName}/{oItemName}"
Dim oNodes As XmlNodeList = oDoc.SelectNodes(oPath)
Logger.Debug("Calling function [{0}] on note [{1}]", oFunction, oPath)
For Each oNode As XmlNode In oNodes
If oItem.Config.Function.Name = "GLN" Then
Dim oGLN = Winline.TryGetGLN(oNode.InnerText, pMandator)
If oGLN Is Nothing Then
Throw New Exceptions.MissingAttributeException("GLN")
Throw New MissingAttributeException("GLN")
End If
oNode.InnerText = oGLN
@@ -327,7 +345,11 @@ Namespace Winline
Dim oEAN = Winline.TryGetEAN(oNode.InnerText, pMandator)
If oEAN Is Nothing Then
Throw New Exceptions.MissingAttributeException("EAN")
' 21.04.2022: Relax the EAN Check
' Since it is possible to have articles without a proper EAN in export,
' we dont throw here, but leave the original value in case of a failure.
' Throw New Exceptions.MissingAttributeException("EAN")
Logger.Warn("EAN could not be retrieved for Node {0}. Skipping.", oNode.Name)
End If
oNode.InnerText = oEAN
@@ -337,7 +359,7 @@ Namespace Winline
Dim oValue = Database.GetScalarValue(oSQL)
If oValue Is Nothing Then
Throw New Exceptions.MissingAttributeException("SQL")
Throw New MissingAttributeException("SQL")
End If
oNode.InnerText = oValue