Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: exposes original axios error object #479

Merged
merged 2 commits into from
Jun 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 15 additions & 12 deletions src/helpers/api-error-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,34 +31,37 @@ export function apiErrorFactory(error: AxiosError): ExtendableError {
if (isAPIModelError(data)) {
const modelError = data.errors.shift()
if (modelError === undefined) {
return new SellingPartnerUnknownError(
{
return new SellingPartnerUnknownError({
modelError: {
code: code || 'UnknownError',
message,
},
headers,
)
cause: error,
})
}

const errorParameters = { modelError, headers, cause: error }

switch (status) {
case StatusCodes.BAD_REQUEST:
return new SellingPartnerBadRequestError(modelError, headers)
return new SellingPartnerBadRequestError(errorParameters)
case StatusCodes.FORBIDDEN:
return new SellingPartnerForbiddenError(modelError, headers)
return new SellingPartnerForbiddenError(errorParameters)
case StatusCodes.NOT_FOUND:
return new SellingPartnerNotFoundError(modelError, headers)
return new SellingPartnerNotFoundError(errorParameters)
case StatusCodes.REQUEST_TOO_LONG:
return new SellingPartnerRequestTooLongError(modelError, headers)
return new SellingPartnerRequestTooLongError(errorParameters)
case StatusCodes.UNSUPPORTED_MEDIA_TYPE:
return new SellingPartnerUnsupportedMediaTypeError(modelError, headers)
return new SellingPartnerUnsupportedMediaTypeError(errorParameters)
case StatusCodes.TOO_MANY_REQUESTS:
return new SellingPartnerTooManyRequestsError(modelError, headers)
return new SellingPartnerTooManyRequestsError(errorParameters)
case StatusCodes.INTERNAL_SERVER_ERROR:
return new SellingPartnerInternalServerError(modelError, headers)
return new SellingPartnerInternalServerError(errorParameters)
case StatusCodes.SERVICE_UNAVAILABLE:
return new SellingPartnerServiceUnavailableError(modelError, headers)
return new SellingPartnerServiceUnavailableError(errorParameters)
default:
return new SellingPartnerGenericError(modelError, headers)
return new SellingPartnerGenericError(errorParameters)
}
}
}
Expand Down
29 changes: 20 additions & 9 deletions src/types/errors/selling-partner-api-errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ export interface ModelError {
details?: string
}

export interface SellingPartnerErrorParameters {
modelError: ModelError

headers: AxiosResponseHeaders

cause: Error
}

export class SellingPartnerGenericError extends ExtendableError {
public code: string

Expand All @@ -36,13 +44,16 @@ export class SellingPartnerGenericError extends ExtendableError {

public requestId: string

public constructor(error: ModelError, headers: AxiosResponseHeaders) {
super(error.details)
public cause: Error

public constructor({ modelError, headers, cause }: SellingPartnerErrorParameters) {
super(modelError.details)

this.code = error.code
this.message = error.message
this.details = error.details
this.code = modelError.code
this.message = modelError.message
this.details = modelError.details
this.requestId = headers['x-amzn-RequestId'] || headers['x-amzn-requestid'] || ''
this.cause = cause
}
}

Expand All @@ -54,11 +65,11 @@ export class SellingPartnerUnsupportedMediaTypeError extends SellingPartnerGener
export class SellingPartnerTooManyRequestsError extends SellingPartnerGenericError {
public rateLimit?: number

public constructor(error: ModelError, headers: AxiosResponseHeaders) {
super(error, headers)
public constructor(parameters: SellingPartnerErrorParameters) {
super(parameters)
this.rateLimit =
Number(headers['x-amzn-RateLimit-Limit']) ||
Number(headers['x-amzn-ratelimit-limit']) ||
Number(parameters.headers['x-amzn-RateLimit-Limit']) ||
Number(parameters.headers['x-amzn-ratelimit-limit']) ||
undefined
}
}
Expand Down
17 changes: 12 additions & 5 deletions test/types/errors/errors.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import {
} from '../../../src'

describe(`client`, () => {
const DEFAULT_INTERNAL_SERVER_ERROR_MESSAGE = 'Request failed with status code 500'

it(`should throw ${SellingPartnerForbiddenError.name} when pass invalid token`, async () => {
expect.assertions(2)

Expand Down Expand Up @@ -136,7 +138,7 @@ describe(`client`, () => {
})

it(`should handle unknown error`, async () => {
expect.assertions(3)
expect.assertions(4)

const { CA } = amazonMarketplaces
assertMarketplaceHasSellingPartner(CA)
Expand All @@ -161,11 +163,16 @@ describe(`client`, () => {

await expect(client.getMarketplaceParticipations()).rejects.toHaveProperty(
'message',
'Request failed with status code 500',
DEFAULT_INTERNAL_SERVER_ERROR_MESSAGE,
)
await expect(client.getMarketplaceParticipations()).rejects.toHaveProperty(
'code',
'ERR_BAD_RESPONSE',
AxiosError.ERR_BAD_RESPONSE,
)

await expect(client.getMarketplaceParticipations()).rejects.toHaveProperty(
'cause',
new AxiosError(DEFAULT_INTERNAL_SERVER_ERROR_MESSAGE, AxiosError.ERR_BAD_RESPONSE),
)
})

Expand All @@ -192,11 +199,11 @@ describe(`client`, () => {

await expect(client.getMarketplaceParticipations()).rejects.toHaveProperty(
'message',
'Request failed with status code 500',
DEFAULT_INTERNAL_SERVER_ERROR_MESSAGE,
)
await expect(client.getMarketplaceParticipations()).rejects.toHaveProperty(
'code',
'ERR_BAD_RESPONSE',
AxiosError.ERR_BAD_RESPONSE,
)
})
})