better handling of invalid zugferd files

This commit is contained in:
Jonathan Jenne 2019-07-04 16:09:14 +02:00
parent 90cd63c484
commit eacf8e2743
3 changed files with 46 additions and 9 deletions

View File

@ -8,6 +8,7 @@ Imports System.Text.RegularExpressions
Imports System.Xml
Imports DigitalData.Modules.Database
Imports DigitalData.Modules.Interfaces
Imports DigitalData.Modules.Interfaces.Exceptions
Imports DigitalData.Modules.Jobs.Exceptions
Imports DigitalData.Modules.Logging
Imports FirebirdSql.Data.FirebirdClient
@ -260,10 +261,23 @@ Public Class ImportZUGFeRDFiles
Try
oDocument = _zugferd.ExtractZUGFeRDFile(oFile.FullName)
Catch ex As Exception
_logger.Warn("File is not a valid ZUGFeRD document! Skipping.")
oFileAttachmentFiles.Add(oFile)
Continue For
Catch ex As ZUGFeRDExecption
Select Case ex.ErrorType
Case ZUGFeRDInterface.ErrorType.NoZugferd
_logger.Warn("File is not a valid ZUGFeRD document! Skipping.")
oFileAttachmentFiles.Add(oFile)
Continue For
Case ZUGFeRDInterface.ErrorType.NoValidZugferd
_logger.Warn("File is an Incorrectly formatted ZUGFeRD document!")
Throw New InvalidFerdException()
Case Else
_logger.Warn("Unexpected Error occurred while extracting ZUGFeRD Information from file {0}", oFile.FullName)
Throw ex
End Select
End Try
oMD5CheckSum = CreateMD5(oFile.FullName)
@ -482,6 +496,20 @@ Public Class ImportZUGFeRDFiles
Dim oBody = "<p>The invoice attached to your email has already been processed in our system.</p>"
Dim oEmailData = MoveAndRenameEmailToRejected(args, oFileGroupId)
AddToEmailQueue(oFileGroupId, oBody, oEmailData)
Catch ex As InvalidFerdException
_logger.Error(ex)
oMoveDirectory = args.ErrorDirectory
Dim oBody = """
<p>Your email contained a ZUGFeRD document but it was incorrectly formatted.</p>
<p>Possible reasons include:<ul>
<li>Amount value has incorrect format (25,01 instead of 25.01)</li>
</ul></p>
"""
Dim oEmailData = MoveAndRenameEmailToRejected(args, oFileGroupId)
AddToEmailQueue(oFileGroupId, oBody, oEmailData)
Catch ex As TooMuchFerdsException
_logger.Error(ex)
@ -549,7 +577,7 @@ Public Class ImportZUGFeRDFiles
End If
End If
_filesystem.MoveTo(oFile.FullName, oFinalMoveDirectory)
_filesystem.MoveTo(oFile.FullName, oFinalMoveDirectory)
_logger.Info("Finished processing file {0}", oFile.Name)
_logger.Info("File moved to {0}", oFinalMoveDirectory)
Next

View File

@ -21,6 +21,14 @@ Public Class Exceptions
End Sub
End Class
Public Class InvalidFerdException
Inherits ApplicationException
Public Sub New()
MyBase.New("ZUGFeRD document found but was not formatted correctly")
End Sub
End Class
Public Class NoFerdsException
Inherits ApplicationException

View File

@ -14,7 +14,8 @@ Public Class ZUGFeRDInterface
Public Enum ErrorType
NoValidFile
ExtractionFailed
NoZugferd
NoValidZugferd
End Enum
Public Sub New(LogConfig As LogConfig)
@ -33,7 +34,7 @@ Public Class ZUGFeRDInterface
Dim oXmlDocument = ValidateZUGFeRDFile(Path)
If IsNothing(oXmlDocument) Then
Throw New ZUGFeRDExecption(ErrorType.ExtractionFailed, "Datei ist kein gültiges ZUGFeRD Format.")
Throw New ZUGFeRDExecption(ErrorType.NoZugferd, "Datei ist keine ZUGFeRD Datei.")
End If
Return SerializeZUGFeRDDocument(oXmlDocument)
@ -70,7 +71,7 @@ Public Class ZUGFeRDInterface
If Not oProcessOutput.ToLower.Contains(ZUGFERD_CONVERTER_SUCCESS_MESSAGE.ToLower) Then
_logger.Warn("File {0} is not a valid ZUGFeRD File!", Path)
Throw New ZUGFeRDExecption(ErrorType.NoValidFile, "Datei ist kein gültiges ZUGFeRD Format.")
Throw New ZUGFeRDExecption(ErrorType.NoZugferd, "Datei ist keine ZUGFeRD Datei.")
End If
Try
@ -93,7 +94,7 @@ Public Class ZUGFeRDInterface
Return oSerializer.Deserialize(oReader)
Catch ex As Exception
_logger.Error(ex)
Throw ex
Throw New ZUGFeRDExecption(ErrorType.NoValidZugferd, "Datei ist eine ungültige ZUGFeRD Datei.")
End Try
End Function
End Class