mirror of
https://github.com/LBRYFoundation/lbry.com.git
synced 2025-08-23 17:47:26 +00:00
more graceful error handling
This commit is contained in:
parent
eb752f2f45
commit
8965500ad6
2 changed files with 54 additions and 32 deletions
|
@ -76,7 +76,7 @@ class ContentActions extends Actions
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$post = Post::load(static::SLUG_NEWS . '/' . ltrim($slug, '/'));
|
$post = Post::load(static::SLUG_NEWS . '/' . ltrim($slug, '/'));
|
||||||
} catch (PostNotFoundException $e) {
|
} catch (PostException $e) {
|
||||||
return NavActions::execute404();
|
return NavActions::execute404();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -130,7 +130,7 @@ class ContentActions extends Actions
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$post = Post::load(static::SLUG_FAQ . '/' . ltrim($slug, '/'));
|
$post = Post::load(static::SLUG_FAQ . '/' . ltrim($slug, '/'));
|
||||||
} catch (PostNotFoundException $e) {
|
} catch (PostException $e) {
|
||||||
return Controller::redirect('/' . static::SLUG_FAQ);
|
return Controller::redirect('/' . static::SLUG_FAQ);
|
||||||
}
|
}
|
||||||
return ['content/faq-post', ['post' => $post]];
|
return ['content/faq-post', ['post' => $post]];
|
||||||
|
@ -154,7 +154,7 @@ class ContentActions extends Actions
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$post = Post::load(static::SLUG_CREDIT_REPORTS . '/' . $year . '-Q' . $quarter);
|
$post = Post::load(static::SLUG_CREDIT_REPORTS . '/' . $year . '-Q' . $quarter);
|
||||||
} catch (PostNotFoundException $e) {
|
} catch (PostException $e) {
|
||||||
return Controller::redirect('/' . static::SLUG_CREDIT_REPORTS);
|
return Controller::redirect('/' . static::SLUG_CREDIT_REPORTS);
|
||||||
}
|
}
|
||||||
$metadata = $post->getMetadata();
|
$metadata = $post->getMetadata();
|
||||||
|
|
|
@ -1,10 +1,12 @@
|
||||||
<?php
|
<?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) + ['','',''];
|
list($ignored, $frontMatter, $content) = explode('---', file_get_contents($path), 3) + ['','',''];
|
||||||
if (!$frontMatter || !$content) {
|
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));
|
return new static($path, $postType, $slug, Spyc::YAMLLoadString(trim($frontMatter)), trim($content));
|
||||||
}
|
}
|
||||||
|
@ -77,33 +81,37 @@ class Post
|
||||||
|
|
||||||
public static function find($folder, $sort = null)
|
public static function find($folder, $sort = null)
|
||||||
{
|
{
|
||||||
$posts = [];
|
$posts = array_filter(array_map(function($file) {
|
||||||
foreach (glob(rtrim($folder, '/') . '/*.md') as $file) {
|
try {
|
||||||
$posts[] = static::load($file);
|
return static::load($file);
|
||||||
}
|
} catch (PostException $e) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}, glob(rtrim($folder, '/') . '/*.md')));
|
||||||
|
|
||||||
|
|
||||||
if ($sort) {
|
if ($sort) {
|
||||||
switch ($sort) {
|
switch ($sort) {
|
||||||
case static::SORT_DATE_DESC:
|
case static::SORT_DATE_DESC:
|
||||||
usort($posts, function (Post $a, Post $b) {
|
usort($posts, function (Post $a, Post $b) {
|
||||||
return strcasecmp($b->getDate()->format('Y-m-d'), $a->getDate()->format('Y-m-d'));
|
return strcasecmp($b->getDate()->format('Y-m-d'), $a->getDate()->format('Y-m-d'));
|
||||||
});
|
});
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case static::SORT_ORD_ASC:
|
case static::SORT_ORD_ASC:
|
||||||
usort($posts, function (Post $a, Post $b) {
|
usort($posts, function (Post $a, Post $b) {
|
||||||
$aMeta = $a->getMetadata();
|
$aMeta = $a->getMetadata();
|
||||||
$bMeta = $b->getMetadata();
|
$bMeta = $b->getMetadata();
|
||||||
if (!isset($aMeta['order']) && !isset($bMeta['order'])) {
|
if (!isset($aMeta['order']) && !isset($bMeta['order'])) {
|
||||||
return $a->getTitle() < $b->getTitle() ? -1 : 1;
|
return $a->getTitle() < $b->getTitle() ? -1 : 1;
|
||||||
}
|
}
|
||||||
if (isset($aMeta['order']) && isset($bMeta['order'])) {
|
if (isset($aMeta['order']) && isset($bMeta['order'])) {
|
||||||
return $aMeta['order'] < $bMeta['order'] ? -1 : 1;
|
return $aMeta['order'] < $bMeta['order'] ? -1 : 1;
|
||||||
}
|
}
|
||||||
return isset($aMeta['order']) ? -1 : 1;
|
return isset($aMeta['order']) ? -1 : 1;
|
||||||
});
|
});
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return $posts;
|
return $posts;
|
||||||
}
|
}
|
||||||
|
@ -185,7 +193,7 @@ class Post
|
||||||
|
|
||||||
public function getDate()
|
public function getDate()
|
||||||
{
|
{
|
||||||
return $this->date;
|
return $this->date ?? new DateTime();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getCover()
|
public function getCover()
|
||||||
|
@ -230,14 +238,28 @@ class Post
|
||||||
{
|
{
|
||||||
$slugs = array_keys(Post::getSlugMap($this->postType));
|
$slugs = array_keys(Post::getSlugMap($this->postType));
|
||||||
$postNum = $this->getPostNum();
|
$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()
|
public function getNextPost()
|
||||||
{
|
{
|
||||||
$slugs = array_keys(Post::getSlugMap($this->postType));
|
$slugs = array_keys(Post::getSlugMap($this->postType));
|
||||||
$postNum = $this->getPostNum();
|
$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()
|
public function hasAuthor()
|
||||||
|
|
Loading…
Add table
Reference in a new issue