-
-
Notifications
You must be signed in to change notification settings - Fork 67
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Location of thrown exceptions make them impossible to catch properly. #221
Comments
I disagree here: any kind of iterator that loads data can potentially fail, and the mail component is the same. Moving the crash from the loading of an item to the loading of a specific detail of a message later on only moves the "mine". This could have been If I/O requires asynchronous/lazy loading, you shift the exception, making the user more involved in it. I agree that it isn't nice to get a crash mid-iteration, but I also think that shifting the exception further inside the loop causes even worse problems. |
All /** @var \Laminas\Mail\Storage\AbstractStorage $mail */
$mail->rewind();
while ($mail->valid()) {
try {
$message = $mail->current();
} catch (\Throwable) {
// Do stuff
}
$mail->next();
} |
@Slamdunk I like that solution, its much more elegant that what I came up with. I feel that this should be mentioned in the docs at the least though, as https://github.com/laminas/laminas-mail/blob/2.22.x/docs/book/read.md (only documentation I could find here that show how to check a mailbox and read the mail) only uses the On a related note, I'm not sure "invalid" headers are worthy of an exception in the first place. By taking that approach, I believe you are preventing the caller from reading an email body just because your validator didn't like the format or the way certain characters are encoded in the header (whether or not the headers are valid and this is a bug not withstanding). That is to say, given the code above (I haven not actually tried it yet), it seems that handling the issue that I ran into in this manner would still not allow me to actually parse the body of the message that had "invalid headers". If I'm wrong here please feel free to correct me. It just seems to me that's how this scenario would play out given the sample code above - I would be able to continue past the "broken" message in this case but I still do not think I could have read the body, right? Maybe a feature could be added where header validation could be disabled ahead of time and/or instead set an Food for thought :) |
@Ocramius I think you make a valid point but your example is misrepresenting the issue at had in a way. In your example I fully agree, if the image is invalid it should not be be loaded, at face value, but in this case the the "header" of the email was invalid but the other parts of it were not and there is a use-case for wanting to still have access to the non-invalid parts of the message. The entire message is not invalid in this case, only a single, non-consequential header (a spam header in this case) was what it didn't like. I don't think its warranted to treat the entire email message as invalid just because of an issue with a single header. |
laminas-mail/src/Header/GenericHeader.php
Lines 49 to 67 in 0516586
Hello. The above reference method throws exceptions while "loading" the mailbox information meaning you have to wrap your message loop in a try/catch block to catch them but doing that offers no way to "continue" processing the loop after encountering an email with "invalid headers".
Often times it does not matter to what I or others are doing if the email has invalid headers and would prefer not to put the "kabash" on whole routine just because of something like this.
I recommend, instead of checking the headers as it loads the mailbox data as it apparently does right now, instead have them checked when the message is accessed for the first time, e.g.:
(The above line should not trigger an exception.)
Instead:
The text was updated successfully, but these errors were encountered: