Skip to content

Commit 6b3da57

Browse files
authored
Merge pull request #121 from lohanidamodar/feat-php-sdk-exceptions
feat-php-sdk-exceptions
2 parents 0b2673a + c612cb0 commit 6b3da57

File tree

4 files changed

+52
-7
lines changed

4 files changed

+52
-7
lines changed

example.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,11 @@ function getSSLPage($url) {
4545
}
4646

4747
// PHP
48-
$sdk = new SDK(new PHP(), new Swagger2($spec));
48+
$php = new PHP();
49+
$php
50+
->setComposerVendor('appwrite')
51+
->setComposerPackage('appwrite');
52+
$sdk = new SDK($php, new Swagger2($spec));
4953

5054
$sdk
5155
->setName('NAME')

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 >= 400) {
190+
throw new {{spec.title | caseUcfirst}}Exception($responseBody['message'], $responseStatus, $responseBody);
191+
}
192+
193+
191194
return $responseBody;
192195
}
193196
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace {{ spec.title | caseUcfirst }};
4+
5+
use Exception;
6+
7+
class {{spec.title | caseUcfirst}}Exception extends Exception {
8+
9+
/**
10+
* @var mixed
11+
*/
12+
private $response;
13+
14+
/**
15+
* @param String $message
16+
* @param int $code
17+
* @param mixed $response
18+
*/
19+
public function __construct($message = null, $code = 0, $response = null)
20+
{
21+
parent::__construct($message, $code);
22+
$this->response = $response;
23+
}
24+
25+
/**
26+
* @return mixed
27+
*/
28+
final public function getResponse()
29+
{
30+
return $this->response;
31+
}
32+
}

0 commit comments

Comments
 (0)