diff --git a/src/__tests__/checkout.spec.ts b/src/__tests__/checkout.spec.ts index f77c02e..f09e2b1 100644 --- a/src/__tests__/checkout.spec.ts +++ b/src/__tests__/checkout.spec.ts @@ -222,15 +222,27 @@ describe("Checkout", (): void => { expect(paymentsResponse.pspReference).toBeTruthy(); }); - test("should return correct Exception", async (): Promise => { + test("Should properly handle error responses from API", async (): Promise => { try { scope.post("/payments") - .reply(401); + .reply(422, { + "status": 422, + "errorCode": "200", + "message": "Field 'countryCode' is not valid.", + "errorType": "validation", + "pspReference": "DMB552CV6JHKGK82", + }); const paymentsRequest: checkout.PaymentRequest = createPaymentsCheckoutRequest(); await checkoutService.payments(paymentsRequest); - } catch (e) { - expect(e instanceof HttpClientException).toBeTruthy(); + fail("No exception was thrown"); + } catch (error) { + expect(error instanceof HttpClientException).toBeTruthy(); + if(error instanceof HttpClientException && error.responseBody) { + expect(JSON.parse(error.responseBody).errorType).toBe("validation"); + } else { + fail("Error did not contain the expected data"); + } } }); diff --git a/src/__tests__/management.spec.ts b/src/__tests__/management.spec.ts index 766be7d..15ac954 100644 --- a/src/__tests__/management.spec.ts +++ b/src/__tests__/management.spec.ts @@ -5,6 +5,7 @@ import { Management } from "../services"; import { management } from "../typings"; import * as requests from "../__mocks__/management/requests"; import * as responses from "../__mocks__/management/responses"; +import HttpClientException from "../httpClient/httpClientException"; let client: Client; let managementService: Management; @@ -34,6 +35,32 @@ afterEach(() => { describe("Management", (): void => { describe("Me", (): void => { + test("Should properly handle error responses from API", async (): Promise => { + scope.post("/me/allowedOrigins") + .reply(422, { + "type": "https://docs.adyen.com/errors/not-found", + "title": "Entity was not found", + "status": 422, + "detail": "The origin id is invalid or does not exist.", + "requestId": "KQZ5LXK2VMPRMC82", + "errorCode": "30_112", + }); + try { + const createAllowedOriginRequest : management.CreateAllowedOriginRequest = { + domain: "test.com", + }; + await managementService.Me.createAllowedOrigin(createAllowedOriginRequest); + fail("No exception was thrown"); + } catch (error) { + expect(error instanceof HttpClientException).toBeTruthy(); + if(error instanceof HttpClientException && error.responseBody) { + expect(JSON.parse(error.responseBody).requestId).toBe("KQZ5LXK2VMPRMC82"); + } else { + fail("Error did not contain the expected data"); + } + } + }); + test("Should get API credential details based on the API Key used in the request", async (): Promise => { scope.get("/me") .reply(200, {