Skip to content

Commit

Permalink
fix: error code and message are no longer undefined (#134)
Browse files Browse the repository at this point in the history
* fix: error code and message are no longer undefined

* fix: class for unknown errors added, error tests fixed
  • Loading branch information
Mykelo authored Jun 3, 2021
1 parent 6761759 commit 55b64b5
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 10 deletions.
35 changes: 25 additions & 10 deletions src/helpers/api-error-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { ExtendableError } from 'ts-error'

import {
ModelError,
ModelErrorContainer,
SellingPartnerBadRequestError,
SellingPartnerForbiddenError,
SellingPartnerGenericError,
Expand All @@ -12,32 +13,46 @@ import {
SellingPartnerRequestTooLongError,
SellingPartnerServiceUnavailableError,
SellingPartnerTooManyRequestsError,
SellingPartnerUnknownError,
SellingPartnerUnsupportedMediaTypeError,
} from '../types'

export function apiErrorFactory<T extends ModelError>(error: AxiosError<T>): ExtendableError {
export function apiErrorFactory<T extends ModelErrorContainer>(
error: AxiosError<T>,
): ExtendableError {
const { response } = error
if (response) {
const { headers, data } = response
const modelError: ModelError | undefined = data.errors[0]
if (modelError === undefined) {
return new SellingPartnerUnknownError(
{
code: 'UnknownError',
message: 'Selling Partner API unknown error',
},
headers,
)
}

switch (response.status) {
case StatusCodes.BAD_REQUEST:
return new SellingPartnerBadRequestError(data, headers)
return new SellingPartnerBadRequestError(modelError, headers)
case StatusCodes.FORBIDDEN:
return new SellingPartnerForbiddenError(data, headers)
return new SellingPartnerForbiddenError(modelError, headers)
case StatusCodes.NOT_FOUND:
return new SellingPartnerNotFoundError(data, headers)
return new SellingPartnerNotFoundError(modelError, headers)
case StatusCodes.REQUEST_TOO_LONG:
return new SellingPartnerRequestTooLongError(data, headers)
return new SellingPartnerRequestTooLongError(modelError, headers)
case StatusCodes.UNSUPPORTED_MEDIA_TYPE:
return new SellingPartnerUnsupportedMediaTypeError(data, headers)
return new SellingPartnerUnsupportedMediaTypeError(modelError, headers)
case StatusCodes.TOO_MANY_REQUESTS:
return new SellingPartnerTooManyRequestsError(data, headers)
return new SellingPartnerTooManyRequestsError(modelError, headers)
case StatusCodes.INTERNAL_SERVER_ERROR:
return new SellingPartnerInternalServerError(data, headers)
return new SellingPartnerInternalServerError(modelError, headers)
case StatusCodes.SERVICE_UNAVAILABLE:
return new SellingPartnerServiceUnavailableError(data, headers)
return new SellingPartnerServiceUnavailableError(modelError, headers)
default:
return new SellingPartnerGenericError(data, headers)
return new SellingPartnerGenericError(modelError, headers)
}
} else {
return error
Expand Down
5 changes: 5 additions & 0 deletions src/types/errors/selling-partner-api-errors.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
/* eslint-disable max-classes-per-file */
import { ExtendableError } from 'ts-error'

export interface ModelErrorContainer {
errors: ModelError[]
}

export interface ModelError {
/**
* An error code that identifies the type of error that occurred.
Expand Down Expand Up @@ -59,5 +63,6 @@ export class SellingPartnerTooManyRequestsError extends SellingPartnerGenericErr
}
export class SellingPartnerInternalServerError extends SellingPartnerGenericError {}
export class SellingPartnerServiceUnavailableError extends SellingPartnerGenericError {}
export class SellingPartnerUnknownError extends SellingPartnerGenericError {}

/* eslint-enable max-classes-per-file */
16 changes: 16 additions & 0 deletions test/types/errors/errors.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ describe(`client`, () => {
.any(`${CA.sellingPartner.region.endpoint}/sellers/v1/marketplaceParticipations`)
.intercept((request, response) => {
response.setHeader('x-amzn-requestid', requestId).sendStatus(StatusCodes.FORBIDDEN)
response.send({
errors: [
{
code: 'Forbidden',
message: 'Forbidden',
},
],
})
})

const client = new SellersApiClient(configuration)
Expand Down Expand Up @@ -100,6 +108,14 @@ describe(`client`, () => {
response
.setHeader('x-amzn-RateLimit-Limit', defaultRateLimit)
.sendStatus(StatusCodes.TOO_MANY_REQUESTS)
response.send({
errors: [
{
code: 'TooManyRequests',
message: 'Too many requests',
},
],
})
})

const configuration: APIConfigurationParameters = {
Expand Down

0 comments on commit 55b64b5

Please sign in to comment.