diff --git a/internal/invoiceintake/intake.go b/internal/invoiceintake/intake.go index 338c3e8..6e5881a 100644 --- a/internal/invoiceintake/intake.go +++ b/internal/invoiceintake/intake.go @@ -268,6 +268,8 @@ func (in *Intake) einvoice(ctx context.Context, m communication, invoices []file return "", true, err // the API user's roles, not the file case s/100 == 4: continue // not an e-invoice ("No machine-readable data …") + case s/100 == 5 && wrongSyntax(err): + continue // e.g. UBL: a retry can't change the file case s/100 == 5: in.retries[m.Name]++ in.cfg.Log("%s: e-invoice import failed (%v), try %d of %d", m.Name, err, in.retries[m.Name], maxEInvoiceRetries) @@ -279,6 +281,14 @@ func (in *Intake) einvoice(ctx context.Context, m communication, invoices []file return "", false, nil } +// wrongSyntax: eu_einvoice reads CII only and answers a UBL XRechnung (or any +// XML with another root element) with a 500, "Invalid XML, found tag +// {…ubl…}Invoice where {…}CrossIndustryInvoice was expected". Retrying only +// delays the plain draft by three runs. +func wrongSyntax(err error) bool { + return strings.Contains(messageOf(err), "Invalid XML, found tag") +} + func (in *Intake) attach(ctx context.Context, doctype, name string, files []file) error { var errs []error for _, f := range files { diff --git a/internal/invoiceintake/intake_test.go b/internal/invoiceintake/intake_test.go index 8968d91..5a0861c 100644 --- a/internal/invoiceintake/intake_test.go +++ b/internal/invoiceintake/intake_test.go @@ -20,6 +20,7 @@ type fakeERP struct { docs map[string][]map[string]any writes []string // "POST " in order einvErr int // status for E Invoice Import of a file without "einv" in its name + einvMsg string // its exception text, "" = eu_einvoice's "no machine-readable data" failPI int // status for every Purchase Invoice create, 0 = accept } @@ -153,7 +154,11 @@ func (f *fakeERP) ServeHTTP(w http.ResponseWriter, r *http.Request) { return } if doctype == "E Invoice Import" && !strings.Contains(fmt.Sprint(doc["einvoice"]), "einv") { - reply(f.einvErr, map[string]any{"exception": "frappe.exceptions.ValidationError: No machine-readable data was found in the PDF file."}) + msg := f.einvMsg + if msg == "" { + msg = "frappe.exceptions.ValidationError: No machine-readable data was found in the PDF file." + } + reply(f.einvErr, map[string]any{"exception": msg}) return } if doctype == "Purchase Invoice" && f.failPI != 0 { @@ -350,6 +355,20 @@ func TestEInvoiceServerErrorIsRetriedThenPlain(t *testing.T) { } } +func TestEInvoiceWrongSyntaxIsPlainAtOnce(t *testing.T) { + f, in := setup(t) + f.einvErr = http.StatusInternalServerError + f.einvMsg = "TypeError: Invalid XML, found tag {urn:oasis:names:specification:ubl:schema:xsd:Invoice-2}Invoice where {urn:un:unece:uncefact:data:standard:CrossIndustryInvoice:100}CrossIndustryInvoice was expected" + f.mail("c1", "billing@hetzner.example", "ubl.xml") + res := run(t, in) + if len(res) != 1 || res[0].Outcome != Draft { + t.Fatalf("results %+v", res) + } + if in.retries["c1"] != 0 { + t.Errorf("retries %d", in.retries["c1"]) + } +} + func TestDryRunCreatesNothing(t *testing.T) { f, in := setup(t) in.cfg.DryRun = true