Skip to content

Commit

Permalink
Merge pull request #6 from blockshiftnetwork/feat-improve-exception-a…
Browse files Browse the repository at this point in the history
…dd-context

feat: add context feature to exceptions of SAP
  • Loading branch information
AlexR1712 authored Apr 5, 2023
2 parents 43624cd + 1572b9d commit fdf21a7
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 8 deletions.
6 changes: 5 additions & 1 deletion SAPb1/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,10 @@ private function doRequest(string $action = '', callable $callback = null){
}

// Throw an exception when the response code is not 200.
throw new SAPException($response);
throw new SAPException($response, [
'action' => $action,
'request_query' => $requestQuery,
'status_code' => $response->getStatusCode(),
]);
}
}
5 changes: 4 additions & 1 deletion SAPb1/SAPClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ public static function createSession(array $configOptions, string $username, str
return new SAPClient($config->toArray(), $response->getCookies());
}

throw new SAPException($response);
throw new SAPException($response, [
'action' => 'login',
'status_code' => $response->getStatusCode(),
]);
}
}
17 changes: 15 additions & 2 deletions SAPb1/SAPException.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
class SAPException extends \Exception{

protected $statusCode;

protected $context;
/**
* Initializes a new instance of SAPException.
*/
public function __construct(Response $response){
public function __construct(Response $response, array $context = []){
$this->statusCode = $response->getStatusCode();
$message = '';
$erroCode = $this->code;
Expand All @@ -22,11 +22,24 @@ public function __construct(Response $response){
$message = $response->getJson()->error->message->value ?? $response->getJson()->error->message;
$erroCode = $response->getJson()->error->code;
}

$this->context = $context;

parent::__construct($message, $erroCode);
}

public function getStatusCode() : int{
return $this->statusCode;
}

/**
* Get the exception's context information.
*
* @return array<string, mixed>
*/
public function context(): array
{
return $this->context;
}

}
25 changes: 21 additions & 4 deletions SAPb1/Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,11 @@ public function create(array $data){
return true;
}

throw new SAPException($response);
throw new SAPException($response, [
'action' => 'create',
'data' => $data,
'status_code' => $response->getStatusCode(),
]);
}

/**
Expand All @@ -61,7 +65,12 @@ public function update($id, array $data) : bool{
return true;
}

throw new SAPException($response);
throw new SAPException($response, [
'action' => 'update',
'id' => $id,
'data' => $data,
'status_code' => $response->getStatusCode(),
]);
}

/**
Expand All @@ -80,7 +89,11 @@ public function delete($id) : bool{
return true;
}

throw new SAPException($response);
throw new SAPException($response, [
'action' => 'delete',
'id' => $id,
'status_code' => $response->getStatusCode(),
]);
}

/**
Expand All @@ -99,7 +112,11 @@ public function action($id, string $action) : bool{
return true;
}

throw new SAPException($response);
throw new SAPException($response, [
'action' => $action,
'id' => $id,
'status_code' => $response->getStatusCode(),
]);
}

/**
Expand Down

0 comments on commit fdf21a7

Please sign in to comment.