rjbs/Email-Stuffer

Returning undef from methods designed for chaining causes headaches

Closed this issue · 4 comments

The docs promote the use of chaining:

  Email::Stuffer->to('Simon Cozens<simon@somewhere.jp>')
                ->from('Santa@northpole.org')
                ->text_body("You've been good this year. No coal for you.")
                ->attach_file('choochoo.gif')
                ->send;

but there's no mention of the fact that some methods, like subject and to will return undef in some cases. In those cases the error behaviour is Less Than Awesome.

This has come up before in #8 in relation to subject(). We just hit this again in related to to().

Given that the docs say:

this is very much a module for those "slap it together and fire it off" situations, but that still has enough grunt behind the scenes to do things properly.

and

this module is still subject to name and/or API changes

I'd like to propose that exceptions be thrown in these cases.

I'd happily provide a pull request if you're open to that?

rjbs commented

Heh. I've always found this whole interface to be somewhat bizarre. I made Email-Stuffer just to get people using Email-Stuff, which used Email-Send, onto Email-Sender. :)

There are two possibilities for change: (1) throw an exception (2) return $self anyway.

The thing with 2 is: why, after all, is it returning undef? It looks like it'd do that if:

  • you set subject to the empty string
  • you set the subject to zero
  • you clear the subject header

Only in the last case does it make sense for us to consider it an error condition, I think. And, in that condition, it only makes sense for required fields. You should definitely be able to clear the bcc header.

You could always use Method::Cascade in the interim. :)

Presumably a required field that isn't set will cause send() to fail.
Returning $self regardless seems reasonable.

rjbs commented

I've done this in 6881b7a