Improve error logging
All checks were successful
timw4mail/HummingBirdAnimeClient/pipeline/head This commit looks good

This commit is contained in:
Timothy Warren 2020-08-17 10:23:32 -04:00
parent 65a8da755a
commit e7e70a1541
6 changed files with 55 additions and 81 deletions

View File

@ -20,37 +20,37 @@ use Aviat\AnimeClient\Types\AbstractType;
class MediaListEntry extends AbstractType {
/**
* @var int
* @var int|string
*/
public $id;
/**
* @var string
* @var string|null
*/
public $notes;
public ?string $notes;
/**
* @var bool
*/
public $private;
public ?bool $private;
/**
* @var int
*/
public $progress;
public int $progress;
/**
* @var int
*/
public $repeat;
public ?int $repeat;
/**
* @var string
*/
public $status;
public string $status;
/**
* @var int
*/
public $score;
public ?int $score;
}

View File

@ -98,8 +98,11 @@ final class Model {
if (array_key_exists('error', $data))
{
dump($data['error']);
dump($response);
dump([
'method' => __CLASS__ . '\\' . __METHOD__,
'error' => $data['error'],
'response' => $response,
]);
die();
}
@ -135,8 +138,11 @@ final class Model {
if (array_key_exists('error', $data))
{
dump($data['error']);
dump($response);
dump([
'method' => __CLASS__ . '\\' . __METHOD__,
'error' => $data['error'],
'response' => $response,
]);
die();
}

View File

@ -153,17 +153,13 @@ final class RequestBuilder extends APIRequestBuilder {
* @return array
*/
public function postRequest(...$args): array
{
$logger = NULL;
if ($this->getContainer())
{
$logger = $this->container->getLogger('kitsu-request');
}
$response = $this->getResponse('POST', ...$args);
$validResponseCodes = [200, 201];
if ( ! in_array($response->getStatus(), $validResponseCodes, TRUE) && $logger)
if ( ! in_array($response->getStatus(), $validResponseCodes, TRUE))
{
$logger->warning('Non 2xx response for POST api call', $response->getBody());
}
@ -274,19 +270,11 @@ final class RequestBuilder extends APIRequestBuilder {
* @throws Throwable
*/
public function getResponse(string $type, string $url, array $options = []): Response
{
$logger = NULL;
if ($this->getContainer())
{
$logger = $this->container->getLogger('kitsu-request');
}
$request = $this->setUpRequest($type, $url, $options);
$response = getResponse($request);
if ($logger)
{
$logger->debug('Kitsu API Response', [
'status' => $response->getStatus(),
'reason' => $response->getReason(),
@ -294,7 +282,6 @@ final class RequestBuilder extends APIRequestBuilder {
'headers' => $response->getHeaders(),
'requestHeaders' => $request->getHeaders(),
]);
}
return $response;
}
@ -305,13 +292,8 @@ final class RequestBuilder extends APIRequestBuilder {
* @throws Throwable
*/
private function getResponseFromRequest(Request $request): Response
{
$logger = NULL;
if ($this->getContainer())
{
$logger = $this->container->getLogger('kitsu-request');
}
$response = getResponse($request);
$logger->debug('Kitsu GraphQL response', [
@ -337,9 +319,6 @@ final class RequestBuilder extends APIRequestBuilder {
$response = $this->getResponse('POST', K::GRAPHQL_ENDPOINT, $options);
$validResponseCodes = [200, 201];
$logger = NULL;
if ($this->getContainer())
{
$logger = $this->container->getLogger('kitsu-request');
$logger->debug('Kitsu GraphQL response', [
'status' => $response->getStatus(),
@ -347,15 +326,11 @@ final class RequestBuilder extends APIRequestBuilder {
'body' => $response->getBody(),
'headers' => $response->getHeaders(),
]);
}
if ( ! \in_array($response->getStatus(), $validResponseCodes, TRUE))
{
if ($logger !== NULL)
{
$logger->warning('Non 200 response for GraphQL call', (array)$response->getBody());
}
}
return Json::decode(wait($response->getBody()->buffer()));
}
@ -372,13 +347,8 @@ final class RequestBuilder extends APIRequestBuilder {
* @return array
*/
private function request(string $type, string $url, array $options = []): array
{
$logger = NULL;
if ($this->getContainer())
{
$logger = $this->container->getLogger('kitsu-request');
}
$response = $this->getResponse($type, $url, $options);
$statusCode = $response->getStatus();

View File

@ -105,7 +105,7 @@ final class SyncLists extends BaseCommand {
if ( ! $anilistEnabled)
{
$this->echoErrorBox('Anlist API is not enabled. Can not sync.');
die();
exit();
}
// Authentication is required to update Kitsu
@ -296,17 +296,7 @@ final class SyncLists extends BaseCommand {
{
$uType = ucfirst($type);
$kitsuCount = 0;
try
{
$kitsuCount = $this->kitsuModel->{"get{$uType}ListCount"}();
}
catch (FailedResponseException $e)
{
dump($e);
}
return $kitsuCount;
return $this->kitsuModel->{"get{$uType}ListCount"}() ?? 0;
}
private function fetchKitsu(string $type): array
@ -327,7 +317,10 @@ final class SyncLists extends BaseCommand {
if ( ! array_key_exists('included', $data))
{
dump($data);
dump([
'problem' => 'Missing included data in method ' . __METHOD__,
'data' => $data
]);
return [];
}
@ -742,7 +735,11 @@ final class SyncLists extends BaseCommand {
}
}
dump($responseData);
dump([
'problem' => 'Failed to update kitsu list item',
'syncDate' => $itemsToUpdate[$key],
'responseData' => $responseData,
]);
$verb = ($action === SyncAction::UPDATE) ? SyncAction::UPDATE : SyncAction::CREATE;
$this->echoError("Failed to {$verb} Kitsu {$type} list item with id: {$id}");
@ -799,7 +796,11 @@ final class SyncLists extends BaseCommand {
}
else
{
dump($responseData);
dump([
'problem' => 'Failed to update anilist list item',
'syncDate' => $itemsToUpdate[$key],
'responseData' => $responseData,
]);
$verb = ($action === SyncAction::UPDATE) ? SyncAction::UPDATE : SyncAction::CREATE;
$this->echoError("Failed to {$verb} Anilist {$type} list item with id: {$id}");
}

View File

@ -77,9 +77,6 @@ final class UpdateThumbnails extends ClearThumbnails {
$includes = JsonAPI::organizeIncludes($animeList['included']);
$animeIds = array_keys($includes['anime']);
// print_r($mangaIds);
// die();
return [
'anime' => $animeIds,
'manga' => $mangaIds,

View File

@ -267,7 +267,7 @@ final class Anime extends BaseController {
if (empty($data))
{
$this->errorPage(400, 'Bad Request', '');
die();
exit();
}
$response = $this->model->incrementLibraryItem(FormItem::from($data));