Skip to content

Commit

Permalink
Exception handling
Browse files Browse the repository at this point in the history
  • Loading branch information
LasseRafn committed Nov 9, 2017
1 parent 7f56cf3 commit 8d2a7f2
Showing 1 changed file with 37 additions and 8 deletions.
45 changes: 37 additions & 8 deletions src/Models/DraftInvoice.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
<?php namespace LasseRafn\Economic\Models;

use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Exception\ServerException;
use LasseRafn\Economic\Exceptions\EconomicClientException;
use LasseRafn\Economic\Exceptions\EconomicRequestException;
use LasseRafn\Economic\Utils\Model;

class DraftInvoice extends Model
Expand Down Expand Up @@ -56,8 +60,7 @@ class DraftInvoice extends Model
* @param int $quantity
* @param $product
*/
public function addLine( $description = '', $quantity = 1, $product )
{
public function addLine( $description = '', $quantity = 1, $product ) {
$line = new \stdClass();

$line->description = $description;
Expand Down Expand Up @@ -87,8 +90,7 @@ public function addLine( $description = '', $quantity = 1, $product )
*
* @return BookedInvoice
*/
public function book( $number = null, $sendBy = null )
{
public function book( $number = null, $sendBy = null ) {
$data = [
'draftInvoice' => [
'self' => $this->self,
Expand All @@ -101,12 +103,39 @@ public function book( $number = null, $sendBy = null )
}

if ( $sendBy !== null ) {
$data['sendBy'] = strtolower($sendBy);
$data['sendBy'] = strtolower( $sendBy );
}

$responseData = $this->request->curl->post( 'invoices/booked', [
'json' => $data
] )->getBody()->getContents();
try {
$responseData = $this->request->curl->post( 'invoices/booked', [
'json' => $data
] )->getBody()->getContents();
} catch ( ClientException $exception ) {
$message = $exception->getMessage();
$code = $exception->getCode();

if ( $exception->hasResponse() ) {
$message = $exception->getResponse()->getBody()->getContents();
$code = $exception->getResponse()->getStatusCode();
}

throw new EconomicRequestException( $message, $code );
} catch ( ServerException $exception ) {
$message = $exception->getMessage();
$code = $exception->getCode();

if ( $exception->hasResponse() ) {
$message = $exception->getResponse()->getBody()->getContents();
$code = $exception->getResponse()->getStatusCode();
}

throw new EconomicRequestException( $message, $code );
} catch ( \Exception $exception ) {
$message = $exception->getMessage();
$code = $exception->getCode();

throw new EconomicClientException( $message, $code );
}

$responseData = json_decode( $responseData );

Expand Down

0 comments on commit 8d2a7f2

Please sign in to comment.