Skip to content

Commit

Permalink
Improve error handling of the EmailAction
Browse files Browse the repository at this point in the history
The action now handles all sorts of errors the same. Specifically
it can now display error messages from the email service if the
app is in debug mode.
  • Loading branch information
mzur committed Jun 29, 2017
1 parent e2796f6 commit 7fbc774
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 19 deletions.
2 changes: 1 addition & 1 deletion languages/de.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
'uniform-calc-incorrect' => 'Bitte lösen Sie die Rechenaufgabe.',

'uniform-email-subject' => 'Nachricht über das Formular',
'uniform-email-error' => 'Es ist ein Fehler beim Senden aufgetreten:',
'uniform-email-error' => 'Es ist ein Fehler beim Senden aufgetreten',
'uniform-email-copy' => 'Kopie:',

'uniform-calc-plus' => 'plus',
Expand Down
2 changes: 1 addition & 1 deletion languages/en.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
'uniform-calc-incorrect' => 'Please solve the arithmetic problem.',

'uniform-email-subject' => 'Message from the web form',
'uniform-email-error' => 'There was an error sending the form:',
'uniform-email-error' => 'There was an error sending the form',
'uniform-email-copy' => 'Copy:',

'uniform-calc-plus' => 'plus',
Expand Down
2 changes: 1 addition & 1 deletion languages/es.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
'uniform-calc-incorrect' => 'Por favor, resuelva el problema aritmético.',

'uniform-email-subject' => 'Mensaje del formulario web',
'uniform-email-error' => 'Se ha producido un error al enviar el formulario:',
'uniform-email-error' => 'Se ha producido un error al enviar el formulario',
'uniform-email-copy' => 'Copia:',

'uniform-calc-plus' => 'plus',
Expand Down
2 changes: 1 addition & 1 deletion languages/fr.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
'uniform-calc-incorrect' => 'Veuillez résoudre le problème arithmétique.',

'uniform-email-subject' => 'Message de la part du site internet',
'uniform-email-error' => 'Une erreur s’est produite lors de l’envoi du formulaire :',
'uniform-email-error' => 'Une erreur s’est produite lors de l’envoi du formulaire',
'uniform-email-copy' => 'Copie :',

'uniform-calc-plus' => 'plus',
Expand Down
2 changes: 1 addition & 1 deletion languages/nl.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
'uniform-calc-incorrect' => 'Please solve the arithmetic problem.',

'uniform-email-subject' => 'Bericht van het webformulier',
'uniform-email-error' => 'Er is een fout bij het verzenden van het formulier opgetreden:',
'uniform-email-error' => 'Er is een fout bij het verzenden van het formulier opgetreden',
'uniform-email-copy' => 'Kopie:',

'uniform-calc-plus' => 'plus',
Expand Down
2 changes: 1 addition & 1 deletion languages/tr.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
'uniform-calc-incorrect' => 'Please solve the arithmetic problem.',

'uniform-email-subject' => 'Web-form\'dan gelen mesaj',
'uniform-email-error' => 'Form gonderilirken bir hata olustu:',
'uniform-email-error' => 'Form gonderilirken bir hata olustu',
'uniform-email-copy' => 'Kopyala:',

'uniform-calc-plus' => 'arti',
Expand Down
25 changes: 13 additions & 12 deletions src/Actions/EmailAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

namespace Uniform\Actions;

use C;
use L;
use Str;
use Error;
use Email;
use Exception;
use Uniform\Form;

/**
Expand Down Expand Up @@ -45,36 +46,36 @@ public function perform()
];

try {
if (!$this->sendEmail($params)) {
$this->fail('The email could not be sent.');
}
$this->sendEmail($params);

if ($this->shouldReceiveCopy()) {
$params['subject'] = L::get('uniform-email-copy').' '.$params['subject'];
$to = $params['to'];
$params['to'] = $params['replyTo'];
$params['replyTo'] = $to;

if (!$this->sendEmail($params)) {
$this->fail('The email copy could not be sent but the form has been submitted.');
}
$this->sendEmail($params);
}
} catch (Exception $e) {
if (c::get('debug') === true) {
$this->fail(L::get('uniform-email-error').': '.$e->getMessage());
}
} catch (Error $e) {
$this->fail(L::get('uniform-email-error').' '.$e->getMessage());

$this->fail(L::get('uniform-email-error').'.');
}
}

/**
* Send an email
*
* @param array $params
* @return boolean
*/
protected function sendEmail(array $params)
{
$email = new Email($params);

return $email->send();
if (!$email->send()) {
throw $email->error;
}
}

/**
Expand Down
40 changes: 39 additions & 1 deletion tests/Actions/EmailActionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Uniform\Tests\Actions;

use Exception;
use C as Config;
use Uniform\Form;
use Uniform\Tests\TestCase;
use Uniform\Actions\EmailAction;
Expand Down Expand Up @@ -167,6 +168,37 @@ public function testReceiveCopy()
$this->assertEquals('[email protected]', $action->params['replyTo']);
$this->assertEquals('[email protected]', $action->params['from']);
}

public function testHandleServiceException()
{
$this->form->data('field', 'value');
$action = new EmailAction($this->form, [
'service' => 'thrower',
'to' => '[email protected]',
'from' => '[email protected]',
'subject' => 'Test',
]);

Config::set('debug', false);

try {
$action->perform();
$this->assertFalse(true);
} catch (PerformerException $e) {
// Language variables are not loaded in these tests so the message is empty
// here.
$this->assertEquals('.', $e->getMessage());
}

Config::set('debug', true);

try {
$action->perform();
$this->assertFalse(true);
} catch (PerformerException $e) {
$this->assertEquals(": Throw it like it's hoot", $e->getMessage());
}
}
}

class EmailActionStub extends EmailAction
Expand All @@ -177,7 +209,9 @@ protected function sendEmail(array $params)
{
$this->calls++;
$this->params = $params;
return !isset($this->shouldFail);
if (isset($this->shouldFail)) {
throw new Exception('Failed');
}
}

protected function getSnippet($name, array $data)
Expand All @@ -191,3 +225,7 @@ protected function getSnippet($name, array $data)
return $name;
}
}

\Email::$services['thrower'] = function($email) {
throw new Exception("Throw it like it's hoot");
};

0 comments on commit 7fbc774

Please sign in to comment.