Skip to content

parser / guides / 09

Errors and findings

The parser reports problems in two ways. If the parser can't read the text, it throws an X12SyntaxError error. If a problem leaves the text readable, the parser returns the problem as a finding and reads the rest of the text.

ts
import { readFile } from 'node:fs/promises';

import { readX12, X12SyntaxError } from '@ediocracy/parser';
import { read271s } from '@ediocracy/parser/271';

try {
    readX12('not x12');
} catch (error) {
    if (!(error instanceof X12SyntaxError)) {
        throw error;
    }

    console.log('Cannot read:', error.code, 'at offset', error.offset);
}

const text = await readFile('response.x12', 'utf8');
const document = readX12(text);

for (const finding of document.findings) {
    console.log('Envelope:', finding.code, finding.message);
}

for (const { response } of read271s(document)) {
    for (const finding of response.findings) {
        console.log('271:', finding.code, finding.message);
    }
}

X12SyntaxError ​

The readX12 function throws an X12SyntaxError error when the text has no ISA segment, an envelope has no trailer, or a segment is outside the envelope that it belongs to. The error has a code property, which contains a value from SyntaxErrorCode, and an offset property, which contains the position in the text where the parser found the problem.

Findings ​

Each Finding has a code, a message that names the elements involved, and the segment where the parser found the problem.

The parser returns findings in the following properties:

  • The X12Document.findings property uses codes from ReaderFindingCode. These codes report control numbers or counts in a trailer that don't match, a missing control number, or a segment that ends with a separator.
  • The Response271.findings property uses codes from Response271FindingCode. These codes report a segment that the 005010X279A1 guide doesn't allow where it appears, a hierarchy problem in the HL segments, a list of related parties that doesn't end, or an unexpected version.

Findings don't stop the parser. For each code, decide whether your application accepts the response, logs it, or rejects it.