Skip to content

Commit 80083af

Browse files
committed
feat: uniform throw errors
1 parent e74daab commit 80083af

27 files changed

+485
-434
lines changed

.eslintignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,3 @@ coverage
22
dist
33
node_modules
44
example
5-
*.test.js

src/baas/apigw/index.js

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const { apigw } = require('tencent-cloud-sdk');
22
const { uniqueArray } = require('../../utils/index');
3+
const { TypeError } = require('../../utils/error');
34

45
class Apigw {
56
constructor(credentials = {}, region) {
@@ -19,11 +20,19 @@ class Apigw {
1920

2021
async request(inputs) {
2122
inputs.Region = this.region;
22-
const result = await this.apigwClient.request(inputs);
23-
if (result.code != 0) {
24-
throw new Error(`Request API ${inputs.Action} failed: ${result.message}`);
25-
} else {
26-
return result;
23+
try {
24+
const result = await this.apigwClient.request(inputs);
25+
26+
if (result.code != 0) {
27+
throw new TypeError(
28+
`API_APIGW_${inputs.Action}`,
29+
`Request API ${inputs.Action} failed: ${result.message}`,
30+
);
31+
} else {
32+
return result;
33+
}
34+
} catch (e) {
35+
throw new TypeError(`API_APIGW_${inputs.Action}`, e.message, e.stack);
2736
}
2837
}
2938

@@ -122,7 +131,10 @@ class Apigw {
122131

123132
if (endpoint.protocol === 'WEBSOCKET') {
124133
if (!endpoint.function.transportFunctionName) {
125-
throw new Error('"endpoints.function.transportFunctionName" is required');
134+
throw new TypeError(
135+
`PARAMETER_APIGW`,
136+
'"endpoints.function.transportFunctionName" is required',
137+
);
126138
}
127139
apiInputs.serviceWebsocketTransportFunctionName = endpoint.function.transportFunctionName;
128140
apiInputs.serviceWebsocketTransportFunctionQualifier = funcQualifier;
@@ -137,7 +149,7 @@ class Apigw {
137149
apiInputs.serviceWebsocketCleanupFunctionNamespace = funcNamespace;
138150
} else {
139151
if (!funcName) {
140-
throw new Error('"endpoints.function.functionName" is required');
152+
throw new TypeError(`PARAMETER_APIGW`, '"endpoints.function.functionName" is required');
141153
}
142154
apiInputs.serviceScfFunctionName = funcName;
143155
apiInputs.serviceScfFunctionNamespace = funcNamespace;

src/baas/apigw/index.test.js

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,32 @@
1-
const Apigw = require('./index')
1+
const Apigw = require('./index');
22

33
async function runTest() {
44
const credentials = {
55
SecretId: '',
6-
SecretKey: ''
7-
}
6+
SecretKey: '',
7+
};
88

99
const inputs = {
1010
region: 'ap-guangzhou',
1111
serviceId: 'service-7i9kk5a8',
1212
protocols: ['http', 'https'],
1313
serviceName: 'serverless',
1414
environment: 'release',
15-
customDomains: [
16-
{
17-
domain: 'fullstack.yugasun.com',
18-
// TODO: change to your certId
19-
certificateId: '123456',
20-
isDefaultMapping: 'FALSE',
21-
pathMappingSet: [
22-
{
23-
path: '/',
24-
environment: 'release'
25-
}
26-
],
27-
protocols: ['http', 'https']
28-
}
29-
],
15+
// customDomains: [
16+
// {
17+
// domain: 'fullstack.yugasun.com',
18+
// // TODO: change to your certId
19+
// certificateId: '123456',
20+
// isDefaultMapping: 'FALSE',
21+
// pathMappingSet: [
22+
// {
23+
// path: '/',
24+
// environment: 'release',
25+
// },
26+
// ],
27+
// protocols: ['http', 'https'],
28+
// },
29+
// ],
3030
endpoints: [
3131
{
3232
apiId: 'api-a05zvycu',
@@ -35,7 +35,7 @@ async function runTest() {
3535
method: 'GET',
3636
apiName: 'index',
3737
function: {
38-
functionName: 'egg-function'
38+
functionName: 'egg-function',
3939
},
4040
usagePlan: {
4141
usagePlanName: 'slscmp',
@@ -45,17 +45,21 @@ async function runTest() {
4545
auth: {
4646
serviceTimeout: 15,
4747
secretName: 'secret',
48-
}
48+
},
4949

50-
}
51-
]
52-
}
53-
const apigw = new Apigw(credentials, inputs.region)
54-
const outputs = await apigw.deploy(inputs)
50+
},
51+
],
52+
};
53+
const apigw = new Apigw(credentials, inputs.region);
54+
const outputs = await apigw.deploy(inputs);
5555
console.log('outputs', JSON.stringify(outputs));
5656

5757

58-
await apigw.remove(outputs)
58+
await apigw.remove(outputs);
5959
}
6060

61-
runTest()
61+
runTest();
62+
63+
process.on('unhandledRejection', (e) => {
64+
throw e;
65+
});

src/baas/cam/index.js

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const { cam } = require('tencent-cloud-sdk');
2+
const { TypeError } = require('../../utils/error');
23

34
class Cam {
45
constructor(credentials = {}, region) {
@@ -7,10 +8,21 @@ class Cam {
78
this.camClient = new cam(this.credentials);
89
}
910

10-
async sleep(ms) {
11-
return new Promise((resolve) => {
12-
setTimeout(resolve, ms);
13-
});
11+
async request(data) {
12+
try {
13+
const res = await this.camClient.request(data);
14+
15+
if (res.Response && res.Response.Error) {
16+
throw new TypeError(
17+
`API_CAM_${data.Action}`,
18+
JSON.stringify(res.Response),
19+
res.Response.RequestId,
20+
);
21+
}
22+
return res;
23+
} catch (e) {
24+
throw new TypeError(`API_CAM_${data.Action}`, e.message, e.stack);
25+
}
1426
}
1527

1628
async DescribeRoleList(page, limit) {
@@ -20,7 +32,7 @@ class Cam {
2032
Page: page,
2133
Rp: limit,
2234
};
23-
return await this.camClient.request(reqParams);
35+
return this.request(reqParams);
2436
}
2537

2638
async ListRolePoliciesByRoleId(roleId, page, limit) {
@@ -31,7 +43,7 @@ class Cam {
3143
Rp: limit,
3244
RoleId: roleId,
3345
};
34-
return await this.camClient.request(reqParams);
46+
return this.request(reqParams);
3547
}
3648

3749
async CreateRole(roleName, policiesDocument) {
@@ -42,7 +54,7 @@ class Cam {
4254
PolicyDocument: policiesDocument,
4355
Description: 'Serverless Framework',
4456
};
45-
return await this.camClient.request(reqParams);
57+
return this.request(reqParams);
4658
}
4759

4860
// api limit qps 3/s
@@ -53,16 +65,13 @@ class Cam {
5365
AttachRoleId: roleId,
5466
PolicyName: policyName,
5567
};
56-
return await this.camClient.request(reqParams);
68+
return this.request(reqParams);
5769
}
5870

5971
async CheckSCFExcuteRole() {
6072
const ScfExcuteRoleName = 'QCS_SCFExcuteRole';
6173

6274
const roles = await this.DescribeRoleList(1, 200);
63-
if (roles.Response.Error) {
64-
throw new Error(roles.Response.Error.Message);
65-
}
6675

6776
const len = roles.Response.List.length;
6877
for (var i = 0; i < len; i++) {

src/baas/cam/index.test.js

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
1-
const secret = require('../../../../secret')
2-
const CamUtils = require('./index')
1+
const CamUtils = require('./index');
32

43
class ClientTest {
54
async camTest() {
65
const cam = new CamUtils({
7-
SecretId: secret.SecretId,
8-
SecretKey: secret.SecretKey
9-
})
10-
const ret = await cam.CheckSCFExcuteRole()
11-
console.log(ret)
6+
SecretId: '',
7+
SecretKey: '',
8+
});
9+
const ret = await cam.CheckSCFExcuteRole();
10+
console.log(ret);
1211
}
1312
}
1413

15-
new ClientTest().camTest()
14+
new ClientTest().camTest();
15+
16+
process.on('unhandledRejection', (e) => {
17+
throw e;
18+
});

src/baas/cdn/apis.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
1-
function HttpError(code, message, reqId) {
2-
this.code = code || 0;
3-
this.message = reqId ? `${reqId}, ${message || ''}` : message || '';
4-
}
5-
6-
HttpError.prototype = Error.prototype;
1+
const { TypeError } = require('../../utils/error');
72

83
function isEmpty(val) {
94
return val === undefined || val === null || (typeof val === 'number' && isNaN(val));
@@ -41,11 +36,16 @@ function apiFactory(actions) {
4136
},
4237
);
4338
if (Response && Response.Error && Response.Error.Code) {
44-
throw new HttpError(Response.Error.Code, Response.Error.Message, Response.RequestId);
39+
throw new TypeError(
40+
`API_CDN_${action}`,
41+
Response.Error.Message,
42+
null,
43+
Response.RequestId,
44+
);
4545
}
4646
return Response;
4747
} catch (e) {
48-
throw new HttpError(500, e.message);
48+
throw new TypeError(`API_CDN_${action}`, JSON.stringify(e), e.stack);
4949
}
5050
};
5151
});

src/baas/cdn/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const { Capi } = require('@tencent-sdk/capi');
22
const { sleep, waitResponse } = require('@ygkit/request');
3+
const { TypeError } = require('../../utils/error');
34
const {
45
AddCdnDomain,
56
UpdateDomainConfig,
@@ -240,7 +241,7 @@ class Cdn {
240241
async remove(inputs = {}) {
241242
const { domain } = inputs;
242243
if (!domain) {
243-
throw new Error('domain is required');
244+
throw new TypeError(`PARAMETER_CDN`, 'domain is required');
244245
}
245246

246247
// need circle for deleting, after domain status is 6, then we can delete it

src/baas/cdn/index.test.js

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
const Cdn = require('./index')
1+
const Cdn = require('./index');
22

33
async function runTest() {
44
const credentials = {
55
SecretId: '',
6-
SecretKey: ''
7-
}
6+
SecretKey: '',
7+
};
88

99
// const inputs = {
1010
// async: true,
@@ -49,28 +49,27 @@ async function runTest() {
4949
switch: 'on',
5050
http2: 'on',
5151
certInfo: {
52-
certId: 'xxx'
53-
}
52+
certId: 'xxx',
53+
},
5454
},
5555
forceRedirect: {
5656
switch: 'on',
5757
redirectType: 'https',
58-
redirectStatusCode: 301
59-
}
60-
}
61-
const cdn = new Cdn(credentials, inputs.region)
62-
const outputs = await cdn.deploy(inputs)
63-
console.log(outputs)
58+
redirectStatusCode: 301,
59+
},
60+
};
61+
const cdn = new Cdn(credentials, inputs.region);
62+
const outputs = await cdn.deploy(inputs);
63+
console.log(outputs);
6464

65-
// await cdn.remove({
66-
// domain: 'fullstack.yugasun.com'
67-
// })
65+
await cdn.remove({
66+
domain: 'fullstack.yugasun.com',
67+
});
6868
}
6969

70-
runTest()
70+
runTest();
7171

7272

7373
process.on('unhandledRejection', (e) => {
7474
console.log(e);
75-
76-
})
75+
});

0 commit comments

Comments
 (0)