Skip to content

Commit 41f5a5b

Browse files
authored
Merge pull request #124 from lohanidamodar/feat-deno-exceptions
Feat Deno SDK exception
2 parents af07ce5 + b38b3f6 commit 41f5a5b

File tree

6 files changed

+49
-7
lines changed

6 files changed

+49
-7
lines changed

src/SDK/Language/Deno.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ public function getFiles()
3636
'template' => '/deno/src/service.ts.twig',
3737
'minify' => false,
3838
],
39+
[
40+
'scope' => 'default',
41+
'destination' => '/src/exception.ts',
42+
'template' => '/deno/src/exception.ts.twig',
43+
'minify' => false,
44+
],
3945
[
4046
'scope' => 'service',
4147
'destination' => '/src/services/{{service.name | caseDash}}.ts',

templates/deno/mod.ts.twig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import { Client } from "./src/client.ts";
2+
import { {{spec.title | caseUcfirst}}Exception } from "./src/exception.ts";
23
{% for service in spec.services %}
34
import { {{service.name | caseUcfirst}} } from "./src/services/{{service.name | caseDash}}.ts";
45
{% endfor %}
56

67
export {
78
Client,
9+
{{spec.title | caseUcfirst}}Exception,
810
{% for service in spec.services %}
911
{{service.name | caseUcfirst}},
1012
{% endfor %}

templates/deno/src/client.ts.twig

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { {{ spec.title | caseUcfirst}}Exception } from './exception.ts';
2+
13
export interface DocumentData {
24
[key: string]: any;
35
}
@@ -87,14 +89,22 @@ export class Client {
8789
body: body,
8890
};
8991

90-
let response = await fetch(url.toString(), options);
91-
const contentType = response.headers.get('content-type');
92+
try {
93+
let response = await fetch(url.toString(), options);
94+
const contentType = response.headers.get('content-type');
9295

93-
if (contentType && contentType.includes('application/json')) {
94-
return response.json()
95-
}
96+
if(response.status >= 400) {
97+
let res = await response.json();
98+
throw new {{ spec.title | caseUcfirst}}Exception(res.message, res.status, res);
99+
}
96100

97-
return response;
101+
if (contentType && contentType.includes('application/json')) {
102+
return response.json()
103+
}
104+
return response;
105+
} catch(error) {
106+
throw new {{ spec.title | caseUcfirst}}Exception(error?.response?.message || error.message, error?.response?.code, error.response);
107+
}
98108
}
99109

100110
flatten(data: DocumentData, prefix = '') {
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
export class {{ spec.title | caseUcfirst}}Exception {
2+
message: String;
3+
code: Number;
4+
response: any;
5+
6+
constructor(message: String, code: Number, response: any) {
7+
this.message = message;
8+
this.code = code;
9+
this.response = response;
10+
}
11+
12+
}

tests/SDKTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ class SDKTest extends TestCase
110110
'envs' => [
111111
'deno-1.1.3' => 'docker run --rm -v $(pwd):/app -w /app hayd/alpine-deno:1.1.3 run --allow-net --allow-read tests/languages/deno/tests.ts', // TODO: use official image when its out
112112
],
113-
'supportException' => false,
113+
'supportException' => true,
114114
],
115115

116116
'node' => [

tests/languages/deno/tests.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,18 @@ async function start() {
7171
response = await general.upload('string', 123, ['string in array'], file)
7272
// @ts-ignore
7373
console.log(response.result)
74+
75+
try {
76+
response = await general.error400();
77+
}catch(error){
78+
console.log(error.message);
79+
}
80+
81+
try {
82+
response = await general.error500();
83+
}catch(error){
84+
console.log(error.message);
85+
}
7486
}
7587

7688
start()

0 commit comments

Comments
 (0)