Skip to content

Commit

Permalink
better REST error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Ekliptor authored and Ekliptor committed Sep 4, 2019
1 parent aeaff80 commit 89727aa
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/BlockchainApi/BitcoinComRestApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function getTokenInfo(string $tokenID): ?SlpToken {

public function getAddressBalance(string $address): float {
$bchAddress = $this->getAddressDetails($address);
if ($bchAddress === null)
if ($bchAddress === null || !isset($bchAddress->balance))
return -1.0;
return $bchAddress->balance;
}
Expand All @@ -66,7 +66,7 @@ public function getAddressTokenBalance(string $address, string $tokenID): float
if ($response === false)
return -1.0;
$jsonRes = json_decode($response);
if (!$jsonRes)
if (!$jsonRes || !isset($jsonRes->balance))
return -1.0;
return $jsonRes->balance;
}
Expand Down
5 changes: 5 additions & 0 deletions src/BlockchainApi/Http/WordpressHttpAgent.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@ public function get(string $url, array $options = array()) {
$this->logError("Error on HTTP GET $url", $response->get_error_messages());
return false;
}
$responseCode = wp_remote_retrieve_response_code( $response );
$body = wp_remote_retrieve_body($response);
if ($responseCode !== 200) {
$this->logError("Invalid HTTP response code $responseCode on GET: $url", $body);
return false;
}
return $body;
}

Expand Down
2 changes: 2 additions & 0 deletions src/BlockchainApi/Structs/SlpToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ class SlpToken {
public $rate = 0.0;
/** @var string */
public $icon = '';
/** @var bool */
public $enabled = true;

public function __construct() {
}
Expand Down
4 changes: 2 additions & 2 deletions src/CashP.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,10 @@ public function generateQrCodeForAddress(string $fileLocal, string $address, flo
* @param float $amountBCH The amount in BCH to receive.
* @param float $amountToken (optional) The amount of SLP tokens to receive.
* @param string $tokenID (optional) The hex ID of the SLP token. Required if $amountToken > 0.
* @param int $tokenDigits (optional). The number of decimal places to use in the payment URI of this token.
* @return string
*/
public function createPaymentURI(string $address, float $amountBCH, float $amountToken = 0.0, string $tokenID = ""): string {
$tokenDigits = 8; // TODO add parameter for this?
public function createPaymentURI(string $address, float $amountBCH, float $amountToken = 0.0, string $tokenID = "", int $tokenDigits = 8): string {
$address = preg_replace("/.+:/i", "", $address);
// we use the bitcoincash URI if tokens are disabled because bitcoin.com and other wallets only support this as of June 2019
//$uri = "simpleledger:$address?amount=$amountBCH";
Expand Down

0 comments on commit 89727aa

Please sign in to comment.