Skip to content

Implement safeStringify for circular reference handling#425

Open
MK-2001 wants to merge 4 commits intowindkh:masterfrom
MK-2001:patch-2
Open

Implement safeStringify for circular reference handling#425
MK-2001 wants to merge 4 commits intowindkh:masterfrom
MK-2001:patch-2

Conversation

@MK-2001
Copy link
Contributor

@MK-2001 MK-2001 commented Jan 27, 2026

Situation:
My flow aborted without information when the message was malformed with an editMessageText type.

Unhandled rejection TypeError: Converting circular structure to JSON
    --> starting at object with constructor 'Socket'
    |     property 'parser' -> object with constructor 'HTTPParser'
    --- property 'socket' closes the circle
    at JSON.stringify (<anonymous>)
    at TelegramOutNode.processError (/data/node_modules/node-red-contrib-telegrambot/telegrambot/nodes/out-node.js:86:128)
    at /data/node_modules/node-red-contrib-telegrambot/telegrambot/nodes/out-node.js:579:46
    at tryCatcher (/data/node_modules/bluebird/js/release/util.js:16:23)
    at Promise._settlePromiseFromHandler (/data/node_modules/bluebird/js/release/promise.js:547:31)
    at Promise._settlePromise (/data/node_modules/bluebird/js/release/promise.js:604:18)
    at Promise._settlePromise0 (/data/node_modules/bluebird/js/release/promise.js:649:10)
    at Promise._settlePromises (/data/node_modules/bluebird/js/release/promise.js:725:18)
    at _drainQueueStep (/data/node_modules/bluebird/js/release/async.js:93:12)
    at _drainQueue (/data/node_modules/bluebird/js/release/async.js:86:9)
    at Async._drainQueues (/data/node_modules/bluebird/js/release/async.js:102:5)
    at Async.drainQueues (/data/node_modules/bluebird/js/release/async.js:15:14)
    at process.processImmediate (node:internal/timers:485:21)

Added a safeStringify method to handle circular references in JSON.

Copy link
Owner

@windkh windkh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, duplicated keys are removed. How is it even possible to have duplicated ones?
The object that is shown in the error does not contain all info. What if the removed key caused the problem?
Wouldnt it be better to return the raw message with a hint that the string is not a valid json?

@windkh
Copy link
Owner

windkh commented Jan 28, 2026

Hi, duplicated keys are removed. How is it even possible to have duplicated ones? The object that is shown in the error does not contain all info. What if the removed key caused the problem? Wouldnt it be better to return the raw message with a hint that the string is not a valid json?

@MK-2001 what do you think?

@MK-2001
Copy link
Contributor Author

MK-2001 commented Jan 28, 2026

It is not a directly a sublimated one, it mir a cut of a circulating structure in an object. If the same object gets identified twice, what is the origin of the current problem, it will crash the application.
Therefore I remove the second.

@windkh
Copy link
Owner

windkh commented Jan 28, 2026

If so the root cause of the error should be pointed out clearly in the resulting object. Otherwise somebody will have problems when finding out what was going on.
Do you have a sample object for testing?

@windkh
Copy link
Owner

windkh commented Feb 15, 2026

@MK-2001 Hi Marcus, I just did some investigation about how to handle this correctly
https://stackoverflow.com/questions/4816099/chrome-sendrequest-error-typeerror-converting-circular-structure-to-json

Instead of discarding the key somebody in the thread above replaced the reference with a string point out that this is a reference e.g. "circular reference"

What do you think about that?

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

Successfully merging this pull request may close these issues.

2 participants

Comments