diff --git a/README.md b/README.md index e8674e38..8d6ae2e2 100644 --- a/README.md +++ b/README.md @@ -418,6 +418,7 @@ Any errors originating from Node.js will be passed on and we use [GOT](https://g - `code` (`string`) - [The Transloadit API error code](https://transloadit.com/docs/api/response-codes/#error-codes). - `rawMessage` (`string`) - A textual representation of the Transloadit API error. +- `reason` (`string`) - Additional information about the Transloadit API error. - `assemblyId`: (`string`) - If the request is related to an assembly, this will be the ID of the assembly. - `assemblySslUrl` (`string`) - If the request is related to an assembly, this will be the SSL URL to the assembly . diff --git a/src/ApiError.ts b/src/ApiError.ts index 6ec4fe6d..03f5b185 100644 --- a/src/ApiError.ts +++ b/src/ApiError.ts @@ -3,6 +3,7 @@ import { HTTPError, RequestError } from 'got' export interface TransloaditErrorResponseBody { error?: string message?: string + reason?: string assembly_ssl_url?: string assembly_id?: string } @@ -16,6 +17,8 @@ export class ApiError extends Error { rawMessage?: string + reason?: string + assemblySslUrl?: string assemblyId?: string @@ -36,6 +39,7 @@ export class ApiError extends Error { super(message) this.rawMessage = body.message + this.reason = body.reason this.assemblyId = body.assembly_id this.assemblySslUrl = body.assembly_ssl_url this.code = body.error diff --git a/test/unit/mock-http.test.ts b/test/unit/mock-http.test.ts index 9fedb3f9..0711127b 100644 --- a/test/unit/mock-http.test.ts +++ b/test/unit/mock-http.test.ts @@ -115,15 +115,18 @@ describe('Mocked API tests', () => { it('should throw error with error code', async () => { const client = getLocalClient() - nock('http://localhost') - .post(createAssemblyRegex) - .reply(400, { error: 'INVALID_FILE_META_DATA', message: 'Invalid file metadata' }) + nock('http://localhost').post(createAssemblyRegex).reply(400, { + error: 'INVALID_FILE_META_DATA', + message: 'Invalid file metadata', + reason: 'Some reason', + }) await expect(client.createAssembly()).rejects.toThrow( expect.objectContaining({ name: 'ApiError', code: 'INVALID_FILE_META_DATA', rawMessage: 'Invalid file metadata', + reason: 'Some reason', message: 'API error (HTTP 400) INVALID_FILE_META_DATA: Invalid file metadata', }) ) @@ -165,6 +168,7 @@ describe('Mocked API tests', () => { expect.stringMatching(` at .+`), expect.stringMatching(` code: 'INVALID_FILE_META_DATA',`), expect.stringMatching(` rawMessage: 'Invalid file metadata',`), + expect.stringMatching(` reason: undefined,`), expect.stringMatching( ` assemblySslUrl: 'https:\\/\\/api2-oltu\\.transloadit\\.com\\/assemblies\\/foo'` ),