From 0f9dd61b6b4f926c306f58bb5c6b51f12a2d8c43 Mon Sep 17 00:00:00 2001 From: "Timothy J. Warren" Date: Mon, 22 Feb 2021 15:39:03 -0500 Subject: [PATCH] Better handle update API errors --- src/AnimeClient/Command/BaseCommand.php | 7 +++++-- src/AnimeClient/Command/SyncLists.php | 13 +++++++++++-- src/AnimeClient/Controller/Anime.php | 2 +- src/AnimeClient/Controller/Manga.php | 2 +- 4 files changed, 18 insertions(+), 6 deletions(-) diff --git a/src/AnimeClient/Command/BaseCommand.php b/src/AnimeClient/Command/BaseCommand.php index 3451972f..10ec679e 100644 --- a/src/AnimeClient/Command/BaseCommand.php +++ b/src/AnimeClient/Command/BaseCommand.php @@ -144,13 +144,16 @@ abstract class BaseCommand extends Command { { if ($fgColor !== NULL) { - $fgColor = (string)$fgColor; + $fgColor = (int)$fgColor; } if ($bgColor !== NULL) { - $bgColor = (string)$bgColor; + $bgColor = (int)$bgColor; } + // Colorize the CLI output + // the documented type for the function is wrong + // @phpstan-ignore-next-line $message = Colors::colorize($message, $fgColor, $bgColor); $this->getConsole()->writeln($message); } diff --git a/src/AnimeClient/Command/SyncLists.php b/src/AnimeClient/Command/SyncLists.php index cb062b02..01964a2b 100644 --- a/src/AnimeClient/Command/SyncLists.php +++ b/src/AnimeClient/Command/SyncLists.php @@ -16,6 +16,7 @@ namespace Aviat\AnimeClient\Command; +use Aviat\Ion\JsonException; use ConsoleKit\Widgets; use Aviat\AnimeClient\API\{ @@ -288,7 +289,15 @@ final class SyncLists extends BaseCommand { // This uses a static so I don't have to fetch this list twice for a count if ($list[$type] === NULL) { - $list[$type] = $this->anilistModel->getSyncList(strtoupper($type)); + try + { + $list[$type] = $this->anilistModel->getSyncList(strtoupper($type)); + } + catch (JsonException) + { + $this->echoErrorBox('Anlist API exception. Can not sync.'); + die(); + } } return $list[$type]; @@ -599,7 +608,7 @@ final class SyncLists extends BaseCommand { $kitsuItem['data']['ratingTwenty'] !== 0 ) { - $update['data']['ratingTwenty'] = $kitsuItem['data']['ratingTwenty']; + $update['data']['ratingTwenty'] = $kitsuItem['data']['rating']; $return['updateType'][] = Enum\API::ANILIST; } else if($dateDiff === self::ANILIST_GREATER && $anilistItem['data']['rating'] !== 0) diff --git a/src/AnimeClient/Controller/Anime.php b/src/AnimeClient/Controller/Anime.php index 90aa63c5..d56740a0 100644 --- a/src/AnimeClient/Controller/Anime.php +++ b/src/AnimeClient/Controller/Anime.php @@ -228,7 +228,7 @@ final class Anime extends BaseController { $postData = $transformer->untransform($data); $fullResult = $this->model->updateLibraryItem(FormItem::from($postData)); - if ($fullResult['statusCode'] === 200) + if ($fullResult['statusCode'] === 200 && $fullResult['body']['error'] === NULL) { $this->setFlashMessage('Successfully updated.', 'success'); $this->cache->clear(); diff --git a/src/AnimeClient/Controller/Manga.php b/src/AnimeClient/Controller/Manga.php index d0aa83ce..05e7719d 100644 --- a/src/AnimeClient/Controller/Manga.php +++ b/src/AnimeClient/Controller/Manga.php @@ -229,7 +229,7 @@ final class Manga extends Controller { $post_data = $transformer->untransform($data); $full_result = $this->model->updateLibraryItem(FormItem::from($post_data)); - if ($full_result['statusCode'] === 200) + if ($full_result['statusCode'] === 200 && $full_result['body']['error'] === NULL) { $this->setFlashMessage('Successfully updated manga.', 'success'); $this->cache->clear();