error.js 1.1 KB

12345678910111213141516171819202122232425262728
  1. /** takes an error object and serializes it to a plan object */
  2. function serializeError (input) {
  3. if (!(input instanceof Error)) {
  4. if (typeof input === 'string') {
  5. const error = new Error(`attempted to serialize a non-error, string String, "${input}"`)
  6. return serializeError(error)
  7. }
  8. const error = new Error(`attempted to serialize a non-error, ${typeof input} ${input?.constructor?.name}`)
  9. return serializeError(error)
  10. }
  11. // different error objects store status code differently
  12. // AxiosError uses `status`, other services use `statusCode`
  13. const statusCode = input.statusCode ?? input.status
  14. // CAUTION: what we serialize here gets add to the size of logs
  15. return {
  16. errorType: input.errorType ?? input.constructor.name,
  17. ...(input.message ? { message: input.message } : {}),
  18. ...(input.stack ? { stack: input.stack } : {}),
  19. // think of this as error code
  20. ...(input.code ? { code: input.code } : {}),
  21. // think of this as http status code
  22. ...(statusCode ? { statusCode } : {}),
  23. }
  24. }
  25. module.exports = {
  26. serializeError,
  27. }