Skip to content

Commit

Permalink
fix(sdk-client-v3): fixing support ticket ST-29803 (#864)
Browse files Browse the repository at this point in the history
- fix issues with SDK return 200 and undefined response body on error
  • Loading branch information
ajimae authored Dec 10, 2024
1 parent d1bd393 commit 2d622a1
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/ninety-pugs-decide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@commercetools/ts-client': patch
---

Fix issue with sdk-client-v3 returning undefined body on error
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,8 @@ async function executeRequest({
})

throw {
body: error,
// because body and error should be mutually exclusive
body: null,
error,
}
} finally {
Expand Down
2 changes: 1 addition & 1 deletion packages/sdk-client-v3/src/utils/executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ export default async function executor(request: HttpClientConfig) {
data = response.data || response
}
} catch (err) {
data = result
throw err
}

return {
Expand Down
45 changes: 43 additions & 2 deletions packages/sdk-client-v3/tests/http.test/http-middleware.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -394,14 +394,14 @@ describe('Http Middleware.', () => {
}
})

test('should return a parse a text encoded response as json', async () => {
test('should return a text encoded response as a parsed json', async () => {
const _response = {
data: {},
statusCode: 200,
}

const response = createTestResponse({
text: () => _response,
text: () => JSON.stringify(_response),
})

const request = createTestRequest({
Expand Down Expand Up @@ -429,6 +429,47 @@ describe('Http Middleware.', () => {
await createHttpMiddleware(httpMiddlewareOptions)(next)(request)
})

test('should throw an error if it is unable to parse a text response', async () => {
const _response = {
data: {},
statusCode: 200,
}

const response = createTestResponse({
text: () => _response, // this is not a text response
})

const request = createTestRequest({
uri: '/error-url/retry',
method: 'POST',
body: { id: 'test-id' },
headers: {
'Content-Type': 'image/jpeg',
},
})

const httpMiddlewareOptions: HttpMiddlewareOptions = {
host: 'http://api-host.com',
httpClient: jest.fn(() => response),
enableRetry: false,
}

const next = (req: MiddlewareRequest) => {
expect(typeof req.response).toEqual('object')
expect(req.response?.body).toEqual(_response)

return response
}

try {
await createHttpMiddleware(httpMiddlewareOptions)(next)(request)
} catch (e) {
expect(e.body).toEqual(null)
expect(e.error.statusCode).toEqual(0)
expect(e.error.name).toEqual('NetworkError')
}
})

describe('::retry test', () => {
test('should throw if `retryCode` is not an array', async () => {
const response = createTestResponse({
Expand Down

0 comments on commit 2d622a1

Please sign in to comment.