normalize-string-error.test.ts 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import { expect } from 'chai'
  2. import OError from '@overleaf/o-error'
  3. import { normalizeStringError } from '@/features/pdf-preview/util/normalize-string-error'
  4. describe('normalizeStringError', function () {
  5. it('wraps a string in an Error so OError.tag/Sentry can consume it', function () {
  6. // V8 throws the bare string "out of memory" (instead of an Error) from
  7. // some buffer-allocation paths (e.g. `new Uint8Array(N)`,
  8. // `Response.prototype.arrayBuffer()`).
  9. const result = normalizeStringError('out of memory')
  10. expect(result).to.be.an.instanceOf(Error)
  11. expect((result as Error).message).to.equal('out of memory')
  12. expect((result as Error).stack).to.be.a('string')
  13. })
  14. it('returns an Error instance unchanged', function () {
  15. const original = new Error('boom')
  16. expect(normalizeStringError(original)).to.equal(original)
  17. })
  18. it('returns a custom Error subclass unchanged', function () {
  19. class CustomError extends Error {}
  20. const original = new CustomError('boom')
  21. expect(normalizeStringError(original)).to.equal(original)
  22. })
  23. it('returns non-string non-Error values unchanged so genuine bugs surface', function () {
  24. // The helper deliberately does not wrap `null`/`undefined`/numbers/etc.,
  25. // so code paths that `throw null` or `throw 42` continue to surface as
  26. // bugs rather than being masked.
  27. expect(normalizeStringError(null)).to.equal(null)
  28. expect(normalizeStringError(undefined)).to.equal(undefined)
  29. expect(normalizeStringError(42)).to.equal(42)
  30. const obj = { foo: 'bar' }
  31. expect(normalizeStringError(obj)).to.equal(obj)
  32. })
  33. it('produces an Error that OError.tag can attach metadata to', function () {
  34. // Round-trip the realistic usage: tag a normalised string error with
  35. // some info, and verify both the tag and the info make it through.
  36. const err = OError.tag(
  37. normalizeStringError('out of memory'),
  38. 'fallback request failed',
  39. { url: '/project/abc/output.pdf', start: 0, end: 1024 }
  40. )
  41. expect(err).to.be.an.instanceOf(Error)
  42. expect((err as Error).message).to.equal('out of memory')
  43. expect(OError.getFullInfo(err)).to.deep.include({
  44. url: '/project/abc/output.pdf',
  45. start: 0,
  46. end: 1024,
  47. })
  48. })
  49. })