Fixed Swiftmailer not catching Exceptions

This commit is contained in:
R. Eric Wheeler 2016-07-15 11:05:27 -07:00
parent 2b33856e17
commit 80f83362f2
5 changed files with 32 additions and 6 deletions

View File

@ -130,14 +130,18 @@ class App extends Application
$matches = 'Available methods are unknown.'; $matches = 'Available methods are unknown.';
} }
$message = json_encode(['status' => 'error', 'message' => 'Method not allowed', 'allowedMethods' => $matches, 'requestedMethod' => $request->getMethod(), 'code' => $code]); $message = json_encode(['status' => 'error', 'message' => 'Method not allowed', 'allowedMethods' => $matches, 'requestedMethod' => $request->getMethod(), 'code' => $code]);
$this->log($e->getMessage(), ['code' => $code], \Symfony\Bridge\Monolog\Logger::WARNING);
break; break;
case 500: case 500:
$message = json_encode(['status' => 'error', 'message' => $e->getMessage(), 'code' => $code]); $message = json_encode(['status' => 'error', 'message' => 'Critical Error', 'code' => $code]);
$this->log($e->getMessage(), ['code' => $code], \Symfony\Bridge\Monolog\Logger::CRITICAL);
break; break;
default: default:
$message = json_encode(['status' => 'error', 'message' => $e->getMessage(), 'code' => $code]); $message = json_encode(['status' => 'error', 'message' => $e->getMessage(), 'code' => $code]);
$this->log($e->getMessage(), ['code' => $code], \Symfony\Bridge\Monolog\Logger::ERROR);
break; break;
} }
return new \Symfony\Component\HttpFoundation\Response($message, $code); return new \Symfony\Component\HttpFoundation\Response($message, $code);
}); });
} }

View File

@ -71,7 +71,7 @@ $app
'console.version' => '0.0.1', 'console.version' => '0.0.1',
'console.project_directory' => $app->getAppDirectory(), 'console.project_directory' => $app->getAppDirectory(),
]); ]);
$app['swiftmailer.use_spool'] = false;
if(false === getenv('SPARKPOST_API_KEY') && null !== $app->config('app.smtp_host')) { if(false === getenv('SPARKPOST_API_KEY') && null !== $app->config('app.smtp_host')) {
$app['swiftmailer.options'] = [ $app['swiftmailer.options'] = [
'host' => $app->config('app.smtp_host'), 'host' => $app->config('app.smtp_host'),

View File

@ -137,7 +137,7 @@ class ApiControllerProvider implements ControllerProviderInterface {
$contactFormName = $contactFormData['contact']['name']; $contactFormName = $contactFormData['contact']['name'];
$contactFormEmail = $contactFormData['contact']['email']; $contactFormEmail = $contactFormData['contact']['email'];
$contactFormMessage = $contactFormData['contact']['message']; $contactFormMessage = $contactFormData['contact']['message'];
$request->getSession()->remove('_csrf/contact');
$failures = ''; $failures = '';
$sent = $app['mailer']->send(\Swift_Message::newInstance() $sent = $app['mailer']->send(\Swift_Message::newInstance()
@ -147,6 +147,7 @@ class ApiControllerProvider implements ControllerProviderInterface {
->setBody($contactFormMessage) ->setBody($contactFormMessage)
, $failures); , $failures);
if($sent > 0) { if($sent > 0) {
$request->getSession()->remove('_csrf/contact');
return new JsonResponse([ return new JsonResponse([
'status' => 'success', 'status' => 'success',
'message' => 'Message successfully sent.', 'message' => 'Message successfully sent.',

View File

@ -16,6 +16,7 @@ use Sikofitt\Form\Type\ContactType;
use Silex\Api\ControllerProviderInterface; use Silex\Api\ControllerProviderInterface;
use Silex\Application; use Silex\Application;
use Symfony\Component\Form\FormFactory; use Symfony\Component\Form\FormFactory;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
/** /**
@ -63,6 +64,22 @@ class ResumeControllerProvider implements ControllerProviderInterface
]); ]);
}); });
$controllers->post('/', function(Request $request) use ($app) {
$contactFormData = $request->request->all();
$contactFormName = $contactFormData['contact']['name'];
$contactFormEmail = $contactFormData['contact']['email'];
$contactFormMessage = $contactFormData['contact']['message'];
$sent = $app['mailer']->send(\Swift_Message::newInstance()
->setSubject('[Resume] Message')
->setFrom([$contactFormEmail => $contactFormName])
->setTo($app->config('app.email'))
->setBody($contactFormMessage)
, $failures);
dump($failures);
dump($sent);
return new JsonResponse(['failures' => $failures, 'sent' => (bool)$sent]);
});
return $controllers; return $controllers;
} }

View File

@ -38,6 +38,7 @@ jq(document).ready(function (jq) {
contactModal.show(); contactModal.show();
jq.post(jq(this).attr('action'), jq(this).serialize(), function(response) { jq.post(jq(this).attr('action'), jq(this).serialize(), function(response) {
if(response.status !== 'success') { if(response.status !== 'success') {
resp = response;
jq('#' + response.id).addClass('uk-form-danger'); jq('#' + response.id).addClass('uk-form-danger');
UIkit.notify('<i class="uk-icon uk-icon-frown-o uk-icon-justify uk-margin-right"></i>' + response.message + ' ('+response.code+')</div>', { UIkit.notify('<i class="uk-icon uk-icon-frown-o uk-icon-justify uk-margin-right"></i>' + response.message + ' ('+response.code+')</div>', {
pos: 'top-center', pos: 'top-center',
@ -58,10 +59,13 @@ jq(document).ready(function (jq) {
} }
}).fail(function(response, code, resp) { }).fail(function(response, code, resp) {
console.log(response);
console.log(code);
console.log(resp);
contactModal.hide(); contactModal.hide();
resp = response;
console.log(response);
UIkit.notify('<i class="uk-icon uk-icon-frown-o uk-icon-justify uk-margin-right"></i>'+ response.message + ' ('+response.status+')</div>', {
pos: 'top-center',
status: 'danger'
});
}).done(function () { }).done(function () {
contactModal.hide(); contactModal.hide();
}); });