Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core): Improve axios error handling in nodes #5891

Merged
merged 3 commits into from
Apr 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"@types/crypto-js": "^4.0.1",
"@types/express": "^4.17.6",
"@types/lodash.get": "^4.4.6",
"@types/lodash.pick": "^4.4.7",
"@types/mime-types": "^2.1.0",
"@types/request-promise-native": "~1.0.15",
"@types/uuid": "^8.3.2"
Expand All @@ -54,6 +55,7 @@
"flatted": "^3.2.4",
"form-data": "^4.0.0",
"lodash.get": "^4.4.2",
"lodash.pick": "^4.4.0",
"mime-types": "^2.1.27",
"n8n-workflow": "workspace:*",
"oauth-1.0a": "^2.2.6",
Expand Down
70 changes: 34 additions & 36 deletions packages/core/src/NodeExecuteFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ import {
ExpressionError,
} from 'n8n-workflow';

import pick from 'lodash.pick';
import { Agent } from 'https';
import { stringify } from 'qs';
import type { Token } from 'oauth-1.0a';
Expand Down Expand Up @@ -675,50 +676,47 @@ async function proxyRequestToAxios(
return body;
}
} catch (error) {
const { request, response, isAxiosError, toJSON, config, ...errorData } = error;
if (configObject.simple === false && response) {
if (configObject.resolveWithFullResponse) {
return {
body: response.data,
headers: response.headers,
statusCode: response.status,
statusMessage: response.statusText,
};
} else {
return response.data;
}
}
const { config, response } = error;

// Axios hydrates the original error with more data. We extract them.
// https://github.com/axios/axios/blob/master/lib/core/enhanceError.js
// Note: `code` is ignored as it's an expected part of the errorData.
if (response) {
Logger.debug('Request proxied to Axios failed', { status: response.status });
let responseData = response.data;
if (error.isAxiosError) {
if (response) {
Logger.debug('Request proxied to Axios failed', { status: response.status });
let responseData = response.data;

if (Buffer.isBuffer(responseData) || responseData instanceof Readable) {
responseData = await binaryToBuffer(responseData).then((buffer) =>
buffer.toString('utf-8'),
);
}

if (Buffer.isBuffer(responseData) || responseData instanceof Readable) {
responseData = await binaryToBuffer(responseData).then((buffer) =>
buffer.toString('utf-8'),
);
if (configObject.simple === false) {
if (configObject.resolveWithFullResponse) {
return {
body: responseData,
headers: response.headers,
statusCode: response.status,
statusMessage: response.statusText,
};
} else {
return responseData;
}
}

const message = `${response.status as number} - ${JSON.stringify(responseData)}`;
throw Object.assign(new Error(message, { cause: error }), {
statusCode: response.status,
options: pick(config ?? {}, ['url', 'method', 'data', 'headers']),
});
} else {
throw Object.assign(new Error(error.message, { cause: error }), {
options: pick(config ?? {}, ['url', 'method', 'data', 'headers']),
});
}
error.message = `${response.status as number} - ${JSON.stringify(responseData)}`;
}

error.cause = errorData;
error.error = error.response?.data || errorData;
error.statusCode = error.response?.status;
error.options = config || {};

// Remove not needed data and so also remove circular references
error.request = undefined;
error.config = undefined;
error.options.adapter = undefined;
error.options.httpsAgent = undefined;
error.options.paramsSerializer = undefined;
error.options.transformRequest = undefined;
error.options.transformResponse = undefined;
error.options.validateStatus = undefined;

throw error;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1321,7 +1321,7 @@ export class HttpRequestV3 implements INodeType {
if (autoDetectResponseFormat && response.reason.error instanceof Buffer) {
response.reason.error = Buffer.from(response.reason.error as Buffer).toString();
}
throw new NodeApiError(this.getNode(), response as JsonObject);
throw new NodeApiError(this.getNode(), response.reason as JsonObject);
} else {
// Return the actual reason as error
returnItems.push({
Expand Down
18 changes: 12 additions & 6 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.