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

HTTP Request Node - "TypeError: Converting circular structure to JSON" #3089

Closed
mark-monteiro opened this issue Apr 4, 2022 · 10 comments
Closed

Comments

@mark-monteiro
Copy link

Describe the bug
After updating tot he latest version, the HTTP Request node in my workflow now always crashes with the error message "Workflow execution process did crash for an unknown reason!"

The log file on the server has an error message TypeError: Converting circular structure to JSON. The relevant log and stack trace has been included below.

To Reproduce
Steps to reproduce the behavior:

  1. Create HTTP request node with code below
  2. Execute node and see error

Http request node configuration:

{
  "nodes": [
    {
      "parameters": {
        "authentication": "oAuth2",
        "requestMethod": "PATCH",
        "url": "=https://www.googleapis.com/upload/drive/v3/files/{{$json[\"googleDriveFileId\"]}}",
        "allowUnauthorizedCerts": true,
        "options": {
          "fullResponse": true
        },
        "bodyParametersUi": {
          "parameter": [
            {
              "name": "mimeType",
              "value": "application/vnd.google-apps.spreadsheet"
            }
          ]
        },
        "queryParametersUi": {
          "parameter": [
            {
              "name": "uploadType",
              "value": "resumable"
            }
          ]
        }
      },
      "name": "Start Upload To Drive (Metadata)",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 1,
      "position": [
        500,
        320
      ],
      "credentials": {
        "oAuth2Api": {
          "id": "7",
          "name": "Google Drive Update API"
        }
      },
      "notes": "Details on how xlsx file is converted to Google Sheet: https://stackoverflow.com/a/36957953/1988326"
    }
  ],
  "connections": {}
}

Expected behavior
Node should not crash with an unhandled exception.

Environment (please complete the following information):

  • OS: Docker image on Ubuntu host
  • n8n Version: 0.171.0
  • Node.js Version: Deployed with docker image
  • Database system: SQLite (default)
  • Operation mode: own (default)

Additional context
Logs + stacktrace:

n8n    | 2022-04-04T20:07:02.430Z | debug    | Received child process message of type start for execution ID 3601. {"executionId":"3601","file":"WorkflowRunner.js"}
n8n    | 2022-04-04T20:07:02.440Z | verbose  | Initializing n8n sub-process {"pid":198,"workflowId":"8","file":"WorkflowRunnerProcess.js","function":"runWorkflow"}
n8n    | 2022-04-04T20:07:02.709Z | verbose  | Workflow execution started {"workflowId":"8","file":"WorkflowExecute.js","function":"processRunExecutionData"}
n8n    | 2022-04-04T20:07:02.712Z | debug    | Received child process message of type processHook for execution ID 3601. {"executionId":"3601","file":"WorkflowRunner.js"}
n8n    | 2022-04-04T20:07:02.712Z | debug    | Executing hook (hookFunctionsPush) {"executionId":"3601","sessionId":"y8wf57iym9","workflowId":"8","file":"WorkflowExecuteAdditionalData.js","function":"workflowExecuteBefore"}
n8n    | 2022-04-04T20:07:02.713Z | debug    | Send data of type "executionStarted" to editor-UI {"dataType":"executionStarted","sessionId":"y8wf57iym9","file":"Push.js","function":"send"}
n8n    | 2022-04-04T20:07:02.712Z | debug    | Start processing node "Start Upload To Drive (Metadata)" {"node":"Start Upload To Drive (Metadata)","workflowId":"8","file":"WorkflowExecute.js"}
n8n    | 2022-04-04T20:07:02.713Z | debug    | Received child process message of type processHook for execution ID 3601. {"executionId":"3601","file":"WorkflowRunner.js"}
n8n    | 2022-04-04T20:07:02.713Z | debug    | Executing hook on node "Start Upload To Drive (Metadata)" (hookFunctionsPush) {"executionId":"3601","sessionId":"y8wf57iym9","workflowId":"8","file":"WorkflowExecuteAdditionalData.js","function":"nodeExecuteBefore"}
n8n    | 2022-04-04T20:07:02.713Z | debug    | Send data of type "nodeExecuteBefore" to editor-UI {"dataType":"nodeExecuteBefore","sessionId":"y8wf57iym9","file":"Push.js","function":"send"}
n8n    | 2022-04-04T20:07:02.713Z | debug    | Running node "Start Upload To Drive (Metadata)" started {"node":"Start Upload To Drive (Metadata)","workflowId":"8","file":"WorkflowExecute.js"}
n8n    | 2022-04-04T20:07:02.760Z | debug    | Received child process message of type sendMessageToUI for execution ID 3601. {"executionId":"3601","file":"WorkflowRunner.js"}
n8n    | 2022-04-04T20:07:02.760Z | debug    | Send data of type "sendConsoleMessage" to editor-UI {"dataType":"sendConsoleMessage","sessionId":"y8wf57iym9","file":"Push.js","function":"send"}
n8n    | 2022-04-04T20:07:02.770Z | debug    | Proxying request to axios {"file":"NodeExecuteFunctions.js","function":"proxyRequestToAxios"}
n8n    | /usr/local/lib/node_modules/n8n/dist/src/Logger.js:20
n8n    |                     return `${timestamp} | ${level.padEnd(18)} | ${message}${Object.keys(metadata).length ? ` ${JSON.stringify(metadata)}` : ''}`;
n8n    |
      ^
n8n    |
n8n    | TypeError: Converting circular structure to JSON
n8n    |     --> starting at object with constructor 'TLSSocket'
n8n    |     |     property '_httpMessage' -> object with constructor 'ClientRequest'
n8n    |     --- property 'socket' closes the circle
n8n    |     at JSON.stringify (<anonymous>)
n8n    |     at Printf.template (/usr/local/lib/node_modules/n8n/dist/src/Logger.js:20:118)
n8n    |     at Printf.transform (/usr/local/lib/node_modules/n8n/node_modules/logform/printf.js:11:26)
n8n    |     at Format.transform (/usr/local/lib/node_modules/n8n/node_modules/logform/combine.js:20:24)
n8n    |     at Console._write (/usr/local/lib/node_modules/n8n/node_modules/winston-transport/index.js:91:33)
n8n    |     at doWrite (/usr/local/lib/node_modules/n8n/node_modules/winston-transport/node_modules/readable-stream/lib/_stream_writable.js:409:139)
n8n    |     at writeOrBuffer (/usr/local/lib/node_modules/n8n/node_modules/winston-transport/node_modules/readable-stream/lib/_stream_writable.js:398:5)
n8n    |     at Console.Writable.write (/usr/local/lib/node_modules/n8n/node_modules/winston-transport/node_modules/readable-stream/lib/_stream_writable.js:307:11)
n8n    |     at DerivedLogger.ondata (/usr/local/lib/node_modules/n8n/node_modules/winston/node_modules/readable-stream/lib/_stream_readable.js:681:20)
n8n    |     at DerivedLogger.emit (node:events:526:28)
n8n    | 2022-04-04T20:07:02.977Z | debug    | Subprocess for execution ID 3601 finished with error code 1. {"executionId":"3601","file":"WorkflowRunner.js"}
n8n    | 2022-04-04T20:07:02.977Z | debug    | Executing hook (hookFunctionsSave) {"executionId":"3601","workflowId":"8","file":"WorkflowExecuteAdditionalData.js","function":"workflowExecuteAfter"}
n8n    | 2022-04-04T20:07:02.980Z [Rudder] debug: no existing flush timer, creating new one
n8n    | 2022-04-04T20:07:03.037Z | debug    | Executing hook (hookFunctionsPush) {"executionId":"3601","sessionId":"y8wf57iym9","workflowId":"8","file":"WorkflowExecuteAdditionalData.js","function":"workflowExecuteAfter"}
n8n    | 2022-04-04T20:07:03.038Z | debug    | Save execution progress to database for execution ID 3601  {"executionId":"3601","workflowId":"8","file":"WorkflowExecuteAdditionalData.js","function":"workflowExecuteAfter"}
n8n    | 2022-04-04T20:07:03.038Z | debug    | Send data of type "executionFinished" to editor-UI {"dataType":"executionFinished","sessionId":"y8wf57iym9","file":"Push.js","function":"send"}
@mark-monteiro mark-monteiro changed the title HTTP Request Node Always Crashes HTTP Request Node - "TypeError: Converting circular structure to JSON" Apr 5, 2022
@Joffcom
Copy link
Member

Joffcom commented Apr 5, 2022

Hey @mark-monteiro,

That doesn't look good, I have just given the node you provided a quick run and I have not been able to reproduce it. Is there more to the workflow? Once we can reproduce the issue we will be able to get it fixed.

@mark-monteiro
Copy link
Author

@Joffcom The node was in an workflow with more nodes, but I copied it to a new workflow and executed it on it's own and still got the same error. Here's is an export of the simplified workflow that produces the error for me.

{
  "name": "Error Repro",
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        240,
        300
      ]
    },
    {
      "parameters": {
        "requestMethod": "PATCH",
        "url": "=https://www.googleapis.com/upload/drive/v3/files/{{$json[\"googleDriveFileId\"]}}",
        "allowUnauthorizedCerts": true,
        "options": {
          "fullResponse": true
        },
        "bodyParametersUi": {
          "parameter": [
            {
              "name": "mimeType",
              "value": "application/vnd.google-apps.spreadsheet"
            }
          ]
        },
        "headerParametersUi": {
          "parameter": [
            {
              "name": "X-Upload-Content-Type",
              "value": "={{$binary.attachment_0.mimeType}}"
            }
          ]
        },
        "queryParametersUi": {
          "parameter": [
            {
              "name": "uploadType",
              "value": "resumable"
            }
          ]
        }
      },
      "name": "Start Upload To Drive (Metadata)",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 1,
      "position": [
        460,
        300
      ],
      "notes": "Details on how xlsx file is converted to Google Sheet: https://stackoverflow.com/a/36957953/1988326"
    }
  ],
  "connections": {
    "Start": {
      "main": [
        [
          {
            "node": "Start Upload To Drive (Metadata)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {},
  "id": 10
}

Not sure if it will help, but here is the full workflow this node was originally in:

{
  "name": "SOS Export To Google Drive",
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        -440,
        460
      ]
    },
    {
      "parameters": {
        "format": "resolved",
        "options": {
          "customEmailConfig": "[[\"UNSEEN\"], [\"FROM\", \"[email protected]\"], [\"TO\", \"[email protected]\"]]",
          "allowUnauthorizedCerts": true
        }
      },
      "name": "IMAP Email - SOS Item Export",
      "type": "n8n-nodes-base.emailReadImap",
      "typeVersion": 1,
      "position": [
        -180,
        460
      ],
      "credentials": {
        "imap": {
          "id": "3",
          "name": "Senti IMAP account"
        }
      }
    },
    {
      "parameters": {
        "functionCode": "// Code here will run once per input item.\n// More info and help: https://docs.n8n.io/nodes/n8n-nodes-base.functionItem\n// Tip: You can use luxon for dates and $jmespath for querying JSON structures\n\n// This list of valid mime types comes from the 'about' endpoint in the Google Workspace API in the 'importFormats' array\n// See: https://developers.google.com/drive/api/guides/manage-uploads#import_to_google_docs_types\n// and: https://developers.google.com/drive/api/v3/reference/about/get?apix_params=%7B%22fields%22%3A%22importFormats%22%7D\nconst validMimeTypes = [\n  \"text/tab-separated-values\",\n  \"application/vnd.ms-excel.sheet.macroenabled.12\",\n  \"application/vnd.ms-excel\",\n  \"application/vnd.oasis.opendocument.spreadsheet\",\n  \"application/x-vnd.oasis.opendocument.spreadsheet\",\n  \"application/vnd.openxmlformats-officedocument.spreadsheetml.template\",\n  \"application/vnd.ms-excel.template.macroenabled.12\",\n  \"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet\",\n  \"text/csv\"\n];\n\n// Validate the file mime type of the input file\nconst fileMimeType = getBinaryData().attachment_0.mimeType;\nif (validMimeTypes.indexOf(fileMimeType) === -1) {\n  throw new Error(\"Invalid mime type: \" + fileMimeType);\n}\n\n// Add a new field called 'googleDriveFileId' to the JSON of the item\nif (item.subject === \"Item Export  for Senti Solutions Inc\") {\n  item.googleDriveFileId = \"1-7wJ8gHyP-XxKXSi6KQO9HRlCuK_UWtSlt1296qVZRo\";\n} else if (item.subject === \"Vendor Export  for Senti Solutions Inc\") {\n  item.googleDriveFileId = \"1WwFLfoG19eyiUu9SFc6okub8C1v4ZyPbWT2u07p5er4\";\n} else if (item.subject === \"Vendor-Item Catalog Export  for Senti Solutions Inc\") {\n  item.googleDriveFileId = \"1Es1-4lAxyr1BOaWVLkoGUyVLVNtWOtF1l9OtxhtXg_0\";\n} else {\n  throw new Error(\"Unsupported email subject line:\" + item.subject);\n}\n\n// Return the modified item\nreturn item;"
      },
      "name": "Set Google Drive File Id",
      "type": "n8n-nodes-base.functionItem",
      "typeVersion": 1,
      "position": [
        60,
        640
      ]
    },
    {
      "parameters": {
        "mode": "mergeByIndex",
        "join": "inner"
      },
      "name": "Merge",
      "type": "n8n-nodes-base.merge",
      "typeVersion": 1,
      "position": [
        500,
        640
      ]
    },
    {
      "parameters": {
        "authentication": "oAuth2",
        "requestMethod": "PATCH",
        "url": "=https://www.googleapis.com/upload/drive/v3/files/{{$json[\"googleDriveFileId\"]}}",
        "allowUnauthorizedCerts": true,
        "options": {
          "fullResponse": true
        },
        "bodyParametersUi": {
          "parameter": [
            {
              "name": "mimeType",
              "value": "application/vnd.google-apps.spreadsheet"
            }
          ]
        },
        "headerParametersUi": {
          "parameter": [
            {
              "name": "X-Upload-Content-Type",
              "value": "={{$binary.attachment_0.mimeType}}"
            }
          ]
        },
        "queryParametersUi": {
          "parameter": [
            {
              "name": "uploadType",
              "value": "resumable"
            }
          ]
        }
      },
      "name": "Start Upload To Drive (Metadata)",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 1,
      "position": [
        280,
        740
      ],
      "credentials": {
        "oAuth2Api": {
          "id": "7",
          "name": "Google Drive Update API"
        }
      },
      "notes": "Details on how xlsx file is converted to Google Sheet: https://stackoverflow.com/a/36957953/1988326"
    },
    {
      "parameters": {
        "authentication": "oAuth2",
        "requestMethod": "PATCH",
        "url": "={{$json[\"headers\"][\"location\"]}}",
        "jsonParameters": true,
        "options": {
          "bodyContentType": "raw"
        },
        "sendBinaryData": true,
        "binaryPropertyName": "attachment_0"
      },
      "name": "Finish Upload To Drive (File Content)",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 1,
      "position": [
        720,
        640
      ],
      "credentials": {
        "oAuth2Api": {
          "id": "7",
          "name": "Google Drive Update API"
        }
      },
      "notes": "Details on how xlsx file is converted to Google Sheet: https://stackoverflow.com/a/36957953/1988326"
    },
    {
      "parameters": {
        "mode": "mergeByIndex",
        "join": "inner"
      },
      "name": "Merge1",
      "type": "n8n-nodes-base.merge",
      "typeVersion": 1,
      "position": [
        300,
        200
      ],
      "disabled": true
    },
    {
      "parameters": {
        "authentication": "oAuth2",
        "requestMethod": "POST",
        "url": "https://www.googleapis.com/upload/drive/v3/files",
        "jsonParameters": true,
        "options": {
          "bodyContentType": "json",
          "fullResponse": true
        },
        "bodyParametersJson": "={\n  \"name\": \"{{$binary.attachment_0.fileName}}\",\n  \"mimeType\": \"application/vnd.google-apps.spreadsheet\",\n  \"parents\": [\"11neNYN3IHeCLE_MAOcrVKXkB5lnVZD3b\"]\n}",
        "headerParametersJson": "={ \"X-Upload-Content-Type\": \"{{$binary.attachment_0.mimeType}}\" }",
        "queryParametersJson": "{ \"uploadType\": \"resumable\" }"
      },
      "name": "Start New Upload To Drive (Metadata)",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 1,
      "position": [
        60,
        200
      ],
      "credentials": {
        "oAuth2Api": {
          "id": "7",
          "name": "Google Drive Update API"
        }
      },
      "disabled": true,
      "notes": "Execute to create new files. It is required that the file is created via this auth token so that the token also has permission to update the file at a later date"
    },
    {
      "parameters": {
        "authentication": "oAuth2",
        "requestMethod": "PUT",
        "url": "={{$json[\"headers\"][\"location\"]}}",
        "allowUnauthorizedCerts": true,
        "jsonParameters": true,
        "options": {
          "bodyContentType": "raw"
        },
        "sendBinaryData": true,
        "binaryPropertyName": "attachment_0"
      },
      "name": "Finish New Upload To Drive (File Content)",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 1,
      "position": [
        500,
        200
      ],
      "credentials": {
        "oAuth2Api": {
          "id": "7",
          "name": "Google Drive Update API"
        }
      },
      "disabled": true,
      "notes": "Details on how xlsx file is converted to Google Sheet: https://stackoverflow.com/a/36957953/1988326"
    },
    {
      "parameters": {
        "spaceId": "spaces/AAAATTakZLA",
        "messageUi": {
          "text": "=Starting import of SOS Inventory data into Google Drive for file {{$binary.attachment_0.fileName}}"
        },
        "additionalFields": {}
      },
      "name": "Google Chat Initial Message",
      "type": "n8n-nodes-base.googleChat",
      "typeVersion": 1,
      "position": [
        60,
        460
      ],
      "credentials": {
        "googleApi": {
          "id": "8",
          "name": "Google Chat Service Account"
        }
      }
    },
    {
      "parameters": {
        "spaceId": "spaces/AAAATTakZLA",
        "messageUi": {
          "text": "=File successfully updated on Google Drive: <https://docs.google.com/spreadsheets/d/{{$json[\"id\"]}}|{{$json[\"name\"]}}>"
        },
        "additionalFields": {}
      },
      "name": "Google Chat Complete Message",
      "type": "n8n-nodes-base.googleChat",
      "typeVersion": 1,
      "position": [
        940,
        640
      ],
      "credentials": {
        "googleApi": {
          "id": "8",
          "name": "Google Chat Service Account"
        }
      }
    },
    {
      "parameters": {},
      "name": "Error Trigger",
      "type": "n8n-nodes-base.errorTrigger",
      "typeVersion": 1,
      "position": [
        -180,
        980
      ]
    },
    {
      "parameters": {
        "spaceId": "spaces/AAAATTakZLA",
        "messageUi": {
          "text": "=An error occurred in n8n workflow '{{$json[\"workflow\"][\"name\"]}}' in node '{{$json[\"execution\"][\"lastNodeExecuted\"]}}': {{$json[\"execution\"][\"url\"]}}"
        },
        "additionalFields": {}
      },
      "name": "Google Chat Error Message",
      "type": "n8n-nodes-base.googleChat",
      "typeVersion": 1,
      "position": [
        20,
        980
      ],
      "credentials": {
        "googleApi": {
          "id": "8",
          "name": "Google Chat Service Account"
        }
      }
    }
  ],
  "connections": {
    "IMAP Email - SOS Item Export": {
      "main": [
        [
          {
            "node": "Start New Upload To Drive (Metadata)",
            "type": "main",
            "index": 0
          },
          {
            "node": "Merge1",
            "type": "main",
            "index": 0
          },
          {
            "node": "Set Google Drive File Id",
            "type": "main",
            "index": 0
          },
          {
            "node": "Google Chat Initial Message",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set Google Drive File Id": {
      "main": [
        [
          {
            "node": "Start Upload To Drive (Metadata)",
            "type": "main",
            "index": 0
          },
          {
            "node": "Merge",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Merge": {
      "main": [
        [
          {
            "node": "Finish Upload To Drive (File Content)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Start Upload To Drive (Metadata)": {
      "main": [
        [
          {
            "node": "Merge",
            "type": "main",
            "index": 1
          }
        ]
      ]
    },
    "Merge1": {
      "main": [
        [
          {
            "node": "Finish New Upload To Drive (File Content)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Start New Upload To Drive (Metadata)": {
      "main": [
        [
          {
            "node": "Merge1",
            "type": "main",
            "index": 1
          }
        ]
      ]
    },
    "Finish Upload To Drive (File Content)": {
      "main": [
        [
          {
            "node": "Google Chat Complete Message",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Error Trigger": {
      "main": [
        [
          {
            "node": "Google Chat Error Message",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": true,
  "settings": {
    "saveExecutionProgress": "DEFAULT",
    "errorWorkflow": "8"
  },
  "id": 8
}

Perhaps there is something specific with my environment that is causing this? Here is the docker-compose.yml file I am using:

version: "3"
services:
  n8n:
    image: n8nio/n8n
    container_name: n8n
    labels:
      com.centurylinklabs.watchtower.enable: true
    restart: always
    ports:
      - '5675:5675'
    volumes:
      - xxxxxxxxxxx:/home/node/.n8n
      - xxxxxxxxxxx:/files
    environment:
      - TZ=Canada/Eastern
      - N8N_BASIC_AUTH_ACTIVE=true
      - N8N_HOST=xxxxxxxxxxxxxxx
      - N8N_PORT=5675
      - N8N_PROTOCOL=https
      - NODE_ENV=production
      - WEBHOOK_TUNNEL_URL=xxxxxxxxxxxxxxx
      - N8N_LOG_LEVEL=debug
      # Allows importing built-in modules to function nodes
      # - NODE_FUNCTION_ALLOW_BUILTIN=*
    env_file:
      - credentials.env # This file should contain N8N_BASIC_AUTH_USER and N8N_BASIC_AUTH_PASSWORD

@Joffcom
Copy link
Member

Joffcom commented Apr 5, 2022

Hey @mark-monteiro,

I have tried to reproduce using a cut down version of your main workflow where instead of an email I have done an HTTP Get on a test CSV file. I have then set item.subject to be the first entry in your If block and it oddly enough runs with no issue.

Are you able to try the workflow below (changing the googleDriveFileId to a test one and seeing if you get the same issue? I am wondering if maybe the error is only happening with certain file formats.

{
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        500,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "const validMimeTypes = [\n  \"text/tab-separated-values\",\n  \"application/vnd.ms-excel.sheet.macroenabled.12\",\n  \"application/vnd.ms-excel\",\n  \"application/vnd.oasis.opendocument.spreadsheet\",\n  \"application/x-vnd.oasis.opendocument.spreadsheet\",\n  \"application/vnd.openxmlformats-officedocument.spreadsheetml.template\",\n  \"application/vnd.ms-excel.template.macroenabled.12\",\n  \"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet\",\n  \"text/csv\"\n];\n\nitem.subject = 'Item Export  for Senti Solutions Inc';\n\n// Validate the file mime type of the input file\nconst fileMimeType = getBinaryData().attachment_0.mimeType;\nif (validMimeTypes.indexOf(fileMimeType) === -1) {\n  throw new Error(\"Invalid mime type: \" + fileMimeType);\n}\n\n// Add a new field called 'googleDriveFileId' to the JSON of the item\nif (item.subject === \"Item Export  for Senti Solutions Inc\") {\n  item.googleDriveFileId = \"1n2mMOyd5bKOW3YYQNNs0_o9ZWNxo-ak-0XlgQ5YM91A\";\n} else if (item.subject === \"Vendor Export  for Senti Solutions Inc\") {\n  item.googleDriveFileId = \"1WwFLfoG19eyiUu9SFc6okub8C1v4ZyPbWT2u07p5er4\";\n} else if (item.subject === \"Vendor-Item Catalog Export  for Senti Solutions Inc\") {\n  item.googleDriveFileId = \"1Es1-4lAxyr1BOaWVLkoGUyVLVNtWOtF1l9OtxhtXg_0\";\n} else {\n  throw new Error(\"Unsupported email subject line:\" + item.subject);\n}\n\n// Return the modified item\nreturn item;"
      },
      "name": "Set Google Drive File Id",
      "type": "n8n-nodes-base.functionItem",
      "typeVersion": 1,
      "position": [
        860,
        300
      ]
    },
    {
      "parameters": {
        "mode": "mergeByIndex",
        "join": "inner"
      },
      "name": "Merge",
      "type": "n8n-nodes-base.merge",
      "typeVersion": 1,
      "position": [
        1360,
        320
      ]
    },
    {
      "parameters": {
        "authentication": "oAuth2",
        "requestMethod": "PATCH",
        "url": "=https://www.googleapis.com/upload/drive/v3/files/{{$json[\"googleDriveFileId\"]}}",
        "allowUnauthorizedCerts": true,
        "options": {
          "fullResponse": true
        },
        "bodyParametersUi": {
          "parameter": [
            {
              "name": "mimeType",
              "value": "application/vnd.google-apps.spreadsheet"
            }
          ]
        },
        "headerParametersUi": {
          "parameter": [
            {
              "name": "X-Upload-Content-Type",
              "value": "={{$binary.attachment_0.mimeType}}"
            }
          ]
        },
        "queryParametersUi": {
          "parameter": [
            {
              "name": "uploadType",
              "value": "resumable"
            }
          ]
        }
      },
      "name": "Start Upload To Drive (Metadata)",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 1,
      "position": [
        1100,
        420
      ],
      "credentials": {
        "oAuth2Api": {
          "id": "94",
          "name": "Google Drive"
        }
      },
      "notes": "Details on how xlsx file is converted to Google Sheet: https://stackoverflow.com/a/36957953/1988326"
    },
    {
      "parameters": {
        "authentication": "oAuth2",
        "requestMethod": "PATCH",
        "url": "={{$json[\"headers\"][\"location\"]}}",
        "jsonParameters": true,
        "options": {
          "bodyContentType": "raw"
        },
        "sendBinaryData": true,
        "binaryPropertyName": "attachment_0"
      },
      "name": "Finish Upload To Drive (File Content)",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 1,
      "position": [
        1540,
        320
      ],
      "credentials": {
        "oAuth2Api": {
          "id": "94",
          "name": "Google Drive"
        }
      },
      "notes": "Details on how xlsx file is converted to Google Sheet: https://stackoverflow.com/a/36957953/1988326"
    },
    {
      "parameters": {
        "url": "https://sample-videos.com/csv/Sample-Spreadsheet-100-rows.csv",
        "responseFormat": "file",
        "dataPropertyName": "attachment_0",
        "options": {}
      },
      "name": "HTTP Request",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 1,
      "position": [
        680,
        300
      ]
    }
  ],
  "connections": {
    "Start": {
      "main": [
        [
          {
            "node": "HTTP Request",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set Google Drive File Id": {
      "main": [
        [
          {
            "node": "Start Upload To Drive (Metadata)",
            "type": "main",
            "index": 0
          },
          {
            "node": "Merge",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Merge": {
      "main": [
        [
          {
            "node": "Finish Upload To Drive (File Content)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Start Upload To Drive (Metadata)": {
      "main": [
        [
          {
            "node": "Merge",
            "type": "main",
            "index": 1
          }
        ]
      ]
    },
    "HTTP Request": {
      "main": [
        [
          {
            "node": "Set Google Drive File Id",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

@mark-monteiro
Copy link
Author

@Joffcom That didn't seem to help :/

I'm not sure how likely it is that this error depends on the specific file being used. On my instance I'm able to reproduce this with a very simple workflow with only a single HTTP request node that has no reference to the file at all:

image

@mark-monteiro
Copy link
Author

Did a bit of digging and it looks like this same problem is solved in winston by using fast-safe-stringify instead of JSON.stringify to log objects: winstonjs/logform#35

Perhaps the same fix could be applied here?

Object.keys(metadata).length ? ` ${JSON.stringify(metadata)}` : ''

Seems like a good option, I think in general it is best practice to make sure your logging infrastructure can never throw exceptions

@Joffcom
Copy link
Member

Joffcom commented Apr 5, 2022

@mark-monteiro it is a good idea but I am still not sure what is actually causing it, I have been unable to reproduce it on a few different installs so it would be nice to work out what is causing it.

I will have another poke at it in the morning to what I can find.

@mark-monteiro
Copy link
Author

Thanks for looking into it. Errors that can't be reproduced are the worst 😩

I did some more searching into old issues and I found #2256 that appears to have the same error as me in an HTTP Request node:

Converting circular structure to JSON --> starting at object with constructor TLSSocket | property _httpMessage -> object with constructor ClientRequest --- property socket closes the circle

It looks like some other problems were resolved in that issue but this specific error was also not reproducible back then (Sept 2011).

I double checked in my execution history and it turns out the error was also present a few days before I updated to the latest version. So it doesn't look like this issue is specific to the latest release, sorry for the confusion.

@mark-monteiro
Copy link
Author

@Joffcom I was able to narrow down exactly where this error is coming from 🎉

I had a hunch that this line was causing the logger error. The error object is being logged as metadata and it contains the HTTP response object, which is likely not serializable.

What I ended up doing was going into a shell in my docker container with docker-compose exec and manually commenting out that line. Running the workflow with that line commented out prevented previous logger exception from being thrown and finally gave a reasonable error message for the node execution:

n8n    | 2022-04-07T20:21:22.220Z | debug    | Executing hook on node "Start Upload To Drive (Metadata)" (hookFunctionsPush) {"executionId":"3770","sessionId":"neku1ohvtrp","workflowId":"8","file":"WorkflowExecuteAdditionalData.js","function":"nodeExecuteAfter"}
n8n    | 2022-04-07T20:21:22.220Z | verbose  | Workflow execution finished with error {"error":{"name":"NodeApiError","cause":{"status":"rejected","reason":{}},"node":{"parameters":{"authentication":"oAuth2","requestMethod":"PATCH","url":"=https://www.googleapis.com/upload/drive/v3/files/{{$json[\"googleDriveFileId\"]}}","allowUnauthorizedCerts":true,"responseFormat":"json","jsonParameters":false,"options":{"fullResponse":true},"bodyParametersUi":{"parameter":[{"name":"mimeType","value":"application/vnd.google-apps.spreadsheet"}]},"headerParametersUi":{"parameter":[{"name":"X-Upload-Content-Type","value":"={{$binary.attachment_0.mimeType}}"}]},"queryParametersUi":{"parameter":[{"name":"uploadType","value":"resumable"}]}},"name":"Start Upload To Drive (Metadata)","type":"n8n-nodes-base.httpRequest","typeVersion":1,"position":[280,740],"credentials":{"oAuth2Api":{"id":"7","name":"Google Drive Update API"}},"notes":"Details on how xlsx file is converted to Google Sheet: https://stackoverflow.com/a/36957953/1988326"},"timestamp":1649362882218,"httpCode":"rejected","message":"UNKNOWN ERROR - check the detailed error for more information","description":null,"stack":"NodeApiError: UNKNOWN ERROR - check the detailed error for more information\n    at Object.execute (/usr/local/lib/node_modules/n8n/node_modules/n8n-nodes-base/dist/nodes/HttpRequest/HttpRequest.node.js:894:27)\n    at processTicksAndRejections (node:internal/process/task_queues:96:5)\n    at async /usr/local/lib/node_modules/n8n/node_modules/n8n-core/dist/src/WorkflowExecute.js:451:47"},"workflowId":"8","file":"WorkflowExecute.js","function":"processSuccessExecution"}

As you can see, it turns out the underlying issue was that I was getting a rejected response in the HTTP request. I refreshed the OAuth token that I was using for the request and my workflow started working again.

So I think there's two issues here:

  1. The logger shouldn't be using JSON.stringify() as I mentioned in my previous comment
  2. My OAuth token wasn't refreshed automatically, I had to open up the credential and click the 'reconnect' button for it to start working again. I was under the impression that refreshing the token should happen automatically, but perhaps I've configured something incorrectly. I can investigate this on my own and will open up a new issue if necessary

@Joffcom
Copy link
Member

Joffcom commented Apr 8, 2022

That is some good work and I should be able to reproduce that one by letting the token expire.

The problem with the credential will likely be the access type parameter not being set to offline.

I will have a play, a lot of the time the data internally is json so the stringify method should be fine so I suspect somewhere along the way there there is something not being set as a json object.

Hopefully I will have a proper soon.

@mark-monteiro
Copy link
Author

This should be fixed with #3583. If this error comes up again I'll re-open this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants