Skip to content

Commit

Permalink
Refactor error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
3m1n3nc3 committed Sep 5, 2024
1 parent 8d76858 commit fca93df
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 11 deletions.
18 changes: 12 additions & 6 deletions src/Apis/CuttlyRegular.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,11 +206,7 @@ public function send(): ShortenResponse|BaseResponse|StatsResponse
} catch (\GuzzleHttp\Exception\ClientException $e) {
$body = (string)$e->getResponse()->getBody();

return match (true) {
isset($this->query['edit']) => Thrower::editing($body),
isset($this->query['stats']) => Thrower::stats($body),
default => Thrower::shortener($body),
};
return $this->buildError($body);
}

$body = (string)$response->getBody();
Expand All @@ -219,6 +215,7 @@ public function send(): ShortenResponse|BaseResponse|StatsResponse
$data = json_decode($body);

return match (true) {
is_string($data) => $this->buildError($body),
isset($this->query['edit']) => new BaseResponse($data),
isset($this->query['stats']) => new StatsResponse($data->stats),
default => new ShortenResponse($data->url),
Expand All @@ -228,4 +225,13 @@ public function send(): ShortenResponse|BaseResponse|StatsResponse
$blank = (object)["error" => "No Data Available"];
return new BaseResponse($blank);
}
}

private function buildError(string $body)
{
return match (true) {
isset($this->query['edit']) => Thrower::editing($body),
isset($this->query['stats']) => Thrower::stats($body),
default => Thrower::shortener($body),
};
}
}
16 changes: 11 additions & 5 deletions src/Apis/CuttlyTeam.php
Original file line number Diff line number Diff line change
Expand Up @@ -220,11 +220,7 @@ public function send(): ShortenResponse|BaseResponse|StatsResponse
} catch (\GuzzleHttp\Exception\ClientException $e) {
$body = (string)$e->getResponse()->getBody();

return match (true) {
$this->query['action'] === 'edit' => Thrower::editing($body),
$this->query['action'] === 'stats' => Thrower::stats($body),
default => Thrower::shortener($body),
};
return $this->buildError($body);
}

$body = (string)$response->getBody();
Expand All @@ -233,6 +229,7 @@ public function send(): ShortenResponse|BaseResponse|StatsResponse
$data = json_decode($body);

return match (true) {
is_string($data) => $this->buildError($body),
$this->query['action'] === 'edit' => new BaseResponse($data),
$this->query['action'] === 'stats' => new StatsResponse($data->stats),
default => new ShortenResponse($data),
Expand All @@ -242,4 +239,13 @@ public function send(): ShortenResponse|BaseResponse|StatsResponse
$blank = (object)["error" => "No Data Available"];
return new BaseResponse($blank);
}

private function buildError(string $body)
{
return match (true) {
$this->query['action'] === 'edit' => Thrower::editing($body),
$this->query['action'] === 'stats' => Thrower::stats($body),
default => Thrower::shortener($body),
};
}
}

0 comments on commit fca93df

Please sign in to comment.