From 80f83362f2db89852dc5fbd90fa2df0bf2770417 Mon Sep 17 00:00:00 2001 From: sikofitt Date: Fri, 15 Jul 2016 11:05:27 -0700 Subject: [PATCH] Fixed Swiftmailer not catching Exceptions --- app/App.php | 6 +++++- app/providers.php | 2 +- .../Controller/ApiControllerProvider.php | 3 ++- .../Controller/ResumeControllerProvider.php | 17 +++++++++++++++++ src/Sikofitt/js/resume.js | 10 +++++++--- 5 files changed, 32 insertions(+), 6 deletions(-) diff --git a/app/App.php b/app/App.php index 4f376b3..f0efad5 100644 --- a/app/App.php +++ b/app/App.php @@ -130,14 +130,18 @@ class App extends Application $matches = 'Available methods are unknown.'; } $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; 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; default: $message = json_encode(['status' => 'error', 'message' => $e->getMessage(), 'code' => $code]); + $this->log($e->getMessage(), ['code' => $code], \Symfony\Bridge\Monolog\Logger::ERROR); break; } + return new \Symfony\Component\HttpFoundation\Response($message, $code); }); } diff --git a/app/providers.php b/app/providers.php index c7c8a76..9b800a4 100644 --- a/app/providers.php +++ b/app/providers.php @@ -71,7 +71,7 @@ $app 'console.version' => '0.0.1', 'console.project_directory' => $app->getAppDirectory(), ]); - + $app['swiftmailer.use_spool'] = false; if(false === getenv('SPARKPOST_API_KEY') && null !== $app->config('app.smtp_host')) { $app['swiftmailer.options'] = [ 'host' => $app->config('app.smtp_host'), diff --git a/src/Sikofitt/Controller/ApiControllerProvider.php b/src/Sikofitt/Controller/ApiControllerProvider.php index e544c33..11d0a8e 100644 --- a/src/Sikofitt/Controller/ApiControllerProvider.php +++ b/src/Sikofitt/Controller/ApiControllerProvider.php @@ -137,7 +137,7 @@ class ApiControllerProvider implements ControllerProviderInterface { $contactFormName = $contactFormData['contact']['name']; $contactFormEmail = $contactFormData['contact']['email']; $contactFormMessage = $contactFormData['contact']['message']; - $request->getSession()->remove('_csrf/contact'); + $failures = ''; $sent = $app['mailer']->send(\Swift_Message::newInstance() @@ -147,6 +147,7 @@ class ApiControllerProvider implements ControllerProviderInterface { ->setBody($contactFormMessage) , $failures); if($sent > 0) { + $request->getSession()->remove('_csrf/contact'); return new JsonResponse([ 'status' => 'success', 'message' => 'Message successfully sent.', diff --git a/src/Sikofitt/Controller/ResumeControllerProvider.php b/src/Sikofitt/Controller/ResumeControllerProvider.php index be4f731..20ae70c 100644 --- a/src/Sikofitt/Controller/ResumeControllerProvider.php +++ b/src/Sikofitt/Controller/ResumeControllerProvider.php @@ -16,6 +16,7 @@ use Sikofitt\Form\Type\ContactType; use Silex\Api\ControllerProviderInterface; use Silex\Application; use Symfony\Component\Form\FormFactory; +use Symfony\Component\HttpFoundation\JsonResponse; 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; } diff --git a/src/Sikofitt/js/resume.js b/src/Sikofitt/js/resume.js index ed08c2a..cf1bc07 100644 --- a/src/Sikofitt/js/resume.js +++ b/src/Sikofitt/js/resume.js @@ -38,6 +38,7 @@ jq(document).ready(function (jq) { contactModal.show(); jq.post(jq(this).attr('action'), jq(this).serialize(), function(response) { if(response.status !== 'success') { + resp = response; jq('#' + response.id).addClass('uk-form-danger'); UIkit.notify('' + response.message + ' ('+response.code+')', { pos: 'top-center', @@ -58,10 +59,13 @@ jq(document).ready(function (jq) { } }).fail(function(response, code, resp) { - console.log(response); - console.log(code); - console.log(resp); contactModal.hide(); + resp = response; + console.log(response); + UIkit.notify(''+ response.message + ' ('+response.status+')', { + pos: 'top-center', + status: 'danger' + }); }).done(function () { contactModal.hide(); });