diff --git a/lib/browser/client.js b/lib/browser/client.js index 88feceba..f8868f2c 100644 --- a/lib/browser/client.js +++ b/lib/browser/client.js @@ -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 @@ -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 = ''; @@ -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'); diff --git a/lib/client.js b/lib/client.js index 49952964..d2fa1929 100644 --- a/lib/client.js +++ b/lib/client.js @@ -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 = '';