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 fb71ce3 commit 0004de9
Showing 1 changed file with 35 additions and 21 deletions.
56 changes: 35 additions & 21 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ const merge = require('merge-descriptors');
const platform = require('platform');
const utility = require('utility');
const urllib = require('urllib');
const {
Base64: { decode: base64ToStr }
} = require('js-base64');
const pkg = require('../package.json');
const bowser = require('bowser');
const signUtils = require('./common/signUtils');
Expand Down Expand Up @@ -356,6 +359,31 @@ 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.hostId = info.HostId;
};

if (result.name === 'ResponseTimeoutError') {
err = new Error(result.message);
err.name = result.name;
Expand All @@ -382,6 +410,12 @@ proto.requestError = async function requestError(result) {
err = new Error(`Unknow error, status: ${result.status}`);
err.name = 'UnknownError';
err.status = result.status;
const ossErr = result.headers['x-oss-err'];
if (ossErr) {
const message = base64ToStr(ossErr);
await setError(message);
}
err.res = result;
}
err.requestId = result.headers['x-oss-request-id'];
err.host = '';
Expand All @@ -390,27 +424,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

0 comments on commit 0004de9

Please sign in to comment.