error pages

This commit is contained in:
R. Eric Wheeler 2016-07-15 14:41:32 -07:00
parent 8811b863d8
commit 8f741b19b5
9 changed files with 111 additions and 72 deletions

View File

@ -84,7 +84,7 @@ class App extends Application
*/
public function getResumeSchema()
{
return $this->getDataDirectory() . '/resume.schema.json';
return $this->getDataDirectory() . '/schema/schema.v1.json';
}
public function getLogDirectory()
@ -129,7 +129,18 @@ class App extends Application
} else {
$matches = 'Available methods are unknown.';
}
$message = json_encode(['status' => 'error', 'message' => 'Method not allowed', 'allowedMethods' => $matches, 'requestedMethod' => $request->getMethod(), 'code' => $code]);
$message = [
'status' => 'error',
'message' => 'Method not allowed',
'allowedMethods' => $matches,
'requestedMethod' => $request->getMethod(),
'code' => $code,
];
if($request->isXmlHttpRequest()) {
$message = json_encode($message);
} else {
$message = $this->renderView('error.405.html.twig', $message);
}
$this->log($e->getMessage(), ['code' => $code], \Symfony\Bridge\Monolog\Logger::WARNING);
break;
case 500:
@ -137,7 +148,13 @@ class App extends Application
$this->log($e->getMessage(), ['code' => $code], \Symfony\Bridge\Monolog\Logger::CRITICAL);
break;
default:
$message = json_encode(['status' => 'error', 'message' => $e->getMessage(), 'code' => $code]);
$message = ['status' => 'error', 'message' => $e->getMessage(), 'code' => $code, 'requestUri' => $request->getRequestUri()];
if($request->isXmlHttpRequest()) {
$message = json_decode($message);
} else {
$message = $this->renderView('error.html.twig', $message);
}
$this->log($e->getMessage(), ['code' => $code], \Symfony\Bridge\Monolog\Logger::ERROR);
break;
}

View File

@ -14,3 +14,4 @@ app:
smtp_port: 465
smtp_user: eric@rewiv.com
smtp_password: 'P*8ic32!100023&p'
from_email: no-reply@rewiv.com

View File

@ -2,8 +2,9 @@ app:
debug: true
environment: dev
title: R. Eric Wheeler | Resume
email: eric@rewiv.com
phone: 510-646-2135
email: email@example.com
from_email: no-reply@example.com
phone: 510-555-5555
schema: https://raw.githubusercontent.com/jsonresume/resume-schema/v1.0.0/schema.json
captcha: true
captcha_sitekey: 6LcvmSQTAAAAAMmf9w6mhCbpdLvknuD9SGVHT0q-

View File

@ -0,0 +1,13 @@
{% extends 'base.html.twig' %}
{% block title %}HTTP 405{% endblock %}
{% block body %}
<div class="uk-container uk-container-center">
<h1 class="uk-article-title">HTTP 405</h1>
<p class="uk-text-lead">{{ message }} [{{ requestedMethod }}]</p>
<img class="uk-img-preserve uk-thumbnail uk-thumbnail-medium" src="https://http.cat/405" alt="Error 405" />
<p>Return to the home <a href="/" title="Home page">page</a>.</p>
</div>
{% endblock %}

View File

@ -0,0 +1,13 @@
{% extends 'base.html.twig' %}
{% block title %}HTTP {{ code }}{% endblock %}
{% block body %}
<div class="uk-container uk-container-center">
<h1 class="uk-article-title">HTTP {{ code }}</h1>
<p class="uk-text-lead">{{ message }} / {{ requestUri }}</p>
<img class="uk-img-preserve uk-thumbnail uk-thumbnail-medium" src="https://http.cat/{{ code }}" alt="HTTP Error {{ code }}" />
<p>Return to the home <a href="/" title="Home page">page</a>.</p>
</div>
{% endblock %}

View File

@ -26,6 +26,7 @@ namespace Sikofitt\Controller;
use ReCaptcha\ReCaptcha;
use Sikofitt\Form\Type\ContactType;
use Silex\Api\ControllerProviderInterface;
use Silex\Application;
use Symfony\Component\HttpFoundation\JsonResponse;
@ -36,6 +37,7 @@ use Symfony\Component\Validator\Constraints\Email;
use Symfony\Component\Validator\Constraints\EqualTo;
use Symfony\Component\Validator\Constraints\Length;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Constraints\Type;
/**
* Class ApiControllerProvider
@ -85,6 +87,7 @@ class ApiControllerProvider implements ControllerProviderInterface {
'message' => 'Name must not be blank.',
]
),
new Type('string'),
],
'email' => [
new Email([
@ -93,6 +96,7 @@ class ApiControllerProvider implements ControllerProviderInterface {
new NotBlank([
'message' => 'Email must not be blank.',
]),
new Type('string'),
],
'message' => [
new Length([
@ -102,9 +106,11 @@ class ApiControllerProvider implements ControllerProviderInterface {
new NotBlank([
'message' => 'Message must not be blank',
]),
new Type('string'),
],
'_token' => [
new EqualTo(['value' => $csrf, 'message' => 'Invalid token.']),
new Type('string'),
],
]
),
@ -136,34 +142,32 @@ class ApiControllerProvider implements ControllerProviderInterface {
$contactFormName = $contactFormData['contact']['name'];
$contactFormEmail = $contactFormData['contact']['email'];
$contactFormMessage = $contactFormData['contact']['message'];
$message = sprintf("%s\n\nEmail : %s <%s>", $contactFormMessage, $contactFormName, $contactFormEmail);
$contactFormMessage = sprintf("%s\n\nEmail : %s <%s>", $contactFormData['contact']['message'], $contactFormName, $contactFormEmail);
$failures = '';
$sent = $app['mailer']->send(\Swift_Message::newInstance()
$message = \Swift_Message::newInstance()
->setSubject('[Resume] Message')
->setFrom(['no-reply@rewiv.com' => $contactFormName])
->setFrom([$app->config('app.from_email') => $contactFormName])
->setTo([$app->config('app.email') => 'No-Reply'])
->setReplyTo([$contactFormEmail => $contactFormName])
->setBody($message)
, $failures);
if($sent > 0) {
->setBody($contactFormMessage);
$sent = $app['mailer']->send($message, $failures);
if ($sent > 0) {
$request->getSession()->remove('_csrf/contact');
return new JsonResponse([
'status' => 'success',
'message' => 'Message successfully sent.',
'code' => 201,
'data' => $contactFormData,
'failed' => $failures,
'sent' => $sent,
], 200);
} else {
], 201);
}
else {
return new JsonResponse([
'status' => 'error',
'message' => 'There was an error sending the message.',
'code' => 255,
'data' => $contactFormData,
'failed' => $failures,
'sent' => $sent,
], 255);
@ -171,22 +175,11 @@ class ApiControllerProvider implements ControllerProviderInterface {
}
})->method('GET|POST')->bind('api_message');
})->method('POST')->bind('api_message');
$controllers->post('/v1/update', function(Request $request) use ($app) {
$controllers->get('/v1/emailTest', function (Request $request) use ($app) {
try {
$app->mail(\Swift_Message::newInstance()
->setSubject('[Resume] Message')
->setFrom(['eric@rewiv.com' => 'Eric'])
->setTo('eric@ericwheeler.net')
->setBody('Testing message.')
);
} catch (\Exception $e) {
dump($e->getMessage());
}
return new Response('Hello');
});
return new JsonResponse($app->decodeFile($app->getResumeSchema()));
})->bind('api_update');
$controllers->post('/v1/captcha', function (Request $request) use ($app) {
$captcha = new ReCaptcha('6LcvmSQTAAAAAITkvYJjgLar1LqGGLz-ic0ZMiXo');

View File

@ -15,6 +15,7 @@ use ReCaptcha\ReCaptcha;
use Sikofitt\Form\Type\ContactType;
use Silex\Api\ControllerProviderInterface;
use Silex\Application;
use Symfony\Component\Form\Form;
use Symfony\Component\Form\FormFactory;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;

View File

@ -34,6 +34,6 @@ trait JsonFileTrait
public function decodeFile($file, $schema = null)
{
return $this['json.decode']->decodeFile($file, $file, $schema);
return $this['json.decode']->decodeFile($file, $schema);
}
}

View File

@ -33,14 +33,14 @@ jq(document).ready(function (jq) {
}
});
}); // Phone form
jq('form[name=contact]').on('submit', function(event) {
contactModal = UIkit.modal('#spinner-modal', { modal:false, center:true, bgclose:false });
jq('form[name=contact]').on('submit', function (event) {
contactModal = UIkit.modal('#spinner-modal', {modal: false, center: true, bgclose: false});
contactModal.show();
jq.post(jq(this).attr('action'), jq(this).serialize(), function(response) {
if(response.status !== 'success') {
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('<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',
status: 'danger'
});
@ -58,22 +58,22 @@ jq(document).ready(function (jq) {
jq('a[href="#contact-form-wrapper"]').replaceWith('eric@ericwheeler.net');
}
}).fail(function(response, code, resp) {
}).fail(function (response) {
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>', {
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 () {
contactModal.hide();
}).always(function () {
contactModal.hide();
});
});
jq('form[name=contact] input, form[name=contact] textarea').on('focus', function() {
if(jq(this).hasClass('uk-form-danger')) {
jq('form[name=contact] input, form[name=contact] textarea').on('focus', function () {
if (jq(this).hasClass('uk-form-danger')) {
jq(this).removeClass('uk-form-danger');
}
});