Skip to content

Commit 8b3dc2f

Browse files
committed
fix(cos): update cos sdk version and refactor cos error
1 parent 81eb357 commit 8b3dc2f

File tree

4 files changed

+74
-73
lines changed

4 files changed

+74
-73
lines changed

__tests__/cos.test.ts

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ describe('Cos', () => {
7272
};
7373
const cos = new Cos(credentials, process.env.REGION);
7474

75-
test('[cos] deploy cos fail', async () => {
75+
test('deploy cos fail', async () => {
7676
try {
7777
const res = await cos.deploy({ ...inputs, bucket: '1234567890' });
7878
expect(res).toBe(undefined);
@@ -82,31 +82,42 @@ describe('Cos', () => {
8282
}
8383
});
8484

85-
test('[cos] convert error correct', async () => {
85+
test('convert error correct', async () => {
8686
expect(
8787
convertCosError({
88+
code: 'error',
8889
message: 'message',
89-
requestId: '123',
90+
headers: {
91+
'x-cos-request-id': '123',
92+
},
93+
error: undefined,
9094
}).message,
9195
).toBe('message (reqId: 123)');
9296

9397
expect(
9498
convertCosError({
99+
code: 'error',
100+
message: 'error_message',
95101
error: 'message',
96102
}).message,
97-
).toBe('message');
103+
).toBe('error_message');
98104

99105
expect(
100106
convertCosError({
107+
code: 'error',
108+
message: 'error_message',
109+
headers: {
110+
'x-cos-request-id': '123',
111+
},
101112
error: {
113+
Code: 'error',
102114
Message: 'message',
103-
RequestId: '123',
104115
},
105116
}).message,
106117
).toBe('message (reqId: 123)');
107118
});
108119

109-
test('[cos] should deploy cos', async () => {
120+
test('should deploy cos', async () => {
110121
const res = await cos.deploy(inputs);
111122
await sleep(1000);
112123
const reqUrl = `https://${bucket}.cos.${process.env.REGION}.myqcloud.com/index.html`;
@@ -115,7 +126,7 @@ describe('Cos', () => {
115126
expect(data).toMatch(/Serverless/gi);
116127
});
117128

118-
test('[cos] deploy cos again (update)', async () => {
129+
test('deploy cos again (update)', async () => {
119130
const res = await cos.deploy(inputs);
120131
await sleep(1000);
121132
const reqUrl = `https://${bucket}.cos.${process.env.REGION}.myqcloud.com/index.html`;
@@ -124,7 +135,7 @@ describe('Cos', () => {
124135
expect(data).toMatch(/Serverless/gi);
125136
});
126137

127-
test('[cos] getObjectUrl', async () => {
138+
test('getObjectUrl', async () => {
128139
const res = await cos.getObjectUrl({
129140
bucket,
130141
object: 'index.html',
@@ -201,7 +212,7 @@ describe('Cos', () => {
201212
expect(data).toMatch(/Serverless/gi);
202213
});
203214

204-
test('[cos] remove success', async () => {
215+
test('remove success', async () => {
205216
await cos.remove(inputs);
206217
try {
207218
await cos.getBucket({

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@
8787
"@types/jest": "^26.0.20",
8888
"@types/node": "^14.14.31",
8989
"@ygkit/request": "^0.1.8",
90-
"cos-nodejs-sdk-v5": "2.8.6",
90+
"cos-nodejs-sdk-v5": "^2.9.20",
9191
"dayjs": "^1.10.4",
9292
"moment": "^2.29.1",
9393
"tencent-cloud-sdk": "^1.0.5",

src/modules/cos/index.ts

Lines changed: 52 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { RegionType, CapiCredentials } from './../interface';
22
import COS, {
33
CORSRule,
4+
CosSdkError,
45
LifecycleRule,
56
PutBucketAclParams,
67
PutBucketCorsParams,
@@ -36,53 +37,38 @@ import fs from 'fs';
3637
import { traverseDirSync } from '../../utils';
3738
import { ApiTypeError, ApiError } from '../../utils/error';
3839

39-
export interface CosError {
40-
error?:
41-
| {
42-
Code?: string;
43-
Message?: string;
44-
Stack?: string;
45-
RequestId?: string;
46-
}
47-
| string;
48-
code?: string;
49-
message?: string;
50-
stack?: string;
51-
requestId?: string;
40+
export interface CosInsideError {
41+
Code: string;
42+
Message: string;
43+
RequestId?: string;
44+
Resource?: string;
45+
TraceId?: string;
5246
}
5347

5448
/** 将 Cos error 转为统一的形式 */
55-
export function convertCosError(err: CosError) {
49+
export function convertCosError(err: CosSdkError) {
50+
let { code } = err;
51+
const reqId = err?.headers && err?.headers['x-cos-request-id'];
52+
const traceId = err?.headers && err?.headers['x-cos-trace-id'];
53+
const msgSuffix = reqId ? ` (reqId: ${reqId}${traceId ? `, traceId: ${traceId}` : ''})` : '';
5654
if (typeof err.error === 'string') {
5755
return {
58-
code: err.code!,
59-
message: err.message! ?? err.error,
60-
stack: err?.stack,
61-
reqId: err?.requestId,
56+
code,
57+
message: `${err.message ?? err.error}`,
58+
reqId,
6259
};
6360
}
61+
const error = err.error as CosInsideError;
62+
code = error?.Code || err.code;
63+
const message = `${error?.Message || err.message}${msgSuffix}`;
6464
return {
65-
code: err?.error?.Code ?? err.code!,
66-
message: err?.error?.Message
67-
? `${err?.error?.Message} (reqId: ${err.error.RequestId})`
68-
: `${err.message!} (reqId: ${err.requestId!})`,
69-
stack: err?.stack ?? err?.error?.Stack!,
70-
reqId: err?.error?.RequestId ?? err.requestId!,
65+
code,
66+
message: `${message}`,
67+
reqId,
7168
};
7269
}
7370

74-
function constructCosError(
75-
type: string,
76-
err: {
77-
error: {
78-
Code: string;
79-
Message: string;
80-
Stack: string;
81-
RequestId: string;
82-
};
83-
stack: string;
84-
},
85-
) {
71+
function constructCosError(type: string, err: CosSdkError) {
8672
const e = convertCosError(err);
8773
return new ApiError({ type, ...e });
8874
}
@@ -179,17 +165,16 @@ export default class Cos {
179165
const accessControlPolicy: Exclude<typeof setAclParams.AccessControlPolicy, undefined> = {
180166
Owner: {
181167
ID: acp?.owner?.id!,
182-
DisplayName: acp?.owner?.displayName!,
183168
},
184-
Grants: {
185-
Permission: acp?.grants?.permission!,
186-
// FIXME: dont have URI
187-
Grantee: {
188-
ID: acp?.grants?.grantee?.id!,
189-
DisplayName: acp.grants?.grantee?.displayName!,
190-
// URI: acp?.grants?.grantee?.uri!,
169+
Grants: [
170+
{
171+
Permission: acp?.grants?.permission!,
172+
// FIXME: dont have URI
173+
Grantee: {
174+
ID: acp?.grants?.grantee?.id!,
175+
},
191176
},
192-
},
177+
],
193178
};
194179
setAclParams.AccessControlPolicy = accessControlPolicy;
195180
}
@@ -444,23 +429,28 @@ export default class Cos {
444429
}
445430

446431
async getObjectUrl(inputs: CosGetObjectUrlInputs = {}) {
447-
try {
448-
const res = await this.cosClient.getObjectUrl({
449-
Bucket: inputs.bucket!,
450-
Region: this.region,
451-
Key: inputs.object!,
452-
// default request method is GET
453-
Method: inputs.method ?? 'GET',
454-
// default expire time is 15min
455-
Expires: inputs.expires ?? 900,
456-
// default is sign url
457-
Sign: inputs.sign === false ? false : true,
458-
});
459-
// FIXME: Fuck you Cos SDK, res is not an object;
460-
return res as unknown as string;
461-
} catch (err) {
462-
throw constructCosError(`API_COS_getObjectUrl`, err);
463-
}
432+
return new Promise((resolve, reject) => {
433+
this.cosClient.getObjectUrl(
434+
{
435+
Bucket: inputs.bucket!,
436+
Region: this.region,
437+
Key: inputs.object!,
438+
// default request method is GET
439+
Method: inputs.method ?? 'GET',
440+
// default expire time is 15min
441+
Expires: inputs.expires ?? 900,
442+
// default is sign url
443+
Sign: inputs.sign === false ? false : true,
444+
},
445+
(err, data) => {
446+
if (err) {
447+
reject(constructCosError(`API_COS_getObjectUrl`, err));
448+
return;
449+
}
450+
resolve(data.Url);
451+
},
452+
);
453+
});
464454
}
465455

466456
async getBucketObjects(bucket: string) {

src/modules/cos/interface.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export interface CosSetAclInputs {
1212
displayName?: string;
1313
};
1414
grants?: {
15-
permission?: 'READ' | 'WRITE';
15+
permission?: 'READ' | 'WRITE' | 'READ_ACP' | 'WRITE_ACP' | 'FULL_CONTROL';
1616
grantee?: {
1717
id?: string;
1818
displayName?: string;

0 commit comments

Comments
 (0)