diff --git a/packages/sdks/typescript/package.json b/packages/sdks/typescript/package.json index c1c59830f..410f3d5ad 100644 --- a/packages/sdks/typescript/package.json +++ b/packages/sdks/typescript/package.json @@ -1,6 +1,6 @@ { "name": "@latitude-data/sdk", - "version": "1.0.0-beta.3", + "version": "1.0.0-beta.4", "description": "Latitude SDK for Typescript", "author": "Latitude Data SL ", "license": "LGPL-3.0", diff --git a/packages/sdks/typescript/src/tests/helpers/run.ts b/packages/sdks/typescript/src/tests/helpers/run.ts index 6c805c2a8..9773d4326 100644 --- a/packages/sdks/typescript/src/tests/helpers/run.ts +++ b/packages/sdks/typescript/src/tests/helpers/run.ts @@ -82,13 +82,16 @@ export function mockNonStreamResponse({ expectedStatus = 200, }: { server: Server - expectedBody: object + expectedBody: object | string expectedStatus?: number }) { server.use( http.post( 'http://localhost:8787/api/v2/projects/123/versions/live/documents/run', - () => HttpResponse.json(expectedBody, { status: expectedStatus }), + () => + typeof expectedBody === 'object' + ? HttpResponse.json(expectedBody, { status: expectedStatus }) + : HttpResponse.text(expectedBody, { status: expectedStatus }), ), ) } diff --git a/packages/sdks/typescript/src/tests/run.test.ts b/packages/sdks/typescript/src/tests/run.test.ts index 6b6f22813..a4278b8c5 100644 --- a/packages/sdks/typescript/src/tests/run.test.ts +++ b/packages/sdks/typescript/src/tests/run.test.ts @@ -390,6 +390,32 @@ describe('/run', () => { ) }) + it('handles 502/504 errors', async () => { + const failedResponse = 'Not json!' + + mockNonStreamResponse({ + server, + expectedBody: failedResponse, + expectedStatus: 502, + }) + + await expect( + sdk.run('path/to/document', { + projectId, + parameters: { foo: 'bar', lol: 'foo' }, + stream: false, + }), + ).rejects.toThrowError( + new LatitudeApiError({ + status: 502, + serverResponse: failedResponse, + message: 'Bad Gateway', + errorCode: ApiErrorCodes.InternalServerError, + dbErrorRef: undefined, + }), + ) + }) + it('should retry 3 times if gateway is not available', async () => { const onErrorMock = vi.fn() const { mockFn } = mock502Response({ diff --git a/packages/sdks/typescript/src/utils/syncRun.ts b/packages/sdks/typescript/src/utils/syncRun.ts index 63f71b5d3..f7272b6dc 100644 --- a/packages/sdks/typescript/src/utils/syncRun.ts +++ b/packages/sdks/typescript/src/utils/syncRun.ts @@ -1,4 +1,5 @@ import { + ApiErrorCodes, ApiErrorJsonResponse, LatitudeErrorCodes, } from '@latitude-data/constants/errors' @@ -54,13 +55,19 @@ export async function syncRun( }) if (!response.ok) { - const json = (await response.json()) as ApiErrorJsonResponse + let json: ApiErrorJsonResponse | undefined + try { + json = (await response.json()) as ApiErrorJsonResponse + } catch (error) { + // Do nothing, sometimes gateway returns html instead of json (502/504 errors) + } + const error = new LatitudeApiError({ status: response.status, - serverResponse: JSON.stringify(json), - message: json.message, - errorCode: json.errorCode, - dbErrorRef: json.dbErrorRef, + serverResponse: json ? JSON.stringify(json) : response.statusText, + message: json?.message ?? response.statusText, + errorCode: json?.errorCode ?? ApiErrorCodes.InternalServerError, + dbErrorRef: json?.dbErrorRef, }) onError?.(error)