404 properly when post is not found

This commit is contained in:
Alex Grintsvayg 2016-08-18 14:01:45 -04:00
parent d05fd00f73
commit 300b5192e4
2 changed files with 8 additions and 3 deletions

View file

@ -85,8 +85,11 @@ class ContentActions extends Actions
public static function executeNewsPost($relativeUri) public static function executeNewsPost($relativeUri)
{ {
$post = Post::load(ltrim($relativeUri, '/')); try
if (!$post) {
$post = Post::load(ltrim($relativeUri, '/'));
}
catch (PostNotFoundException $e)
{ {
return ['page/404', []]; return ['page/404', []];
} }

View file

@ -1,5 +1,7 @@
<?php <?php
class PostNotFoundException extends Exception {}
class Post class Post
{ {
const SORT_DATE_DESC = 'sort_date_desc'; const SORT_DATE_DESC = 'sort_date_desc';
@ -33,7 +35,7 @@ class Post
return static::load($slugMap[$slug]); return static::load($slugMap[$slug]);
} }
} }
throw new InvalidArgumentException('No post found for path: ' . $relativeOrAbsolutePath); throw new PostNotFoundException('No post found for path: ' . $relativeOrAbsolutePath);
} }
list($ignored, $frontMatter, $content) = explode('---', file_get_contents($path), 3); list($ignored, $frontMatter, $content) = explode('---', file_get_contents($path), 3);