Skip to content

Commit

Permalink
add exception
Browse files Browse the repository at this point in the history
  • Loading branch information
daniwebdev committed Nov 22, 2023
1 parent 7463fc7 commit 7910fa7
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 9 deletions.
7 changes: 0 additions & 7 deletions src/Exceptions/HttpException.php

This file was deleted.

10 changes: 10 additions & 0 deletions src/Exceptions/RequestException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php
namespace GOAPI\IO\Exceptions;

class RequestException extends \Exception {

function __construct($message, $code)
{
parent::__construct($message, $code);
}
}
9 changes: 7 additions & 2 deletions src/StockIDX.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,15 @@ private function makeRequest($endpoint, $params = []) {

// Assuming the API returns JSON response
return json_decode($response->getBody(), true);
} catch (\Exception $e) {
} catch (\GuzzleHttp\Exception\RequestException $e) {
// Handle exceptions and errors here
// You might want to log the error or throw a custom exception
return ["error" => $e->getMessage()];
if($e->getResponse()->getStatusCode() === 401) {
$response = json_decode($e->getResponse()->getBody()->getContents(), true);
throw new \GOAPI\IO\Exceptions\RequestException($response['message'], $e->getResponse()->getStatusCode());
}

throw new \GOAPI\IO\Exceptions\RequestException($e->getMessage(), $e->getCode());
}
}

Expand Down

0 comments on commit 7910fa7

Please sign in to comment.