refactor(core): Separate API response from error in execution error causes (no-changelog) (#7880)

Store the third-party API response error separately from the error
stored as `cause`

Follow-up to:
https://github.com/n8n-io/n8n/pull/7820#discussion_r1406009154
This commit is contained in:
Iván Ovejero
2023-11-30 14:44:10 +01:00
committed by GitHub
parent b024cc52e7
commit e0b7f89035
8 changed files with 56 additions and 33 deletions

View File

@@ -112,7 +112,7 @@ export class NodeApiError extends NodeError {
constructor(
node: INode,
error: JsonObject,
errorResponse: JsonObject,
{
message,
description,
@@ -125,38 +125,44 @@ export class NodeApiError extends NodeError {
messageMapping,
}: NodeApiErrorOptions = {},
) {
super(node, error);
super(node, errorResponse);
// only for request library error
if (error.error) {
removeCircularRefs(error.error as JsonObject);
if (errorResponse.error) {
removeCircularRefs(errorResponse.error as JsonObject);
}
// if not description provided, try to find it in the error object
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
if (!description && (error.description || (error?.reason as IDataObject)?.description)) {
if (
!description &&
(errorResponse.description || (errorResponse?.reason as IDataObject)?.description)
) {
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
this.description = (error.description ||
(error?.reason as IDataObject)?.description) as string;
this.description = (errorResponse.description ||
(errorResponse?.reason as IDataObject)?.description) as string;
}
// if not message provided, try to find it in the error object or set description as message
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
if (!message && (error.message || (error?.reason as IDataObject)?.message || description)) {
if (
!message &&
(errorResponse.message || (errorResponse?.reason as IDataObject)?.message || description)
) {
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
this.message = (error.message ||
this.message = (errorResponse.message ||
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
(error?.reason as IDataObject)?.message ||
(errorResponse?.reason as IDataObject)?.message ||
description) as string;
}
// if it's an error generated by axios
// look for descriptions in the response object
if (error.reason) {
const reason: IDataObject = error.reason as unknown as IDataObject;
if (errorResponse.reason) {
const reason: IDataObject = errorResponse.reason as unknown as IDataObject;
if (reason.isAxiosError && reason.response) {
error = reason.response as JsonObject;
errorResponse = reason.response as JsonObject;
}
}
@@ -165,7 +171,7 @@ export class NodeApiError extends NodeError {
this.httpCode = httpCode;
} else {
this.httpCode =
this.findProperty(error, ERROR_STATUS_PROPERTIES, ERROR_NESTING_PROPERTIES) ?? null;
this.findProperty(errorResponse, ERROR_STATUS_PROPERTIES, ERROR_NESTING_PROPERTIES) ?? null;
}
if (severity) {
@@ -181,10 +187,10 @@ export class NodeApiError extends NodeError {
if (!this.description) {
if (parseXml) {
this.setDescriptionFromXml(error.error as string);
this.setDescriptionFromXml(errorResponse.error as string);
} else {
this.description = this.findProperty(
error,
errorResponse,
ERROR_MESSAGE_PROPERTIES,
ERROR_NESTING_PROPERTIES,
);
@@ -209,8 +215,8 @@ export class NodeApiError extends NodeError {
this.description,
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
this.httpCode ||
(error?.code as string) ||
((error?.reason as JsonObject)?.code as string) ||
(errorResponse?.code as string) ||
((errorResponse?.reason as JsonObject)?.code as string) ||
undefined,
messageMapping,
);