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

Bug: Logging rror causes "Converting circular structure to JSON" #74

Closed
iknowcss opened this issue Dec 15, 2020 · 4 comments
Closed

Bug: Logging rror causes "Converting circular structure to JSON" #74

iknowcss opened this issue Dec 15, 2020 · 4 comments
Labels
bug Something isn't working

Comments

@iknowcss
Copy link

Describe the bug
When I pass an error object which has a circular structure to logger.info or any other logging method, I get the following error:

TypeError: Converting circular structure to JSON

To Reproduce
Execute the following code:

import { Logger } from 'tslog';

const logger = new Logger();

const request = { url: 'http://example.com/does-not-exist', originalRequest: {} };
request.originalRequest = request;

const error = new Error('Failed to fetch the resource');
error.request = request;

logger.warn('The request failed', error);

To see this happening in a more realistic context, install got and run the following code:

import { Logger } from 'tslog';
import got from 'got';

const logger = new Logger();

async function getResponse() {
  try {
    const response = await got.get('http://example.com/does-not-exist');
    return response;
  } catch (error) {
    logger.warn('Failed to fetch the data', error);  
  }
};

getResponse();

Expected behavior
tslog stringifies the error object in such a way that it does not throw an error.

Screenshots
Output from the 2nd example above:

(node:15937) UnhandledPromiseRejectionWarning: TypeError: Converting circular structure to JSON
    --> starting at object with constructor 'Socket'
    |     property '_httpMessage' -> object with constructor 'ClientRequest'
    --- property 'socket' closes the circle
    at JSON.stringify (<anonymous>)
    at Logger._buildErrorObject (/tmp/tmp.5tNebHIiKR/node_modules/tslog/src/index.ts:466:55)
    at /tmp/tmp.5tNebHIiKR/node_modules/tslog/src/index.ts:432:16
    at Array.forEach (<anonymous>)
    at Logger._buildLogObject (/tmp/tmp.5tNebHIiKR/node_modules/tslog/src/index.ts:429:28)
    at Logger._handleLog (/tmp/tmp.5tNebHIiKR/node_modules/tslog/src/index.ts:353:40)
    at Logger.warn (/tmp/tmp.5tNebHIiKR/node_modules/tslog/src/index.ts:298:28)
    at getResponse (/tmp/tmp.5tNebHIiKR/bug.ts:11:12)
    at processTicksAndRejections (internal/process/task_queues.js:97:5)

Additional context
N/a

Node.js Version
v12.18.2

OS incl. Version
Linux Mint 19.1

@iknowcss iknowcss added the bug Something isn't working label Dec 15, 2020
@terehov
Copy link
Contributor

terehov commented Dec 15, 2020

Thank you very much. Apparently, Error objects were treated differently, than other JS objects. I have solved this issue and plan to release a new version today.

@terehov
Copy link
Contributor

terehov commented Dec 15, 2020

@iknowcss
Copy link
Author

Thanks very much for the quick turn-around! I really enjoy using tslog in my current project :)

@terehov
Copy link
Contributor

terehov commented Dec 15, 2020

Thank you, I am happy to hear that :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants