{"version":3,"file":"logging-error-handler.mjs","names":[],"sources":["../../src/common/logging-error-handler.ts"],"sourcesContent":["/*\n * Copyright The OpenTelemetry Authors\n * SPDX-License-Identifier: Apache-2.0\n */\n\nimport type { Exception } from '@opentelemetry/api';\nimport { diag } from '@opentelemetry/api';\nimport type { ErrorHandler } from './types';\n\n/**\n * Returns a function that logs an error using the provided logger, or a\n * console logger if one was not provided.\n */\nexport function loggingErrorHandler(): ErrorHandler {\n  return (ex: Exception) => {\n    diag.error(stringifyException(ex));\n  };\n}\n\n/**\n * Converts an exception into a string representation\n * @param {Exception} ex\n */\nfunction stringifyException(ex: Exception | string): string {\n  if (typeof ex === 'string') {\n    return ex;\n  } else {\n    return JSON.stringify(flattenException(ex));\n  }\n}\n\n/**\n * Flattens an exception into key-value pairs by traversing the prototype chain\n * and coercing values to strings. Duplicate properties will not be overwritten;\n * the first insert wins.\n */\nfunction flattenException(ex: Exception): Record<string, string> {\n  const result = {} as Record<string, string>;\n  let current = ex;\n\n  while (current !== null) {\n    Object.getOwnPropertyNames(current).forEach(propertyName => {\n      if (result[propertyName]) return;\n      const value = current[propertyName as keyof typeof current];\n      if (value) {\n        result[propertyName] = String(value);\n      }\n    });\n    current = Object.getPrototypeOf(current);\n  }\n\n  return result;\n}\n"],"mappings":";;;;;;AAaA,SAAgB,sBAAoC;CAClD,QAAQ,OAAkB;EACxB,KAAK,MAAM,mBAAmB,EAAE,CAAC;CACnC;AACF;;;;;AAMA,SAAS,mBAAmB,IAAgC;CAC1D,IAAI,OAAO,OAAO,UAChB,OAAO;MAEP,OAAO,KAAK,UAAU,iBAAiB,EAAE,CAAC;AAE9C;;;;;;AAOA,SAAS,iBAAiB,IAAuC;CAC/D,MAAM,SAAS,CAAC;CAChB,IAAI,UAAU;CAEd,OAAO,YAAY,MAAM;EACvB,OAAO,oBAAoB,OAAO,CAAC,CAAC,SAAQ,iBAAgB;GAC1D,IAAI,OAAO,eAAe;GAC1B,MAAM,QAAQ,QAAQ;GACtB,IAAI,OACF,OAAO,gBAAgB,OAAO,KAAK;EAEvC,CAAC;EACD,UAAU,OAAO,eAAe,OAAO;CACzC;CAEA,OAAO;AACT"}