Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Throws exceptions on RPC errors #5

Merged
merged 2 commits into from
Jul 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [1.2.0] - 2020-07-26
### Changed
- IconServiceHelper::sendRequest throws exception on invalid curl request
- TransactionBuilder::send() handles the exception and returns Object or null
- Removed unnecessary TODOs

## [1.1.0] - 2020-06-11
### Added
- README, added requirements and installation instructions
Expand Down
3 changes: 0 additions & 3 deletions src/IconService/IconService.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ public function getBlockByHash($hash)
*
* @return object
*/
//TODO migrate
public function call($score, $params)
{
return $this->transactionBuilder
Expand Down Expand Up @@ -126,7 +125,6 @@ public function getBalance($address)
*
* @return object
*/
//TODO migrate
public function getScoreApi($address)
{
return $this->transactionBuilder
Expand Down Expand Up @@ -160,7 +158,6 @@ public function getTotalSupply()
* @return object
*/

//TODO Keep migrating from here on
public function getTransactionResult($txHash)
{
return $this->transactionBuilder
Expand Down
10 changes: 7 additions & 3 deletions src/Transaction/TransactionBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -226,10 +226,14 @@ public function get(): Transaction
return $this->transaction;
}

//TODO add endpoint url in here
public function send(): \stdClass
public function send(): ?\stdClass
{
return $this->iconServiceHelper->sendRequest($this->transaction->getTransactionObject());
try {
return $this->iconServiceHelper->sendRequest($this->transaction->getTransactionObject());
} catch (\Exception $e) {
echo $e->getMessage();
return null;
}
}

public function getTransaction(): Transaction
Expand Down
15 changes: 13 additions & 2 deletions src/Utils/IconServiceHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,31 @@ public function __construct(IconService $iconService)

/**
* @param $data
* @return object
* @return object|null
* @throws \Exception
*/
public function sendRequest($data)
{
//Send request to RPC
$data_string = json_encode($data);
$ch = curl_init($this->iconService->getIconServiceUrl());
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json'
));

return json_decode(curl_exec($ch));
$result = curl_exec($ch);

if (curl_errno($ch) !== 0 || strpos(strtolower($result), 'jsonrpc') === false) {
curl_close($ch);
throw new \Exception('Curl error' . curl_error($ch) !== '' ? curl_error($ch) : $result );
}

curl_close($ch);
return json_decode($result);

}
}