From f7119a5b0fed910aeeb2011efcc4f10c69543b47 Mon Sep 17 00:00:00 2001 From: Timothy J Warren Date: Fri, 24 Mar 2017 09:08:39 -0400 Subject: [PATCH] Show custom 404 pages for missing anime and characters --- app/views/404.php | 2 +- src/API/Kitsu/Model.php | 13 ++++++++++++- src/Controller/Anime.php | 12 +++++++++++- src/Controller/Character.php | 9 +++++++-- src/ControllerTrait.php | 8 ++++++-- 5 files changed, 37 insertions(+), 7 deletions(-) diff --git a/app/views/404.php b/app/views/404.php index bba9aec9..a99e7313 100644 --- a/app/views/404.php +++ b/app/views/404.php @@ -1,4 +1,4 @@

404

-

Page Not Found

+

diff --git a/src/API/Kitsu/Model.php b/src/API/Kitsu/Model.php index cf60ffc0..26d7d203 100644 --- a/src/API/Kitsu/Model.php +++ b/src/API/Kitsu/Model.php @@ -196,8 +196,13 @@ class Model { */ public function getAnime(string $slug): array { - // @TODO catch non-existent anime $baseData = $this->getRawMediaData('anime', $slug); + + if (empty($baseData)) + { + return []; + } + $transformed = $this->animeTransformer->transform($baseData); $transformed['included'] = $baseData['included']; return $transformed; @@ -657,6 +662,12 @@ class Model { ]; $data = $this->getRequest($type, $options); + + if (empty($data['data'])) + { + return []; + } + $baseData = $data['data'][0]['attributes']; $baseData['included'] = $data['included']; return $baseData; diff --git a/src/Controller/Anime.php b/src/Controller/Anime.php index 90df958e..85d68eea 100644 --- a/src/Controller/Anime.php +++ b/src/Controller/Anime.php @@ -262,7 +262,17 @@ class Anime extends BaseController { { $data = $this->model->getAnime($animeId); $characters = []; - + + if (empty($data)) + { + return $this->notFound( + $this->config->get('whose_list') . + "'s Anime List · Anime · " . + 'Anime not found', + 'Anime Not Found' + ); + } + foreach($data['included'] as $included) { if ($included['type'] === 'characters') diff --git a/src/Controller/Character.php b/src/Controller/Character.php index 09c74796..e33c5ac3 100644 --- a/src/Controller/Character.php +++ b/src/Controller/Character.php @@ -26,9 +26,14 @@ class Character extends BaseController { $data = $model->getCharacter($slug); - if ( ! array_key_exists('data', $data)) + if (( ! array_key_exists('data', $data)) || empty($data['data'])) { - return $this->notFound(); + return $this->notFound( + $this->config->get('whose_list') . + "'s Anime List · Characters · " . + 'Character not found', + 'Character Not Found' + ); } // $this->outputJSON($data); diff --git a/src/ControllerTrait.php b/src/ControllerTrait.php index 59bf5747..03197051 100644 --- a/src/ControllerTrait.php +++ b/src/ControllerTrait.php @@ -234,10 +234,14 @@ trait ControllerTrait { * * @return void */ - public function notFound() + public function notFound( + string $title = 'Sorry, page not found', + string $message = 'Page Not Found' + ) { $this->outputHTML('404', [ - 'title' => 'Sorry, page not found' + 'title' => $title, + 'message' => $message, ], NULL, 404); }