Skip to content

Conversation

@mifi
Copy link
Collaborator

@mifi mifi commented Feb 26, 2025

This was a tricky one. The problem is a combination of

  1. the fact that got includes a lot of metadata properties on their errors, like request, response, timings etc,
  2. when vitest logs an error, they take all of the properties on the error object and deeply serialize it, and there's no easy way to disable this behavior.

Vitest issues:

Example:

Caused by: HTTPError: Response code 429 (Too Many Requests)
 ❯ Request.<anonymous> node_modules/got/dist/source/as-promise/index.js:118:42

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯
Serialized Error: { code: 'ERR_NON_2XX_3XX_RESPONSE', request: { _readableState: { state: 322582, highWaterMark: +0, buffer: { head: null, tail: null, length: +0, constructor: 'Function<BufferList>', push: 'Function<push>', unshift: 'Function<unshift>', shift: 'Function<shift>', clear: 'Function<clear>', join: 'Function<join>', concat: 'Function<concat>', consume: 'Function<consume>', first: 'Function<first>', _getString: 'Function<_getString>', _getBuffer: 'Function<_getBuffer>' }, length: +0, pipes: [], flowing: null, errored: { stack: 'HTTPError: Response code 429 (Too Many Requests)\n    at Request.<anonymous> (/home/runner/work/node-sdk/node-sdk/node_modules/got/dist/source/as-promise/index.js:118:42)\n    at processTicksAndRejections (node:internal/process/task_queues:95:5)', message: 'Response code 429 (Too Many Requests)', name: 'Caused by: HTTPError', code: 'ERR_NON_2XX_3XX_RESPONSE', request: [Circular], response: { _readableState: { state: 325910, highWaterMark: 16384, buffer: { head: null, tail: null, length: +0, constructor: 'Function<BufferList>', push: 'Function<push>', unshift: 'Function<unshift>', shift: 'Function<shift>', clear: 'Function<clear>', join: 'Function<join>', concat: 'Function<concat>', consume: 'Function<consume>', first: 'Function<first>', _getString: 'Function<_getString>', _getBuffer: 'Function<_getBuffer>' }, length: +0, pipes: [], flowing: false, errored: null, defaultEncoding: 'utf8', awaitDrainWriters: null, decoder: null, encoding: null, constructor: 'Function<ReadableState>', objectMode: false, ended: true, endEmitted: true, reading: false, constructed: true, sync: false, needReadable: false, emittedReadable: false, readableListening: true, resumeScheduled: false, errorEmitted: false, emitClose: true, autoDestroy: true, destroyed: true, closed: true, closeEmitted: true, multiAwaitDrain: false, readingMore: false, dataEmitted: true, pipesCount: +0, paused: true }, _events: { end: 'Function<responseOnEnd>', aborted: [ 'Function<bound onceWrapper>', 'Function<bound onceWrapper>' ], error: 'Function<bound onceWrapper>', readable: 'Function<anonymous>' }, _eventsCount: 4, _maxListeners: undefined, socket: { _tlsOptions: { allowHalfOpen: undefined, pipe: false, secureContext: { context: { init: 'Function<init>', setKey: 'Function<setKey>', setCert: 'Function<setCert>', addCACert: 'Function<addCACert>', addCRL: 'Function<addCRL>', addRootCerts: 'Function<addRootCerts>', setCipherSuites: 'Function<setCipherSuites>', setCiphers: 'Function<setCiphers>', setSigalgs: 'Function<setSigalgs>', setECDHCurve: 'Function<setECDHCurve>', setDHParam: 'Function<setDHParam>', setMaxProto: 'Function<setMaxProto>', setMinProto: 'Function<setMinProto>', getMaxProto: 'Function<getMaxProto>', getMinProto: 'Function<getMinProto>', setOptions: 'Function<setOptions>', setSessionIdContext: 'Function<setSessionIdContext>', setSessionTimeout: 'Function<setSessionTimeout>', close: 'Function<close>', loadPKCS12: 'Function<loadPKCS12>', setTicketKeys: 'Function<setTicketKeys>', enableTicketKeyCallback: 'Function<enableTicketKeyCallback>', getTicketKeys: 'Function<getTicketKeys>', getCertificate: 'Function<getCertificate>', getIssuer: 'Function<getIssuer>', setEngineKey: 'Function<setEngineKey>', setClientCertEngine: 'Function<setClientCertEngine>', _external: {}, constructor: 'Function<SecureContext>' }, constructor: 'Function<SecureContext>' }, isServer: false, requestCert: true, rejectUnauthorized: true, session: { type: 'Buffer', data: [ 48, 130, 5, 203, 2, 1, 1, 2, 2, 3, 4, 4, 2, 19, 2,

...and hundreds of more kB

Because imo this is mostly a problem with vitest, and from the previous 2 issues it doesn't seem like the vitest team wants to change this behaviour, the workaround is to wrap the Transloadit object (when running tests) in a Proxy that will catch-rethrow all errors with a toJSON on all the properties that we don't want to expose in the vitest error serialization. this makes sure Transloadit error API doesn't change, and people can still access properties like err.response.body.

I think in a future major of our SDK we should probably make our own opaque error abstraction that wraps all got (http request) specific errors, instead of passing them through (which we do now). This will also probably avoid this vitest problem, unless we decide to still expose stuff like HTTP response bodies in our errors.

kind of related: sindresorhus/got#1126

@codecov-commenter
Copy link

codecov-commenter commented Feb 26, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 68.31%. Comparing base (2db3023) to head (fa9dc95).

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #216   +/-   ##
=======================================
  Coverage   68.31%   68.31%           
=======================================
  Files           6        6           
  Lines         587      587           
  Branches      113      113           
=======================================
  Hits          401      401           
  Misses        186      186           
Flag Coverage Δ
unittests 68.31% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@mifi mifi requested a review from remcohaszing February 26, 2025 04:03
@mifi mifi requested a review from remcohaszing March 16, 2025 05:23
@mifi mifi merged commit 3a6cc9e into main Mar 17, 2025
9 checks passed
@mifi mifi deleted the fix-monster-error branch March 17, 2025 05:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants