fix: http error log not fount
This commit is contained in:
parent
9875ef92ab
commit
921e32f5db
|
@ -25,8 +25,7 @@
|
|||
"start": "npm run watch",
|
||||
"watch": "tsc -w",
|
||||
"prebuild": "rm -rf node_modules && rm -rf package-lock.json && npm i && rimraf lib",
|
||||
"build": "ncc build src/index.ts -m -e @serverless-devs/core -o lib",
|
||||
"postbuild": "rm -rf package-lock.json && rm -rf node_modules"
|
||||
"build": "ncc build src/index.ts -m -e @serverless-devs/core -o lib"
|
||||
},
|
||||
"dependencies": {
|
||||
"@alicloud/fc2": "^2.2.2",
|
||||
|
|
|
@ -1,7 +1,97 @@
|
|||
import FC from '@alicloud/fc2';
|
||||
import querystring from 'querystring';
|
||||
import kitx from 'kitx';
|
||||
import httpx from 'httpx';
|
||||
import * as core from '@serverless-devs/core';
|
||||
import { ICredentials } from '../interface/entity';
|
||||
|
||||
FC.prototype.costom_request = async function (method, path, query, body, headers = {}, opts = {}) {
|
||||
var url = `${this.endpoint}/${this.version}${path}`;
|
||||
if (query && Object.keys(query).length > 0) {
|
||||
url = `${url}?${querystring.stringify(query)}`;
|
||||
}
|
||||
|
||||
headers = Object.assign(this.buildHeaders(), this.headers, headers);
|
||||
var postBody;
|
||||
if (body) {
|
||||
var buff = null;
|
||||
if (Buffer.isBuffer(body)) {
|
||||
buff = body;
|
||||
headers['content-type'] = 'application/octet-stream';
|
||||
} else if (typeof body === 'string') {
|
||||
buff = new Buffer(body, 'utf8');
|
||||
headers['content-type'] = 'application/octet-stream';
|
||||
} else if ('function' === typeof body.pipe) {
|
||||
buff = body;
|
||||
headers['content-type'] = 'application/octet-stream';
|
||||
} else {
|
||||
buff = new Buffer(JSON.stringify(body), 'utf8');
|
||||
headers['content-type'] = 'application/json';
|
||||
}
|
||||
|
||||
if ('function' !== typeof body.pipe) {
|
||||
const digest = kitx.md5(buff, 'hex');
|
||||
const md5 = new Buffer(digest, 'utf8').toString('base64');
|
||||
|
||||
headers['content-length'] = buff.length;
|
||||
headers['content-md5'] = md5;
|
||||
}
|
||||
postBody = buff;
|
||||
}
|
||||
|
||||
var queriesToSign = null;
|
||||
if (path.startsWith('/proxy/')) {
|
||||
queriesToSign = query || {};
|
||||
}
|
||||
var signature = FC.getSignature(this.accessKeyID, this.accessKeySecret, method, `/${this.version}${path}`, headers, queriesToSign);
|
||||
headers['authorization'] = signature;
|
||||
|
||||
const response = await httpx.request(url, {
|
||||
method,
|
||||
timeout: this.timeout,
|
||||
headers,
|
||||
data: postBody
|
||||
});
|
||||
|
||||
var responseBody;
|
||||
if (!opts['rawBuf'] || response.headers['x-fc-error-type']) {
|
||||
responseBody = await httpx.read(response, 'utf8');
|
||||
} else {
|
||||
// @ts-ignore: .
|
||||
responseBody = await httpx.read(response);
|
||||
}
|
||||
|
||||
const contentType = response.headers['content-type'] || '';
|
||||
if (contentType.startsWith('application/json')) {
|
||||
try {
|
||||
responseBody = JSON.parse(responseBody);
|
||||
} catch (ex) {}
|
||||
}
|
||||
|
||||
let err;
|
||||
if (response.statusCode < 200 || response.statusCode >= 300) {
|
||||
const code = response.statusCode;
|
||||
const requestid = response.headers['x-fc-request-id'];
|
||||
var errMsg;
|
||||
if (responseBody.ErrorMessage) {
|
||||
errMsg = responseBody.ErrorMessage;
|
||||
} else {
|
||||
errMsg = responseBody.errorMessage;
|
||||
}
|
||||
err = new Error(`${method} ${path} failed with ${code}. requestid: ${requestid}, message: ${errMsg}.`);
|
||||
err.name = `FC${responseBody.ErrorCode}Error`;
|
||||
// @ts-ignore: .
|
||||
err.code = responseBody.ErrorCode;
|
||||
}
|
||||
|
||||
return {
|
||||
err,
|
||||
code: response.statusCode,
|
||||
'headers': response.headers,
|
||||
'data': responseBody,
|
||||
};
|
||||
}
|
||||
|
||||
export default class Client {
|
||||
static async buildFcClient(region: string, credentials: ICredentials) {
|
||||
return new FC(credentials.AccountID, {
|
||||
|
|
|
@ -118,18 +118,18 @@ export default class RemoteInvoke {
|
|||
logger.debug(`method is ${mt}.`);
|
||||
logger.debug(`start invoke.`);
|
||||
if (mt === 'GET') {
|
||||
resp = await this.fcClient.get(p, queries, headers);
|
||||
resp = await this.fcClient.costom_request('GET', p, queries, null, headers);
|
||||
} else if (mt === 'POST') {
|
||||
resp = await this.fcClient.post(p, body, headers, queries);
|
||||
resp = await this.fcClient.costom_request('POST', p, queries, body, headers);
|
||||
} else if (mt === 'PUT') {
|
||||
resp = await this.fcClient.put(p, body, headers);
|
||||
resp = await this.fcClient.costom_request('PUT', p, null, body, headers);
|
||||
} else if (mt === 'DELETE') {
|
||||
resp = await this.fcClient.request('DELETE', p, queries, null, headers);
|
||||
/* else if (method.toLocaleUpperCase() === 'PATCH') {
|
||||
resp = await this.fcClient.request('PATCH', p, queries, body, headers);
|
||||
resp = await this.fcClient.costom_request('DELETE', p, queries, null, headers);
|
||||
}
|
||||
else if (method.toLocaleUpperCase() === 'PATCH') {
|
||||
resp = await this.fcClient.costom_request('PATCH', p, queries, body, headers);
|
||||
} else if (method.toLocaleUpperCase() === 'HEAD') {
|
||||
resp = await this.fcClient.request('HEAD', p, queries, body, headers);
|
||||
} */
|
||||
resp = await this.fcClient.costom_request('HEAD', p, queries, body, headers);
|
||||
} else {
|
||||
logger.error(`Does not support ${method} requests temporarily.`);
|
||||
}
|
||||
|
@ -142,12 +142,19 @@ export default class RemoteInvoke {
|
|||
}
|
||||
logger.debug(`end invoke.`);
|
||||
|
||||
if (resp) {
|
||||
if (resp?.err) {
|
||||
this.showLog(resp.headers['x-fc-log-result']);
|
||||
|
||||
logger.log('\nFC Invoke Result:', 'green');
|
||||
logger.log(`\nFC Invoke Result[code: ${resp.code}]:`, 'red');
|
||||
console.log(resp.data);
|
||||
console.log('\n');
|
||||
} else {
|
||||
if (resp) {
|
||||
this.showLog(resp.headers['x-fc-log-result']);
|
||||
|
||||
logger.log('\nFC Invoke Result[code: ${resp.code}]:', 'green');
|
||||
console.log(resp.data);
|
||||
console.log('\n');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue