From eacf8e2743ffbaaac4acf1477c015feec4fc41e5 Mon Sep 17 00:00:00 2001 From: Jonathan Jenne Date: Thu, 4 Jul 2019 16:09:14 +0200 Subject: [PATCH] better handling of invalid zugferd files --- Jobs/EDMI/ZUGFeRD/ImportZUGFeRDFiles.vb | 38 +++++++++++++++++++++---- Jobs/Exceptions.vb | 8 ++++++ Modules.Interfaces/ZUGFeRDInterface.vb | 9 +++--- 3 files changed, 46 insertions(+), 9 deletions(-) diff --git a/Jobs/EDMI/ZUGFeRD/ImportZUGFeRDFiles.vb b/Jobs/EDMI/ZUGFeRD/ImportZUGFeRDFiles.vb index 1d3250ad..07a9af46 100644 --- a/Jobs/EDMI/ZUGFeRD/ImportZUGFeRDFiles.vb +++ b/Jobs/EDMI/ZUGFeRD/ImportZUGFeRDFiles.vb @@ -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 = "

The invoice attached to your email has already been processed in our system.

" Dim oEmailData = MoveAndRenameEmailToRejected(args, oFileGroupId) AddToEmailQueue(oFileGroupId, oBody, oEmailData) + + Catch ex As InvalidFerdException + _logger.Error(ex) + + oMoveDirectory = args.ErrorDirectory + + Dim oBody = """ +

Your email contained a ZUGFeRD document but it was incorrectly formatted.

+

Possible reasons include:

+ """ + 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 diff --git a/Jobs/Exceptions.vb b/Jobs/Exceptions.vb index f73d2d47..d8be37c0 100644 --- a/Jobs/Exceptions.vb +++ b/Jobs/Exceptions.vb @@ -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 diff --git a/Modules.Interfaces/ZUGFeRDInterface.vb b/Modules.Interfaces/ZUGFeRDInterface.vb index 3f2cf8e0..e8b43eff 100644 --- a/Modules.Interfaces/ZUGFeRDInterface.vb +++ b/Modules.Interfaces/ZUGFeRDInterface.vb @@ -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