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

TypeError: Converting circular structure to JSON #1535

Open
Alireza-Hallajian opened this issue Oct 2, 2023 · 2 comments
Open

TypeError: Converting circular structure to JSON #1535

Alireza-Hallajian opened this issue Oct 2, 2023 · 2 comments

Comments

@Alireza-Hallajian
Copy link

I get the "Converting circular structure to JSON" error in my project, and I know it happens in one of my "res.send"s. But it doesn't happen every time the API is called, as I sometimes see it in my logs. The server is in production mode and there is no report about it from users.

Environment

  • Inversify Version: 6.0.1
  • Node.js 18.16.1
  • Operating System and version: Ubuntu 22.04.2 LTS

Stack trace

TypeError: Converting circular structure to JSON
--> starting at object with constructor 'ServerResponse'
| property 'socket' -> object with constructor 'Socket'
--- property '_httpMessage' closes the circle
at JSON.stringify ()
at stringify (/home/lineage/workspace/posterity-refactored-backend/node_modules/express/lib/response.js:1150:12)
at ServerResponse.json (/home/lineage/workspace/posterity-refactored-backend/node_modules/express/lib/response.js:271:14)
at ServerResponse.send (/home/lineage/workspace/posterity-refactored-backend/node_modules/express/lib/response.js:162:21)
at InversifyExpressServer. (/home/lineage/workspace/posterity-refactored-backend/node_modules/inversify-express-utils/src/server.ts:273:25)
at step (/home/lineage/workspace/posterity-refactored-backend/node_modules/inversify-express-utils/lib/server.js:33:23)
at Object.next (/home/lineage/workspace/posterity-refactored-backend/node_modules/inversify-express-utils/lib/server.js:14:53)
at fulfilled (/home/lineage/workspace/posterity-refactored-backend/node_modules/inversify-express-utils/lib/server.js:5:58)
at processTicksAndRejections (node:internal/process/task_queues:95:5)

@ingluis-code
Copy link

I also have the same problem which does not happen every time and it does not interrupt the operation but it is very annoying.

@SzymonKaczorowski-Kainos
Copy link

SzymonKaczorowski-Kainos commented Nov 25, 2024

I faced the same issue.

Based on my observation, the problem arises when the server's response is sent after connection is dropped and return res.sendStatus() syntax is used. In my case, I was able to resolve the issue by modifying the code as follows.

I changed this:

 public async healthCheck(req: Request, res: Response) {
    try {
      const results = await Promise.allSettled([
        checkDatabases(),
        [...]
      ]);
      [...]
      return res.sendStatus(200);
    } catch (error) {
      return res.sendStatus(500);
    }
  }

to this (removed return statement)

 public async healthCheck(req: Request, res: Response) {
    try {
      const results = await Promise.allSettled([
        checkDatabases(),
        [...]
      ]);
      [...]
       res.sendStatus(200);
    } catch (error) {
       res.sendStatus(500);
    }
  }

After this, I was no longer able to reproduce the issue. Not sure if this is expected behaviour.

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

No branches or pull requests

3 participants