From 785e564766abf4079be58923f33f2852ce4ac56a Mon Sep 17 00:00:00 2001 From: Mikael Finstad Date: Wed, 26 Feb 2025 12:37:48 +0900 Subject: [PATCH 1/4] Fix monster error --- test/integration/live-api.test.ts | 3 ++- test/unit/mock-http.test.ts | 29 +++++++++++++++------- test/util.ts | 40 +++++++++++++++++++++++++++++++ 3 files changed, 63 insertions(+), 9 deletions(-) create mode 100644 test/util.ts diff --git a/test/integration/live-api.test.ts b/test/integration/live-api.test.ts index 40ff690c..bea43a5f 100644 --- a/test/integration/live-api.test.ts +++ b/test/integration/live-api.test.ts @@ -12,6 +12,7 @@ import debug = require('debug') import { CreateAssemblyOptions, Transloadit, UploadProgress } from '../../src/Transloadit' import { createTestServer, TestServer } from '../testserver' +import { createProxy } from '../util' const log = debug('transloadit:live-api') @@ -54,7 +55,7 @@ function createClient(opts = {}) { ], } - return new Transloadit({ authKey, authSecret, gotRetry, ...opts }) + return createProxy(new Transloadit({ authKey, authSecret, gotRetry, ...opts })) } function createAssembly(client: Transloadit, params: CreateAssemblyOptions) { diff --git a/test/unit/mock-http.test.ts b/test/unit/mock-http.test.ts index be640cce..6c40fbf0 100644 --- a/test/unit/mock-http.test.ts +++ b/test/unit/mock-http.test.ts @@ -3,14 +3,14 @@ import { inspect } from 'node:util' import { ApiError, - HTTPError, InconsistentResponseError, TimeoutError, Transloadit, } from '../../src/Transloadit' +import { createProxy } from '../util' const getLocalClient = (opts?: Omit) => - new Transloadit({ authKey: '', authSecret: '', endpoint: 'http://localhost', ...opts }) + createProxy(new Transloadit({ authKey: '', authSecret: '', endpoint: 'http://localhost', ...opts })) const createAssemblyRegex = /\/assemblies\/[0-9a-f]{32}/ @@ -21,11 +21,7 @@ describe('Mocked API tests', () => { }) it('should time out createAssembly with a custom timeout', async () => { - const client = new Transloadit({ - authKey: '', - authSecret: '', - endpoint: 'http://localhost', - }) + const client = getLocalClient() nock('http://localhost').post(createAssemblyRegex).delay(100).reply(200) @@ -135,13 +131,13 @@ describe('Mocked API tests', () => { expect.stringMatching( ` at createAssemblyAndUpload \\(.+\\/src\\/Transloadit\\.ts:\\d+:\\d+\\)` ), + expect.stringMatching(` at .+\\/test\\/util\\.ts:\\d+:\\d+`), expect.stringMatching(` at .+\\/test\\/unit\\/mock-http\\.test\\.ts:\\d+:\\d+`), expect.stringMatching(` at .+`), expect.stringMatching(` at .+`), expect.stringMatching(` at .+`), expect.stringMatching(` at .+`), expect.stringMatching(` at .+`), - expect.stringMatching(` at .+`), expect.stringMatching(` rawMessage: 'Invalid file metadata',`), expect.stringMatching(` assemblyId: '123',`), expect.stringMatching( @@ -327,4 +323,21 @@ describe('Mocked API tests', () => { await expect(promise).rejects.toThrow(ApiError) scope.done() }) + + it.skip('should not log monster error stack traces in vitest', async () => { + const client = getLocalClient() + + const scope = nock('http://localhost') + .get('/assemblies/invalid') + .query(() => true) + .reply(404, { error: 'SERVER_404', message: 'not found' }) + + try { + await client.getAssembly('invalid') + // NOTE: manually check output from vitest + // Check that it doesn't print a huge blob of JSON + } finally { + scope.done() + } + }) }) diff --git a/test/util.ts b/test/util.ts new file mode 100644 index 00000000..b5106f47 --- /dev/null +++ b/test/util.ts @@ -0,0 +1,40 @@ +import { HTTPError, Transloadit } from '../src/Transloadit'; + +export const createProxy = (transloaditInstance: Transloadit) => { + return new Proxy(transloaditInstance, { + get(target, propKey) { + // @ts-expect-error I dunno how to type + const origMethod = target[propKey]; + if (typeof origMethod === 'function') { + return async function (...args: any) { + try { + return await origMethod.apply(target, args); + } catch (err) { + if (err instanceof Error && 'cause' in err && err.cause instanceof HTTPError) { + if (err.cause.request) { + Object.defineProperty(err.cause.request, 'toJSON', { + value: () => undefined, + enumerable: false, + }) + Object.defineProperty(err.cause.response, 'toJSON', { + value: () => undefined, + enumerable: false, + }) + Object.defineProperty(err.cause.options, 'toJSON', { + value: () => undefined, + enumerable: false, + }) + Object.defineProperty(err.cause.timings, 'toJSON', { + value: () => undefined, + enumerable: false, + }) + } + } + throw err + } + }; + } + return origMethod; + } + }); +}; From dcc8a637d9f20b353d2b3d8e18103ac59b8e8061 Mon Sep 17 00:00:00 2001 From: Mikael Finstad Date: Wed, 26 Feb 2025 12:50:35 +0900 Subject: [PATCH 2/4] format --- test/unit/mock-http.test.ts | 4 +++- test/util.ts | 16 ++++++++-------- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/test/unit/mock-http.test.ts b/test/unit/mock-http.test.ts index 6c40fbf0..4b011e2e 100644 --- a/test/unit/mock-http.test.ts +++ b/test/unit/mock-http.test.ts @@ -10,7 +10,9 @@ import { import { createProxy } from '../util' const getLocalClient = (opts?: Omit) => - createProxy(new Transloadit({ authKey: '', authSecret: '', endpoint: 'http://localhost', ...opts })) + createProxy( + new Transloadit({ authKey: '', authSecret: '', endpoint: 'http://localhost', ...opts }) + ) const createAssemblyRegex = /\/assemblies\/[0-9a-f]{32}/ diff --git a/test/util.ts b/test/util.ts index b5106f47..dfeeea43 100644 --- a/test/util.ts +++ b/test/util.ts @@ -1,14 +1,14 @@ -import { HTTPError, Transloadit } from '../src/Transloadit'; +import { HTTPError, Transloadit } from '../src/Transloadit' export const createProxy = (transloaditInstance: Transloadit) => { return new Proxy(transloaditInstance, { get(target, propKey) { // @ts-expect-error I dunno how to type - const origMethod = target[propKey]; + const origMethod = target[propKey] if (typeof origMethod === 'function') { return async function (...args: any) { try { - return await origMethod.apply(target, args); + return await origMethod.apply(target, args) } catch (err) { if (err instanceof Error && 'cause' in err && err.cause instanceof HTTPError) { if (err.cause.request) { @@ -32,9 +32,9 @@ export const createProxy = (transloaditInstance: Transloadit) => { } throw err } - }; + } } - return origMethod; - } - }); -}; + return origMethod + }, + }) +} From 86cd0462a25481e93a5a3f086b074c641974fb50 Mon Sep 17 00:00:00 2001 From: Mikael Finstad Date: Sun, 16 Mar 2025 13:20:38 +0800 Subject: [PATCH 3/4] don't assume promise https://github.com/transloadit/node-sdk/pull/216/files#r1971499443 --- test/unit/mock-http.test.ts | 2 +- test/util.ts | 16 +++++++++++----- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/test/unit/mock-http.test.ts b/test/unit/mock-http.test.ts index 4dcfa097..628ded66 100644 --- a/test/unit/mock-http.test.ts +++ b/test/unit/mock-http.test.ts @@ -133,13 +133,13 @@ describe('Mocked API tests', () => { expect.stringMatching( ` at createAssemblyAndUpload \\(.+\\/src\\/Transloadit\\.ts:\\d+:\\d+\\)` ), - expect.stringMatching(` at .+\\/test\\/util\\.ts:\\d+:\\d+`), expect.stringMatching(` at .+\\/test\\/unit\\/mock-http\\.test\\.ts:\\d+:\\d+`), expect.stringMatching(` at .+`), expect.stringMatching(` at .+`), expect.stringMatching(` at .+`), expect.stringMatching(` at .+`), expect.stringMatching(` at .+`), + expect.stringMatching(` at .+`), expect.stringMatching(` rawMessage: 'Invalid file metadata',`), expect.stringMatching(` assemblyId: '123',`), expect.stringMatching( diff --git a/test/util.ts b/test/util.ts index dfeeea43..82a81db2 100644 --- a/test/util.ts +++ b/test/util.ts @@ -6,10 +6,15 @@ export const createProxy = (transloaditInstance: Transloadit) => { // @ts-expect-error I dunno how to type const origMethod = target[propKey] if (typeof origMethod === 'function') { - return async function (...args: any) { - try { - return await origMethod.apply(target, args) - } catch (err) { + return function (...args: any) { + const result = origMethod.apply(target, args) + + if (!(result && 'then' in result)) { + return result + } + + // @ts-expect-error any + return result.catch((err) => { if (err instanceof Error && 'cause' in err && err.cause instanceof HTTPError) { if (err.cause.request) { Object.defineProperty(err.cause.request, 'toJSON', { @@ -31,9 +36,10 @@ export const createProxy = (transloaditInstance: Transloadit) => { } } throw err - } + }) } } + return origMethod }, }) From fa9dc954e53fafd3f1126c2175a391a440bc0d1f Mon Sep 17 00:00:00 2001 From: Mikael Finstad Date: Sun, 16 Mar 2025 13:20:54 +0800 Subject: [PATCH 4/4] fix lint --- test/util.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/util.ts b/test/util.ts index 82a81db2..dc17525f 100644 --- a/test/util.ts +++ b/test/util.ts @@ -12,7 +12,7 @@ export const createProxy = (transloaditInstance: Transloadit) => { if (!(result && 'then' in result)) { return result } - + // @ts-expect-error any return result.catch((err) => { if (err instanceof Error && 'cause' in err && err.cause instanceof HTTPError) {