normalize-string-error.ts 582 B

12345678910111213
  1. /**
  2. * V8 can throw the bare string "out of memory" (instead of an Error) from
  3. * buffer-allocation paths such as `new Uint8Array(N)` and
  4. * `Response.prototype.arrayBuffer()`. Wrap such string errors in an Error so
  5. * that downstream consumers (e.g. OError.tag, Sentry) which assume an Error
  6. * object continue to work.
  7. *
  8. * Non-string values are returned unchanged, so genuine programming bugs that
  9. * `throw null`/`throw 42`/etc. still surface unmasked.
  10. */
  11. export function normalizeStringError(err: unknown): unknown {
  12. return typeof err === 'string' ? new Error(err) : err
  13. }