Skip to content

Commit

Permalink
fix: fix requestError return null and signatureV4 run in Node 8.x (#1338
Browse files Browse the repository at this point in the history
)

Co-authored-by: zhengzuoyu.zzy <[email protected]>
  • Loading branch information
YunZZY and zhengzuoyu.zzy authored Nov 18, 2024
1 parent f3c31ea commit 439ffde
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 14 deletions.
11 changes: 6 additions & 5 deletions lib/browser/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ proto.requestError = async function requestError(result) {
this.debug(message, 'error');
error.message += `\nraw xml: ${message}`;
error.status = result.status;
error.requestId = result.headers['x-oss-request-id'];
error.requestId = result.headers && result.headers['x-oss-request-id'];
return error;
}

Expand All @@ -416,6 +416,7 @@ proto.requestError = async function requestError(result) {
err.requestId = info.RequestId;
err.hostId = info.HostId;
err.serverTime = info.ServerTime;
return err;
};

if (!result.data || !result.data.length) {
Expand All @@ -442,20 +443,20 @@ proto.requestError = async function requestError(result) {
err.name = 'UnknownError';
err.status = result.status;
err.res = result;
const ossErr = result.headers['x-oss-err'];
const ossErr = result.headers && result.headers['x-oss-err'];
if (ossErr) {
const message = atob(ossErr);
await setError(message);
err = await setError(message);
}
}
err.requestId = result.headers['x-oss-request-id'];
err.requestId = result.headers && result.headers['x-oss-request-id'];
err.host = '';
}
} else {
const message = String(result.data);
this.debug('request response error data: %s', message, 'error');

await setError(message);
err = await setError(message);
}

this.debug('generate error %j', err, 'error');
Expand Down
11 changes: 6 additions & 5 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ proto.requestError = async function requestError(result) {
debug(message);
error.message += `\nraw xml: ${message}`;
error.status = result.status;
error.requestId = result.headers['x-oss-request-id'];
error.requestId = result.headers && result.headers['x-oss-request-id'];
return error;
}

Expand All @@ -380,6 +380,7 @@ proto.requestError = async function requestError(result) {
err.requestId = info.RequestId;
err.ecCode = info.EC;
err.hostId = info.HostId;
return err;
};

if (result.name === 'ResponseTimeoutError') {
Expand Down Expand Up @@ -409,20 +410,20 @@ proto.requestError = async function requestError(result) {
err.name = 'UnknownError';
err.status = result.status;
err.res = result;
const ossErr = result.headers['x-oss-err'];
const ossErr = result.headers && result.headers['x-oss-err'];
if (ossErr) {
const message = Buffer.from(ossErr, 'base64').toString('utf8');
await setError(message);
err = await setError(message);
}
}
err.requestId = result.headers['x-oss-request-id'];
err.requestId = result.headers && result.headers['x-oss-request-id'];
err.host = '';
}
} else {
const message = String(result.data);
debug('request response error data: %s', message);

await setError(message);
err = await setError(message);
}

debug('generate error %j', err);
Expand Down
4 changes: 2 additions & 2 deletions lib/common/object/signatureUrlV4.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const proto = exports;
*/
proto.signatureUrlV4 = async function signatureUrlV4(method, expires, request, objectName, additionalHeaders) {
const headers = (request && request.headers) || {};
const queries = { ...((request && request.queries) || {}) };
const queries = Object.assign({}, (request && request.queries) || {});
const date = new Date();
const formattedDate = dateFormat(date, "UTC:yyyymmdd'T'HHMMss'Z'");
const onlyDate = formattedDate.split('T')[0];
Expand Down Expand Up @@ -64,7 +64,7 @@ proto.signatureUrlV4 = async function signatureUrlV4(method, expires, request, o
object: objectName
})
);
signedUrl.query = { ...queries };
signedUrl.query = Object.assign({}, queries);

return signedUrl.format();
};
2 changes: 1 addition & 1 deletion lib/common/signUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ exports.getCanonicalRequest = function getCanonicalRequest(method, request, buck
signContent.push(canonicalHeaders);

// Additional Headers
if (additionalHeaders.length > 0) {
if (additionalHeaders && additionalHeaders.length > 0) {
signContent.push(additionalHeaders.join(';'));
} else {
signContent.push('');
Expand Down
37 changes: 37 additions & 0 deletions test/browser/browser.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2345,6 +2345,43 @@ describe('browser', () => {
assert.strictEqual(netErrs.name, 'ResponseTimeoutError');
store.urllib.request.restore();
});

it('should normal processing of non-OSS errors', async () => {
const stubNetError = sinon.stub(store.urllib, 'request');
const netErr = new Error('TestNonOSSErrorException');
netErr.status = 400;
netErr.code = 'TestNonOSSError';
stubNetError.throws(netErr);
let nonOSSErr;
try {
await store.head('test.txt');
assert.fail('Expect to throw an error.');
} catch (err) {
nonOSSErr = err;
}

assert.strictEqual(nonOSSErr.message, `Unknow error, status: ${netErr.status}`);
assert.strictEqual(nonOSSErr.name, 'UnknownError');
assert.strictEqual(nonOSSErr.status, netErr.status);
stubNetError.restore();

nonOSSErr = undefined;
const stubNetError2 = sinon.stub(store.urllib, 'request');
const netErr2 = new Error('TestNonOSSErrorException');
netErr2.status = 400;
netErr2.data = 'TestNonOSSError';
stubNetError2.throws(netErr2);
try {
await store.getBucketACL('test');
assert.fail('Expect to throw an error.');
} catch (err) {
nonOSSErr = err;
}

assert(nonOSSErr.message.includes(`\nraw xml: ${netErr2.data}`));
assert.strictEqual(nonOSSErr.status, netErr2.status);
stubNetError2.restore();
});
});

describe('options.headerEncoding', () => {
Expand Down
37 changes: 37 additions & 0 deletions test/node/multipart.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -688,6 +688,43 @@ describe('test/multipart.test.js', () => {
assert.strictEqual(netErrs.name, 'abort');
store._uploadPart.restore();
});

it('should normal processing of non-OSS errors', async () => {
const stubNetError = sinon.stub(store.urllib, 'request');
const netErr = new Error('TestNonOSSErrorException');
netErr.status = 400;
netErr.code = 'TestNonOSSError';
stubNetError.throws(netErr);
let nonOSSErr;
try {
await store.head('test.txt');
assert.fail('Expect to throw an error.');
} catch (err) {
nonOSSErr = err;
}

assert.strictEqual(nonOSSErr.message, `Unknow error, status: ${netErr.status}`);
assert.strictEqual(nonOSSErr.name, 'UnknownError');
assert.strictEqual(nonOSSErr.status, netErr.status);
stubNetError.restore();

nonOSSErr = undefined;
const stubNetError2 = sinon.stub(store.urllib, 'request');
const netErr2 = new Error('TestNonOSSErrorException');
netErr2.status = 400;
netErr2.data = 'TestNonOSSError';
stubNetError2.throws(netErr2);
try {
await store.listBuckets();
assert.fail('Expect to throw an error.');
} catch (err) {
nonOSSErr = err;
}

assert(nonOSSErr.message.includes(`\nraw xml: ${netErr2.data}`));
assert.strictEqual(nonOSSErr.status, netErr2.status);
stubNetError2.restore();
});
});

describe('multipartCopy()', () => {
Expand Down
2 changes: 1 addition & 1 deletion test/node/rtmp.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ describe('test/rtmp.test.js', () => {
].forEach((moreConfigs, index) => {
describe(`test rtmp in iterate ${index}`, () => {
before(async () => {
store = oss(config);
store = oss({ ...config, ...moreConfigs });
bucket = `ali-oss-test-bucket-rtmp-${prefix.replace(/[/.]/g, '-')}${index}`;
store.useBucket(bucket);

Expand Down

0 comments on commit 439ffde

Please sign in to comment.