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

[#85] Fix empty object output for Error objects #87

Conversation

skgndi12
Copy link
Collaborator

@skgndi12 skgndi12 commented Dec 19, 2023

Resolves #85

Changes

  • Implement a function to convert error properties into plain objects.

Example Scenario

// Error log
logger.error('error log', { error: new Error('test') });

// CustomError log
logger.error('custom error log', {
error: new CustomError({
  code: AppErrorCode.INTERNAL_ERROR,
  cause: new Error('cause'),
  messages: ['custom error message'],
  context: { foo: 'bar' }
})
});

BEFORE

TEXT

# Error log
[2023-12-19 23:02:44][error]: error log
{
  "deployment": "dev",
  "error": {}
}

# CustomError log
[2023-12-19 23:02:45][error]: custom error log
{
  "deployment": "dev",
  "error": {
    "code": 5,
    "cause": {},
    "messages": [
      "custom error message"
    ],
    "context": {
      "foo": "bar"
    }
  }
}

JSON

// Error log
{
  "deployment": "dev",
  "error": {},
  "level": "error",
  "message": "error log",
  "timestamp": "2023-12-19 23:06:39"
}

// CustomError log
{
  "deployment": "dev",
  "error": {
    "cause": {},
    "code": 5,
    "context": {
      "foo": "bar"
    },
    "messages": [
      "custom error message"
    ]
  },
  "level": "error",
  "message": "custom error log",
  "timestamp": "2023-12-19 23:06:39"
}

AFTER

TEXT

# Error log
[2023-12-19 23:10:07][error]: error log
{
  "deployment": "dev",
  "error": {
    "message": "test",
    "stack": "Error: test\n    at main (/Users/gimseong-won/coding/Mr.C/api/src/main.ts:47:38)\n    at Object.<anonymous> (/Users/gimseong-won/coding/Mr.C/api/src/main.ts:103:3)\n    at Module._compile (node:internal/modules/cjs/loader:1254:14)\n    at Module.m._compile (/Users/gimseong-won/coding/Mr.C/api/node_modules/ts-node/src/index.ts:1618:23)\n    at Module._extensions..js (node:internal/modules/cjs/loader:1308:10)\n    at Object.require.extensions.<computed> [as .ts] (/Users/gimseong-won/coding/Mr.C/api/node_modules/ts-node/src/index.ts:1621:12)\n    at Module.load (node:internal/modules/cjs/loader:1117:32)\n    at Function.Module._load (node:internal/modules/cjs/loader:958:12)\n    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)\n    at phase4 (/Users/gimseong-won/coding/Mr.C/api/node_modules/ts-node/src/bin.ts:649:14)"
  }
}

# CustomError log
[2023-12-19 23:10:07][error]: custom error log
{
  "deployment": "dev",
  "error": {
    "stack": "Error: custom error message\n    at main (/Users/gimseong-won/coding/Mr.C/api/src/main.ts:49:12)\n    at Object.<anonymous> (/Users/gimseong-won/coding/Mr.C/api/src/main.ts:103:3)\n    at Module._compile (node:internal/modules/cjs/loader:1254:14)\n    at Module.m._compile (/Users/gimseong-won/coding/Mr.C/api/node_modules/ts-node/src/index.ts:1618:23)\n    at Module._extensions..js (node:internal/modules/cjs/loader:1308:10)\n    at Object.require.extensions.<computed> [as .ts] (/Users/gimseong-won/coding/Mr.C/api/node_modules/ts-node/src/index.ts:1621:12)\n    at Module.load (node:internal/modules/cjs/loader:1117:32)\n    at Function.Module._load (node:internal/modules/cjs/loader:958:12)\n    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)\n    at phase4 (/Users/gimseong-won/coding/Mr.C/api/node_modules/ts-node/src/bin.ts:649:14)",
    "code": 5,
    "cause": {
      "message": "cause",
      "stack": "Error: cause\n    at main (/Users/gimseong-won/coding/Mr.C/api/src/main.ts:51:14)\n    at Object.<anonymous> (/Users/gimseong-won/coding/Mr.C/api/src/main.ts:103:3)\n    at Module._compile (node:internal/modules/cjs/loader:1254:14)\n    at Module.m._compile (/Users/gimseong-won/coding/Mr.C/api/node_modules/ts-node/src/index.ts:1618:23)\n    at Module._extensions..js (node:internal/modules/cjs/loader:1308:10)\n    at Object.require.extensions.<computed> [as .ts] (/Users/gimseong-won/coding/Mr.C/api/node_modules/ts-node/src/index.ts:1621:12)\n    at Module.load (node:internal/modules/cjs/loader:1117:32)\n    at Function.Module._load (node:internal/modules/cjs/loader:958:12)\n    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)\n    at phase4 (/Users/gimseong-won/coding/Mr.C/api/node_modules/ts-node/src/bin.ts:649:14)"
    },
    "messages": [
      "custom error message"
    ],
    "context": {
      "foo": "bar"
    }
  }
}

JSON

// Error log
{
  "deployment": "dev",
  "error": {
    "message": "test",
    "stack": "Error: test\n    at main (/Users/gimseong-won/coding/Mr.C/api/src/main.ts:47:38)\n    at Object.<anonymous> (/Users/gimseong-won/coding/Mr.C/api/src/main.ts:103:3)\n    at Module._compile (node:internal/modules/cjs/loader:1254:14)\n    at Module.m._compile (/Users/gimseong-won/coding/Mr.C/api/node_modules/ts-node/src/index.ts:1618:23)\n    at Module._extensions..js (node:internal/modules/cjs/loader:1308:10)\n    at Object.require.extensions.<computed> [as .ts] (/Users/gimseong-won/coding/Mr.C/api/node_modules/ts-node/src/index.ts:1621:12)\n    at Module.load (node:internal/modules/cjs/loader:1117:32)\n    at Function.Module._load (node:internal/modules/cjs/loader:958:12)\n    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)\n    at phase4 (/Users/gimseong-won/coding/Mr.C/api/node_modules/ts-node/src/bin.ts:649:14)"
  },
  "level": "error",
  "message": "error log",
  "timestamp": "2023-12-19 23:11:59"
}

// CustomError log
{
  "deployment": "dev",
  "error": {
    "cause": {
      "message": "cause",
      "stack": "Error: cause\n    at main (/Users/gimseong-won/coding/Mr.C/api/src/main.ts:51:14)\n    at Object.<anonymous> (/Users/gimseong-won/coding/Mr.C/api/src/main.ts:103:3)\n    at Module._compile (node:internal/modules/cjs/loader:1254:14)\n    at Module.m._compile (/Users/gimseong-won/coding/Mr.C/api/node_modules/ts-node/src/index.ts:1618:23)\n    at Module._extensions..js (node:internal/modules/cjs/loader:1308:10)\n    at Object.require.extensions.<computed> [as .ts] (/Users/gimseong-won/coding/Mr.C/api/node_modules/ts-node/src/index.ts:1621:12)\n    at Module.load (node:internal/modules/cjs/loader:1117:32)\n    at Function.Module._load (node:internal/modules/cjs/loader:958:12)\n    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)\n    at phase4 (/Users/gimseong-won/coding/Mr.C/api/node_modules/ts-node/src/bin.ts:649:14)"
    },
    "code": 5,
    "context": {
      "foo": "bar"
    },
    "messages": [
      "custom error message"
    ],
    "stack": "Error: custom error message\n    at main (/Users/gimseong-won/coding/Mr.C/api/src/main.ts:49:12)\n    at Object.<anonymous> (/Users/gimseong-won/coding/Mr.C/api/src/main.ts:103:3)\n    at Module._compile (node:internal/modules/cjs/loader:1254:14)\n    at Module.m._compile (/Users/gimseong-won/coding/Mr.C/api/node_modules/ts-node/src/index.ts:1618:23)\n    at Module._extensions..js (node:internal/modules/cjs/loader:1308:10)\n    at Object.require.extensions.<computed> [as .ts] (/Users/gimseong-won/coding/Mr.C/api/node_modules/ts-node/src/index.ts:1621:12)\n    at Module.load (node:internal/modules/cjs/loader:1117:32)\n    at Function.Module._load (node:internal/modules/cjs/loader:958:12)\n    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)\n    at phase4 (/Users/gimseong-won/coding/Mr.C/api/node_modules/ts-node/src/bin.ts:649:14)"
  },
  "level": "error",
  "message": "custom error log",
  "timestamp": "2023-12-19 23:11:59"
}

@skgndi12 skgndi12 added bug Something isn't working api labels Dec 19, 2023
@skgndi12 skgndi12 self-assigned this Dec 19, 2023
@skgndi12 skgndi12 changed the title [#85] Fix logger outputs empty object for Error objects Fix: Logger outputs empty object for Error objects. Dec 19, 2023
@skgndi12 skgndi12 changed the title Fix: Logger outputs empty object for Error objects. [#85] Fix: Logger outputs empty object for Error objects Dec 19, 2023
@skgndi12 skgndi12 changed the title [#85] Fix: Logger outputs empty object for Error objects [#85] Fix empty object output for Error objects Dec 19, 2023
Copy link
Collaborator

@isutare412 isutare412 left a comment

Choose a reason for hiding this comment

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

고생하셨습니다 👍

@skgndi12 skgndi12 merged commit 0b5b756 into MovieReviewComment:develop Dec 20, 2023
1 check passed
@skgndi12 skgndi12 deleted the fix/issue-85/logger-outputs-empty-object-for-error-object branch December 20, 2023 06:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api bug Something isn't working
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

Logger outputs empty object for error object
2 participants