fix(HTTP Request Node): Cleanup circular references in response (#6590)

Co-authored-by: कारतोफ्फेलस्क्रिप्ट™ <aditya@netroy.in>
This commit is contained in:
Romain Dunand
2023-07-12 12:31:32 +02:00
committed by GitHub
parent aaa9ee3949
commit aecc05b787
7 changed files with 70 additions and 71 deletions

View File

@@ -7,6 +7,7 @@
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
// eslint-disable-next-line max-classes-per-file
import { parseString } from 'xml2js';
import { removeCircularRefs, isTraversableObject } from './utils';
import type { IDataObject, INode, IStatusCodeMessages, JsonObject } from './Interfaces';
type Severity = 'warning' | 'error';
@@ -156,7 +157,7 @@ export abstract class NodeError extends ExecutionBaseError {
.map((jsonError) => {
if (typeof jsonError === 'string') return jsonError;
if (typeof jsonError === 'number') return jsonError.toString();
if (this.isTraversableObject(jsonError)) {
if (isTraversableObject(jsonError)) {
return this.findProperty(jsonError, potentialKeys);
}
return null;
@@ -168,7 +169,7 @@ export abstract class NodeError extends ExecutionBaseError {
}
return resolvedErrors.join(' | ');
}
if (this.isTraversableObject(value)) {
if (isTraversableObject(value)) {
const property = this.findProperty(value, potentialKeys);
if (property) {
return property;
@@ -180,7 +181,7 @@ export abstract class NodeError extends ExecutionBaseError {
// eslint-disable-next-line no-restricted-syntax
for (const key of traversalKeys) {
const value = jsonError[key];
if (this.isTraversableObject(value)) {
if (isTraversableObject(value)) {
const property = this.findProperty(value, potentialKeys, traversalKeys);
if (property) {
return property;
@@ -190,47 +191,6 @@ export abstract class NodeError extends ExecutionBaseError {
return null;
}
/**
* Check if a value is an object with at least one key, i.e. it can be traversed.
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
protected isTraversableObject(value: any): value is JsonObject {
return (
value &&
typeof value === 'object' &&
!Array.isArray(value) &&
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
!!Object.keys(value).length
);
}
/**
* Remove circular references from objects.
*/
protected removeCircularRefs(obj: JsonObject, seen = new Set()) {
seen.add(obj);
Object.entries(obj).forEach(([key, value]) => {
if (this.isTraversableObject(value)) {
// eslint-disable-next-line @typescript-eslint/no-unused-expressions
seen.has(value)
? (obj[key] = { circularReference: true })
: this.removeCircularRefs(value, seen);
return;
}
if (Array.isArray(value)) {
value.forEach((val, index) => {
if (seen.has(val)) {
value[index] = { circularReference: true };
return;
}
if (this.isTraversableObject(val)) {
this.removeCircularRefs(val, seen);
}
});
}
});
}
}
interface NodeOperationErrorOptions {
@@ -317,7 +277,7 @@ export class NodeApiError extends NodeError {
if (error.error) {
// only for request library error
this.removeCircularRefs(error.error as JsonObject);
removeCircularRefs(error.error as JsonObject);
}
if ((!message && (error.message || (error?.reason as IDataObject)?.message)) || description) {