mail-mime-parser icon indicating copy to clipboard operation
mail-mime-parser copied to clipboard

ParsingErrorBag

Open ThomasLandauer opened this issue 5 years ago • 2 comments

Follow-up of https://github.com/zbateson/mail-mime-parser/issues/119#issuecomment-668740788 about this idea:

Whenever the parser does some error correction, drop something like $this->addParsingError('Invalid timezone') into it; then the user could retrieve all those errors with something like $message->getParserErrors().

Let's collect the occurrences of such parsing errors first, to see if it's worth it. I found:

  • https://github.com/zbateson/mail-mime-parser/blob/master/src/Header/Part/DatePart.php#L43
  • https://github.com/zbateson/mail-mime-parser/blob/master/src/Header/Part/DatePart.php#L47

ThomasLandauer avatar Aug 04 '20 20:08 ThomasLandauer

Ooh, I quite like this. I'd like to spend some time and see if there are good implementations of what you're proposing and maybe other good ideas/caveats, etc...

The error handling could be an interface applied to all objects, so you could call $dateHeader->hasErrors() directly potentially or $message->hasErrors(), etc... and you could maybe $message->getParserErrors() or $message->getAllParserErrors (including its children?) or something.

If you (or anyone reading) has a good example(s) of this being done elsewhere or more ideas/warnings/comments, very much welcome.

zbateson avatar Aug 04 '20 20:08 zbateson

The only idea I'm having is:

  • Add public array $parsingErrors to DatePart (and all other parts)
  • In AbstractHeader, where all header parts are collected (probably in setParseHeaderValue()), add each part's parsingErrors to $this->parsingErrors
  • In ParentHeaderPart, add function getParsingErrors():
    foreach($this->headerContainer->getHeaderObjects() as $header) {
        $parsingErrors[] = $header->getParsingErrors();
    }
    

ThomasLandauer avatar Aug 14 '20 19:08 ThomasLandauer

Added in 3.0

zbateson avatar Apr 23 '24 20:04 zbateson