Skip to content

Commit 775357b

Browse files
committed
fix(cos): create bucket error message
1 parent 138c6a0 commit 775357b

File tree

2 files changed

+48
-48
lines changed

2 files changed

+48
-48
lines changed

src/modules/cos/index.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ class Cos {
3535
}
3636
e.code = err.error.Code;
3737
e.requestId = err.error.RequestId;
38-
e.traceId = err.error.TraceId;
3938
}
4039
reject(e);
4140
}
@@ -63,7 +62,7 @@ class Cos {
6362
}
6463
} else {
6564
// TODO: cos在云函数中可能出现ECONNRESET错误,没办法具体定位,初步猜测是客户端问题,是函数启动网络还没准备好,这个还不确定,所以在这里做兼容
66-
if (JSON.stringify(e).includes('ECONNRESET')) {
65+
if (e.message.includes('ECONNRESET')) {
6766
// 检查bucket是否存在
6867
const headHandler = this.promisify(this.cosClient.headBucket.bind(this.cosClient));
6968
try {
@@ -81,10 +80,10 @@ class Cos {
8180
throw new TypeError(`API_COS_headBucket`, 'Could not find this bucket');
8281
}
8382
} catch (err) {
84-
throw new TypeError(`API_COS_headBucket`, JSON.stringify(err), err.stack);
83+
throw new TypeError(`API_COS_headBucket`, err.message, err.stack);
8584
}
8685
} else {
87-
throw new TypeError(`API_COS_putBucket`, JSON.stringify(e), e.stack);
86+
throw new TypeError(`API_COS_putBucket`, e.message, e.stack, e.reqId);
8887
}
8988
}
9089
}

src/modules/cos/index.test.js

Lines changed: 45 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,51 @@
1-
const CosUtils = require('./index');
1+
const Client = require('./index');
22

3-
class ClientTest {
4-
async cosTest() {
5-
const APP_ID = '1251556596';
6-
const bucketName = 'test-bucket';
7-
const cos = new CosUtils({
8-
SecretId: '',
9-
SecretKey: '',
10-
});
11-
const cosDemo = {
12-
bucket: `${bucketName}-${APP_ID}`,
13-
force: true,
14-
acl: {
15-
permissions: 'private',
16-
},
17-
tags: [
18-
{
19-
key: 'test',
20-
value: 'abcd',
21-
},
22-
],
23-
rules: [
24-
{
25-
status: 'Enabled',
26-
id: 'deleteObject',
27-
filter: '',
28-
expiration: { days: '10' },
29-
abortIncompleteMultipartUpload: { daysAfterInitiation: '10' },
30-
},
31-
],
32-
};
33-
const result = await cos.deploy(cosDemo);
34-
console.log(JSON.stringify(result));
35-
console.log(result);
3+
async function runTest() {
4+
const APP_ID = '1251556596';
5+
const bucketName = 'test-bucket';
6+
const cos = new Client({
7+
SecretId: '',
8+
SecretKey: '',
9+
});
10+
const inputs = {
11+
bucket: `${bucketName}-${APP_ID}`,
12+
force: true,
13+
acl: {
14+
permissions: 'private',
15+
},
16+
tags: [
17+
{
18+
key: 'test',
19+
value: 'abcd',
20+
},
21+
],
22+
rules: [
23+
{
24+
status: 'Enabled',
25+
id: 'deleteObject',
26+
filter: '',
27+
expiration: { days: '10' },
28+
abortIncompleteMultipartUpload: { daysAfterInitiation: '10' },
29+
},
30+
],
31+
};
32+
const result = await cos.deploy(inputs);
33+
console.log(result);
3634

35+
await cos.upload({
36+
bucket: `${bucketName}-${APP_ID}`,
37+
dir: '../../utils/',
38+
});
3739

38-
await cos.upload({
39-
bucket: `${bucketName}-${APP_ID}`,
40-
dir: '../../utils/',
41-
});
40+
await cos.remove({
41+
bucket: `${bucketName}-${APP_ID}`,
42+
});
4243

43-
await cos.remove({
44-
bucket: `${bucketName}-${APP_ID}`,
45-
});
46-
47-
}
4844
}
4945

50-
new ClientTest().cosTest();
46+
runTest();
47+
48+
process.on('unhandledRejection', (e) => {
49+
console.log(e);
50+
});
51+

0 commit comments

Comments
 (0)