Skip to content

Commit

Permalink
feat: support EC Code and get error detail from header (#1317)
Browse files Browse the repository at this point in the history
* fix: 403 error details not returned

* fix: 403 error details not returned

* fix: 403 error details not returned

* fix: 403 error details not returned

* fix: 403 error details not returned

* fix: error detail from header

* fix: error detail from header

* fix: error detail from header

* fix: error detail from header

* fix: error detail from header

* fix: error detail from header

---------

Co-authored-by: csg01123119 <csg01123119@alibaba-inc.com>
  • Loading branch information
shungang and csg01123119 authored Jul 30, 2024
1 parent c29e15a commit 5a95778
Show file tree
Hide file tree
Showing 5 changed files with 20,210 additions and 43 deletions.
55 changes: 33 additions & 22 deletions lib/browser/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,32 @@ proto.parseXML = function parseXMLThunk(str) {

proto.requestError = async function requestError(result) {
let err = null;
const setError = async message => {
let info;
try {
info = (await this.parseXML(message)) || {};
} catch (error) {
this.debug(message, 'error');
error.message += `\nraw xml: ${message}`;
error.status = result.status;
error.requestId = result.headers['x-oss-request-id'];
return error;
}

let msg = info.Message || `unknow request error, status: ${result.status}`;
if (info.Condition) {
msg += ` (condition: ${info.Condition})`;
}
err = new Error(msg);
err.name = info.Code ? `${info.Code}Error` : 'UnknownError';
err.status = result.status;
err.code = info.Code;
err.ecCode = info.EC;
err.requestId = info.RequestId;
err.hostId = info.HostId;
err.serverTime = info.ServerTime;
};

if (!result.data || !result.data.length) {
if (result.status === -1 || result.status === -2) {
// -1 is net error , -2 is timeout
Expand All @@ -415,6 +441,12 @@ proto.requestError = async function requestError(result) {
err = new Error(`Unknow error, status: ${result.status}`);
err.name = 'UnknownError';
err.status = result.status;
err.res = result;
const ossErr = result.headers['x-oss-err'];
if (ossErr) {
const message = atob(ossErr);
await setError(message);
}
}
err.requestId = result.headers['x-oss-request-id'];
err.host = '';
Expand All @@ -423,28 +455,7 @@ proto.requestError = async function requestError(result) {
const message = String(result.data);
this.debug('request response error data: %s', message, 'error');

let info;
try {
info = (await this.parseXML(message)) || {};
} catch (error) {
this.debug(message, 'error');
error.message += `\nraw xml: ${message}`;
error.status = result.status;
error.requestId = result.headers['x-oss-request-id'];
return error;
}

let msg = info.Message || `unknow request error, status: ${result.status}`;
if (info.Condition) {
msg += ` (condition: ${info.Condition})`;
}
err = new Error(msg);
err.name = info.Code ? `${info.Code}Error` : 'UnknownError';
err.status = result.status;
err.code = info.Code;
err.requestId = info.RequestId;
err.hostId = info.HostId;
err.serverTime = info.ServerTime;
await setError(message);
}

this.debug('generate error %j', err, 'error');
Expand Down
54 changes: 33 additions & 21 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,32 @@ proto.parseXML = function parseXMLThunk(str) {

proto.requestError = async function requestError(result) {
let err = null;

const setError = async message => {
let info;
try {
info = (await this.parseXML(message)) || {};
} catch (error) {
debug(message);
error.message += `\nraw xml: ${message}`;
error.status = result.status;
error.requestId = result.headers['x-oss-request-id'];
return error;
}

let msg = info.Message || `unknow request error, status: ${result.status}`;
if (info.Condition) {
msg += ` (condition: ${info.Condition})`;
}
err = new Error(msg);
err.name = info.Code ? `${info.Code}Error` : 'UnknownError';
err.status = result.status;
err.code = info.Code;
err.requestId = info.RequestId;
err.ecCode = info.EC;
err.hostId = info.HostId;
};

if (result.name === 'ResponseTimeoutError') {
err = new Error(result.message);
err.name = result.name;
Expand All @@ -382,6 +408,12 @@ proto.requestError = async function requestError(result) {
err = new Error(`Unknow error, status: ${result.status}`);
err.name = 'UnknownError';
err.status = result.status;
err.res = result;
const ossErr = result.headers['x-oss-err'];
if (ossErr) {
const message = Buffer.from(ossErr, 'base64').toString('utf8');
await setError(message);
}
}
err.requestId = result.headers['x-oss-request-id'];
err.host = '';
Expand All @@ -390,27 +422,7 @@ proto.requestError = async function requestError(result) {
const message = String(result.data);
debug('request response error data: %s', message);

let info;
try {
info = (await this.parseXML(message)) || {};
} catch (error) {
debug(message);
error.message += `\nraw xml: ${message}`;
error.status = result.status;
error.requestId = result.headers['x-oss-request-id'];
return error;
}

let msg = info.Message || `unknow request error, status: ${result.status}`;
if (info.Condition) {
msg += ` (condition: ${info.Condition})`;
}
err = new Error(msg);
err.name = info.Code ? `${info.Code}Error` : 'UnknownError';
err.status = result.status;
err.code = info.Code;
err.requestId = info.RequestId;
err.hostId = info.HostId;
await setError(message);
}

debug('generate error %j', err);
Expand Down
Loading

0 comments on commit 5a95778

Please sign in to comment.