Skip to content

Commit 16e5834

Browse files
committed
php exception
1 parent 1058203 commit 16e5834

File tree

3 files changed

+35
-6
lines changed

3 files changed

+35
-6
lines changed

src/SDK/Language/PHP.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,12 @@ public function getFiles()
169169
'template' => '/php/src/Client.php.twig',
170170
'minify' => false,
171171
],
172+
[
173+
'scope' => 'default',
174+
'destination' => 'src/{{ spec.title | caseUcfirst}}/{{ spec.title | caseUcfirst}}Exception.php',
175+
'template' => '/php/src/Exception.php.twig',
176+
'minify' => false,
177+
],
172178
[
173179
'scope' => 'default',
174180
'destination' => '/src/{{ spec.title | caseUcfirst}}/Service.php',

templates/php/src/Client.php.twig

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
33
namespace {{ spec.title | caseUcfirst }};
44
5-
use Exception;
6-
75
class Client
86
{
97
const METHOD_GET = 'GET';
@@ -114,7 +112,7 @@ class Client
114112
* @param array $params
115113
* @param array $headers
116114
* @return array|string
117-
* @throws Exception
115+
* @throws {{spec.title | caseUcfirst}}Exception
118116
*/
119117
public function call($method, $path = '', $headers = array(), array $params = array())
120118
{
@@ -182,12 +180,17 @@ class Client
182180
break;
183181
}
184182
185-
if ((curl_errno($ch)/* || 200 != $responseStatus*/)) {
186-
throw new Exception(curl_error($ch) . ' with status code ' . $responseStatus, $responseStatus);
183+
if (curl_errno($ch)) {
184+
throw new {{spec.title | caseUcfirst}}Exception(curl_error($ch), $responseStatus, $responseBody);
187185
}
188-
186+
189187
curl_close($ch);
190188
189+
if($responseStatus < 200 || $responseStatus >= 300) {
190+
throw new {{spec.title | caseUcfirst}}Exception($responseBody['message'], $responseStatus, $responseBody);
191+
}
192+
193+
191194
return $responseBody;
192195
}
193196
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace {{ spec.title | caseUcfirst }};
4+
5+
use Exception;
6+
7+
class {{spec.title | caseUcfirst}}Exception extends Exception {
8+
private $response;
9+
10+
public function __construct($message = null, $code = 0, $response = null)
11+
{
12+
parent::__construct($message, $code);
13+
$this->response = $response;
14+
}
15+
16+
final public function getResponse()
17+
{
18+
return $this->response;
19+
}
20+
}

0 commit comments

Comments
 (0)