Skip to content

Commit af07ce5

Browse files
authored
Merge pull request #119 from lohanidamodar/feat-node-sdk-custom-exception
Feat NodeJS Sdk custom exception
2 parents e1c9c62 + 7644adb commit af07ce5

File tree

6 files changed

+44
-5
lines changed

6 files changed

+44
-5
lines changed

src/SDK/Language/Node.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,12 @@ public function getFiles()
6666
'template' => '/node/package.json.twig',
6767
'minify' => false,
6868
],
69+
[
70+
'scope' => 'default',
71+
'destination' => 'lib/exception.js',
72+
'template' => '/node/lib/exception.js.twig',
73+
'minify' => false,
74+
],
6975
[
7076
'scope' => 'method',
7177
'destination' => 'docs/examples/{{service.name | caseLower}}/{{method.name | caseDash}}.md',

templates/node/index.js.twig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
const Client = require('./lib/client.js');
2+
const {{spec.title | caseUcfirst}}Exception = require('./lib/exception.js');
23
{% for service in spec.services %}
34
const {{service.name | caseUcfirst}} = require('./lib/services/{{service.name | caseDash}}.js');
45
{% endfor %}
56

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

templates/node/lib/client.js.twig

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const URL = require('url').URL;
22
const axios = require('axios');
33
const FormData = require('form-data');
4+
const {{spec.title | caseUcfirst}}Exception = require('./exception.js');
45

56
class Client {
67

@@ -103,10 +104,20 @@ class Client {
103104
data: (method.toUpperCase() === 'GET' || contentType.startsWith('multipart/form-data')) ? formData : params,
104105
json: (contentType.startsWith('application/json'))
105106
};
106-
107-
let response = await axios(options);
108-
109-
return response.data;
107+
try {
108+
let response = await axios(options);
109+
return response.data;
110+
} catch(error) {
111+
if('response' in error) {
112+
if('data' in error.response) {
113+
throw new {{spec.title | caseUcfirst}}Exception(error.response.data.message, error.response.status, error.response.data);
114+
}else{
115+
throw new {{spec.title | caseUcfirst}}Exception(error.response.statusText, error.response.status, error.response.data);
116+
}
117+
}else{
118+
throw new {{spec.title | caseUcfirst}}Exception(error.message);
119+
}
120+
}
110121
}
111122

112123
flatten(data, prefix = '') {
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class {{spec.title | caseUcfirst}}Exception extends Error {
2+
constructor(message, code, response) {
3+
super(message);
4+
this.code = code;
5+
this.response = response;
6+
}
7+
}
8+
9+
module.exports = {{spec.title | caseUcfirst}}Exception;

tests/SDKTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ class SDKTest extends TestCase
124124
'nodejs-12' => 'docker run --rm -v $(pwd):/app -w /app node:12.12-alpine node tests/languages/node/test.js',
125125
'nodejs-14' => 'docker run --rm -v $(pwd):/app -w /app node:14.5-alpine node tests/languages/node/test.js',
126126
],
127-
'supportException' => false,
127+
'supportException' => true,
128128
],
129129

130130
'ruby' => [

tests/languages/node/test.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,17 @@ async function start() {
5454
response = await general.upload('string', 123, ['string in array'], fs.createReadStream(__dirname + '/../../resources/file.png'));
5555
console.log(response.result);
5656

57+
try {
58+
response = await general.error400();
59+
} catch(error) {
60+
console.log(error.message);
61+
}
62+
try {
63+
response = await general.error500();
64+
} catch(error) {
65+
console.log(error.message);
66+
}
67+
5768
}
5869

5970
start();

0 commit comments

Comments
 (0)