Skip to content

Commit

Permalink
feat(sdk/typescript): handle 5xx errors gracefully from sdk (#580)
Browse files Browse the repository at this point in the history
  • Loading branch information
geclos authored Nov 11, 2024
1 parent fd5514b commit 7c68732
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 8 deletions.
2 changes: 1 addition & 1 deletion packages/sdks/typescript/package.json
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>",
"license": "LGPL-3.0",
Expand Down
7 changes: 5 additions & 2 deletions packages/sdks/typescript/src/tests/helpers/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }),
),
)
}
Expand Down
26 changes: 26 additions & 0 deletions packages/sdks/typescript/src/tests/run.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,32 @@ describe('/run', () => {
)
})

it('handles 502/504 errors', async () => {
const failedResponse = '<html>Not json!</html>'

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({
Expand Down
17 changes: 12 additions & 5 deletions packages/sdks/typescript/src/utils/syncRun.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
ApiErrorCodes,
ApiErrorJsonResponse,
LatitudeErrorCodes,
} from '@latitude-data/constants/errors'
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 7c68732

Please sign in to comment.