From 0004de9ab87ded89bc479ebe5958d4959b01e2ce Mon Sep 17 00:00:00 2001 From: csg01123119 Date: Thu, 11 Jul 2024 11:22:59 +0800 Subject: [PATCH] fix: 403 error details not returned --- lib/client.js | 56 ++++++++++++++++++++++++++++++++------------------- 1 file changed, 35 insertions(+), 21 deletions(-) diff --git a/lib/client.js b/lib/client.js index dcd07c81..c2b28214 100644 --- a/lib/client.js +++ b/lib/client.js @@ -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'); @@ -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; @@ -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 = ''; @@ -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);