5
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【JavaScript】ErrorをJSON.stringifyする方法

Last updated at Posted at 2024-03-14

以下のように記述することでErrorをJSON.stringifyできます。

    const error = new Error('this is sample error')
    
    console.log(JSON.stringify(error)) // このように記述すると{}となる
    
    console.log(JSON.stringify(error, Object.getOwnPropertyNames(error)))
    // {"stack":"Error: this is sample error\n    at handleFetchError (/home/node/.next/server/chunks/745.js:227:23)\n    at /home/node/.next/server/chunks/102.js:265:62\n    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)\n    at async UserPage (/home/node/.next/server/chunks/102.js:287:20)","message":"this is sample error"}

5
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?