From b6d6e4a892fd349522b246c1902eb52bbeb6b508 Mon Sep 17 00:00:00 2001 From: Florencia-97 Date: Thu, 31 Oct 2024 13:19:00 -0300 Subject: [PATCH] Now network problems are being treates as a type of response --- package.json | 2 +- src/requester/RemoteRequester.js | 3 ++ .../ConnectivityErrorResponse.js | 7 +++ test/errorHandling.test.js | 51 ++++++++++++++++++- 4 files changed, 60 insertions(+), 3 deletions(-) create mode 100644 src/responses/generalResponses/ConnectivityErrorResponse.js diff --git a/package.json b/package.json index 4d9acd7..874ac00 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@eryxcoop/appyx-comm", - "version": "1.0.9", + "version": "1.0.10", "description": "Paquete de comunicaciĆ³n web", "main": "index.js", "source": "index.js", diff --git a/src/requester/RemoteRequester.js b/src/requester/RemoteRequester.js index adc1b44..199dcfd 100644 --- a/src/requester/RemoteRequester.js +++ b/src/requester/RemoteRequester.js @@ -2,6 +2,7 @@ import {Requester} from "./Requester"; import UnexpectedErrorResponse from "../responses/generalResponses/UnexpectedErrorResponse"; import MultiPartEncoder from "./encoders/MultiPartEncoder.js"; import JsonEncoder from "./encoders/JsonEncoder.js"; +import ConnectivityErrorResponse from "../responses/generalResponses/ConnectivityErrorResponse"; export default class RemoteRequester extends Requester { @@ -24,6 +25,8 @@ export default class RemoteRequester extends Requester { }) .then(jsonResponse => { return this._buildResponse(jsonResponse, endpoint) + }).catch((error) => { + return new ConnectivityErrorResponse(error); }) } diff --git a/src/responses/generalResponses/ConnectivityErrorResponse.js b/src/responses/generalResponses/ConnectivityErrorResponse.js new file mode 100644 index 0000000..4caa056 --- /dev/null +++ b/src/responses/generalResponses/ConnectivityErrorResponse.js @@ -0,0 +1,7 @@ +import ApiErrorResponse from "./ApiErrorResponse"; + +export default class ConnectivityErrorResponse extends ApiErrorResponse { + static errorCodes() { + return ["connectivity_error_code"]; + } +} \ No newline at end of file diff --git a/test/errorHandling.test.js b/test/errorHandling.test.js index 3193176..19a8b36 100644 --- a/test/errorHandling.test.js +++ b/test/errorHandling.test.js @@ -1,11 +1,12 @@ import Endpoint from "../src/endpoints/Endpoint"; -import {FakeRequester, SuccessfulApiResponse} from "../index"; +import {FakeRequester, RemoteRequester, SuccessfulApiResponse} from "../index"; import ApiClient from "../src/ApiClient"; import ApiResponseHandler from "../src/errors/ApiResponseHandler.js"; import AuthenticationErrorResponse from "../src/responses/generalResponses/AuthenticationErrorResponse"; import {expect, test} from 'vitest' import {DummyRequester} from "./utils/DummyRequester.js"; +import ConnectivityErrorResponse from "../src/responses/generalResponses/ConnectivityErrorResponse"; class TestSuccessfulApiResponse extends SuccessfulApiResponse { @@ -427,4 +428,50 @@ test('When using fake requester for endpoint with same url only the clarified me // Then the response is handled by the custom response handler expect(response).toBe(TestSuccessfulApiResponse.defaultResponse()); -}); \ No newline at end of file +}); + +test('When network connective error happens it is handled', async () => { + // Given a client + const requester = new RemoteRequester('http://localhost:3100'); + + // but has a general handler for connection error responses + const generalErrorHandler = ApiResponseHandler.for( + ConnectivityErrorResponse, + () => { + return 'network error!' + }, + ); + const client = new ExampleApiClient(requester, generalErrorHandler); + + const response = await client.exampleEndpoint(); + + // Then the response is handled by the custom response handler + expect(response).toBe('network error!') +}); + +test('When network connectivity is not overriden by a specific error normal handler', async () => { + // Given a client + const requester = new RemoteRequester('http://localhost:3100'); + + // but has a general handler for connection error responses + const generalErrorHandler = ApiResponseHandler.for( + ConnectivityErrorResponse, + () => { + return 'network error!' + }, + ); + const client = new ExampleApiClient(requester, generalErrorHandler); + + // but has a custom error handler for it + const customErrorHandler = ApiResponseHandler.for( + AuthenticationErrorResponse, + (request) => { + return 'should not see this' + }, + ); + + const response = await client.exampleEndpoint(customErrorHandler); + + // Then the response is handled by the custom response handler + expect(response).toBe('network error!') +});