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

@@ -116,6 +116,7 @@ Namespace Documents
Private Function IncludeSchema(pDocument As Document, pTemplate As Template) As Document
Logger.Debug("Adding schema to Document object.")
pDocument.Schema = pTemplate
Return pDocument
End Function
@@ -133,8 +134,23 @@ Namespace Documents
''' </MESOWebService>
''' </example>
Private Function LoadDocumentData(pDocument As Document, pTemplate As Template, pTemplateConfig As TemplateConfig) As Document
Dim oText As String = IO.File.ReadAllText(pDocument.FullName)
Dim oDoc = XDocument.Parse(oText)
Logger.Debug("Loading file contents")
Dim oText As String
Dim oDoc As XDocument
Try
Logger.Debug("Loading file from fs..")
oText = IO.File.ReadAllText(pDocument.FullName)
Logger.Debug("Parsing file..")
oDoc = XDocument.Parse(oText)
Catch ex As Exception
Logger.Error(ex)
Throw ex
End Try
Logger.Debug("File Loaded.")
Dim oRootElement As XElement = XmlData.GetElement(oDoc, "MESOWebService")
If oRootElement Is Nothing Then
@@ -181,6 +197,8 @@ Namespace Documents
Where(Function(t) t.Name = oTopLevelElement.Name).
FirstOrDefault()
Logger.Debug("Creating fields from [{0}] columns for Table [{1}]", oTable.Columns, oTable.Name)
For Each oColumn In oTable.Columns
Dim oSubElement = oSubElements.
Where(Function(e) e.Name = oColumn.Name).
@@ -190,6 +208,8 @@ Namespace Documents
Dim oRequired = oColumn.IsRequired
Dim oValue = oSubElement.Value.Trim()
Logger.Debug("Creating existing field from Element: [{0}]", oSubElement.Name.ToString)
' TODO: Needed when we have time for date times
'If oTemplateField.DataType = Constants.ColumnType.Date Then
' Dim oDate = Date.ParseExact(oValue, "yyyy-MM-dd", CultureInfo.InvariantCulture)
@@ -205,6 +225,8 @@ Namespace Documents
.SortKey = oColumnSortKey
})
Else
Logger.Debug("Creating new field from Configuration: [{0}]", oColumn.Name)
Dim oColumnError = FieldError.None
If oColumn.Config?.IsRequired Then
oColumnError = FieldError.MissingValue
@@ -542,59 +564,72 @@ Namespace Documents
End Sub
Private Sub SetAccountByGLN(oRow As DocumentRow, pMandator As Mandator, pNumberField As String, pNameField As String, pParams As Dictionary(Of String, String))
' Try to read the Account number (which is a GLN really) and account Name
Dim oNumberItem As DocumentRow.FieldValue = oRow.Fields.GetOrDefault(pNumberField)
Dim oNameItem As DocumentRow.FieldValue = oRow.Fields.GetOrDefault(pNameField)
Dim oContainsAccountName As Boolean = Not IsNothing(oNameItem)
Try
' Try to read the Account number (which is a GLN really) and account Name
Dim oNumberItem As DocumentRow.FieldValue = oRow.Fields.GetOrDefault(pNumberField)
Dim oNameItem As DocumentRow.FieldValue = oRow.Fields.GetOrDefault(pNameField)
Dim oContainsAccountName As Boolean = Not IsNothing(oNameItem)
If oNumberItem Is Nothing Then
Exit Sub
End If
If oNumberItem Is Nothing Then
Exit Sub
End If
' Try to find an account that matches the GLN
Dim oAlternateField = pParams.GetOrDefault("AltField", String.Empty)
Dim oAccount = Winline.TryGetAccount(oNumberItem.Original, pMandator, "c260", oAlternateField)
' Try to find an account that matches the GLN
Dim oAlternateField = pParams.GetOrDefault("AltField", String.Empty)
Dim oAccount = Winline.TryGetAccount(oNumberItem.Original, pMandator, "c260", oAlternateField)
' If an account was found, set it for External and Final value
If oAccount IsNot Nothing Then
oNumberItem.External = oAccount.Id
oNumberItem.Final = oAccount.Id
' If an account was found, set it for External and Final value
If oAccount IsNot Nothing Then
oNumberItem.External = oAccount.Id
oNumberItem.Final = oAccount.Id
If oContainsAccountName Then
oNameItem.External = oAccount.Name
oNameItem.Final = oAccount.Name
If oContainsAccountName Then
oNameItem.External = oAccount.Name
oNameItem.Final = oAccount.Name
Else
' TODO: What to to if name field is missing or not set?
'oRow.Fields.Add(pNameField, New DocumentRow.FieldValue() With {
' .External = oAccount.Name,
' .Final = oAccount.Name
'})
End If
Else
' TODO: What to to if name field is missing or not set?
'oRow.Fields.Add(pNameField, New DocumentRow.FieldValue() With {
' .External = oAccount.Name,
' .Final = oAccount.Name
'})
' If no account was found and the field is required,
' mark it as error. Otherwise, do nothing.
If oNumberItem.IsRequired Then
oNumberItem.Error = FieldError.AccountNotFound
End If
End If
Else
' If no account was found and the field is required,
' mark it as error. Otherwise, do nothing.
If oNumberItem.IsRequired Then
oNumberItem.Error = FieldError.AccountNotFound
End If
End If
Catch ex As Exception
Logger.Error(ex)
Throw ex
End Try
End Sub
Private Function WrapFileInfo(pFileInfo As FileInfo) As Document
Logger.Debug("Creating new Document object for file [{0}]", pFileInfo.Name)
Return New Document With {.File = pFileInfo}
End Function
Private Function ParseFunctionParamsAsDict(pParams As String) As Dictionary(Of String, String)
Dim oParamsDict As New Dictionary(Of String, String)
Try
Dim oParamsDict As New Dictionary(Of String, String)
If pParams <> String.Empty Then
Dim oParamList = pParams.Split("|").ToList()
For Each oParam In oParamList
Dim oParamSplit = oParam.Split("=")
oParamsDict.Add(oParamSplit(0), oParamSplit(1))
Next
End If
If pParams <> String.Empty Then
Dim oParamList = pParams.Split("|").ToList()
For Each oParam In oParamList
Dim oParamSplit = oParam.Split("=")
If oParamSplit.Count = 2 Then
oParamsDict.Add(oParamSplit(0), oParamSplit(1))
End If
Next
End If
Return oParamsDict
Return oParamsDict
Catch ex As Exception
Logger.Error(ex)
Return New Dictionary(Of String, String)
End Try
End Function
End Class