Skip to content
This repository has been archived by the owner on Feb 6, 2024. It is now read-only.

Commit

Permalink
fix: handle non-xml errors
Browse files Browse the repository at this point in the history
  • Loading branch information
gigobyte committed May 12, 2020
1 parent f279d8e commit 5c59290
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 22 deletions.
8 changes: 6 additions & 2 deletions src/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,10 @@ export class HttpClient {
try {
return await this.fetch(config).then((x) => parseResponse(x))
} catch (error) {
if (parser.validate(error) !== true) {
throw error
}

const maybeResponse = MWSApiError.decode(parser.parse(error))

if (maybeResponse.isRight()) {
Expand All @@ -197,9 +201,9 @@ export class HttpClient {
const ErrorToThrow = errorMap[errorCode]

throw enhanceError(new ErrorToThrow(`${info.action} request failed`), response)
} else {
throw error
}

throw error
}
}
}
85 changes: 65 additions & 20 deletions test/unit/http.test.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,76 @@
import { amazonMarketplaces, HttpClient, InvalidParameterValue } from '../../src'
import {
amazonMarketplaces,
HttpClient,
HttpError,
InvalidParameterValue,
MWSError,
} from '../../src'
import { Resource } from '../../src/http'
import { getFixture } from '../utils'

const httpClientThatThrows = (error: unknown) =>
new HttpClient(
{
awsAccessKeyId: '',
marketplace: amazonMarketplaces.CA,
mwsAuthToken: '',
secretKey: '',
sellerId: '',
},
() => {
throw error
},
)

const mockRequest = {
resource: Resource.Sellers,
version: '',
action: 'GetServiceStatus',
parameters: {},
} as const

describe('httpClient', () => {
it('should throw a HttpError on failure', async () => {
expect.assertions(1)

const httpClient = new HttpClient(
{
awsAccessKeyId: '',
marketplace: amazonMarketplaces.CA,
mwsAuthToken: '',
secretKey: '',
sellerId: '',
},
() => {
throw getFixture('error-response')
},
const httpClient = httpClientThatThrows(getFixture('error-response'))

await expect(() => httpClient.request('POST', mockRequest)).rejects.toStrictEqual(
new InvalidParameterValue('GetServiceStatus request failed'),
)
})

it('should throw a HttpError that can be handled', async () => {
expect.assertions(7)

const httpClient = httpClientThatThrows(getFixture('error-response'))

let expectedError!: InvalidParameterValue

try {
await httpClient.request('POST', mockRequest)
} catch (error) {
expectedError = error
}

await expect(() =>
httpClient.request('POST', {
resource: Resource.Sellers,
version: '',
action: 'GetServiceStatus',
parameters: {},
}),
).rejects.toStrictEqual(new InvalidParameterValue('GetServiceStatus request failed'))
expect(expectedError instanceof MWSError).toStrictEqual(true)
expect(expectedError instanceof HttpError).toStrictEqual(true)
expect(expectedError instanceof InvalidParameterValue).toStrictEqual(true)
expect(expectedError.code).toStrictEqual('InvalidParameterValue')
expect(expectedError.type).toStrictEqual('Sender')
expect(expectedError.requestId).toStrictEqual('e26147f9-30cc-4379-9fb5-bd4ad966c48b')
expect(expectedError.mwsMessage).toStrictEqual(
'CreatedAfter or LastUpdatedAfter must be specified',
)
})

it('should propagate non-API errors', async () => {
expect.assertions(1)

const httpClient = httpClientThatThrows(new Error('Out of memory'))

await expect(() => httpClient.request('POST', mockRequest)).rejects.toStrictEqual(
new Error('Out of memory'),
)
})
})

0 comments on commit 5c59290

Please sign in to comment.