Skip to content

Commit

Permalink
fix: 403 error details not returned
Browse files Browse the repository at this point in the history
  • Loading branch information
csg01123119 committed Jul 11, 2024
1 parent f40ca3d commit ea441a2
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 23 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
2 changes: 1 addition & 1 deletion lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -408,12 +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.res = result;
}
err.requestId = result.headers['x-oss-request-id'];
err.host = '';
Expand Down

0 comments on commit ea441a2

Please sign in to comment.