more graceful error handling

This commit is contained in:
Jeremy Kauffman 2019-03-25 17:25:04 -04:00
parent eb752f2f45
commit 8965500ad6
2 changed files with 54 additions and 32 deletions

View file

@ -76,7 +76,7 @@ class ContentActions extends Actions
try {
$post = Post::load(static::SLUG_NEWS . '/' . ltrim($slug, '/'));
} catch (PostNotFoundException $e) {
} catch (PostException $e) {
return NavActions::execute404();
}
@ -130,7 +130,7 @@ class ContentActions extends Actions
try {
$post = Post::load(static::SLUG_FAQ . '/' . ltrim($slug, '/'));
} catch (PostNotFoundException $e) {
} catch (PostException $e) {
return Controller::redirect('/' . static::SLUG_FAQ);
}
return ['content/faq-post', ['post' => $post]];
@ -154,7 +154,7 @@ class ContentActions extends Actions
try {
$post = Post::load(static::SLUG_CREDIT_REPORTS . '/' . $year . '-Q' . $quarter);
} catch (PostNotFoundException $e) {
} catch (PostException $e) {
return Controller::redirect('/' . static::SLUG_CREDIT_REPORTS);
}
$metadata = $post->getMetadata();

View file

@ -1,10 +1,12 @@
<?php
class PostNotFoundException extends Exception
class PostException extends Exception {}
class PostNotFoundException extends PostException
{
}
class PostMalformedException extends Exception
class PostMalformedException extends PostException
{
}
@ -55,7 +57,9 @@ class Post
list($ignored, $frontMatter, $content) = explode('---', file_get_contents($path), 3) + ['','',''];
if (!$frontMatter || !$content) {
throw new PostMalformedException('Post "' . basename($path) . '" is missing front matter or content');
$e = new PostMalformedException('Post "' . basename($path) . '" is missing front matter or content');
Slack::sendErrorIfProd($e);
throw $e;
}
return new static($path, $postType, $slug, Spyc::YAMLLoadString(trim($frontMatter)), trim($content));
}
@ -77,10 +81,14 @@ class Post
public static function find($folder, $sort = null)
{
$posts = [];
foreach (glob(rtrim($folder, '/') . '/*.md') as $file) {
$posts[] = static::load($file);
$posts = array_filter(array_map(function($file) {
try {
return static::load($file);
} catch (PostException $e) {
return false;
}
}, glob(rtrim($folder, '/') . '/*.md')));
if ($sort) {
switch ($sort) {
@ -185,7 +193,7 @@ class Post
public function getDate()
{
return $this->date;
return $this->date ?? new DateTime();
}
public function getCover()
@ -230,14 +238,28 @@ class Post
{
$slugs = array_keys(Post::getSlugMap($this->postType));
$postNum = $this->getPostNum();
return $postNum === false || $postNum === 0 ? null : Post::load($this->postType . '/' . $slugs[$postNum-1]);
if ($postNum === false || $postNum === 0) {
return null;
}
try {
return Post::load($this->postType . '/' . $slugs[$postNum-1]);
} catch (PostException $e) {
return null;
}
}
public function getNextPost()
{
$slugs = array_keys(Post::getSlugMap($this->postType));
$postNum = $this->getPostNum();
return $postNum === false || $postNum >= count($slugs)-1 ? null : Post::load($this->postType . '/' . $slugs[$postNum+1]);
if ($postNum === false || $postNum >= count($slugs)-1) {
return null;
}
try {
return Post::load($this->postType . '/' . $slugs[$postNum+1]);
} catch (PostException $e) {
return null;
}
}
public function hasAuthor()