Skip to content

Commit

Permalink
Now network problems are being treates as a type of response
Browse files Browse the repository at this point in the history
  • Loading branch information
Florencia-97 committed Oct 31, 2024
1 parent 8b8d7aa commit b6d6e4a
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 3 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
3 changes: 3 additions & 0 deletions src/requester/RemoteRequester.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -24,6 +25,8 @@ export default class RemoteRequester extends Requester {
})
.then(jsonResponse => {
return this._buildResponse(jsonResponse, endpoint)
}).catch((error) => {
return new ConnectivityErrorResponse(error);
})
}

Expand Down
7 changes: 7 additions & 0 deletions src/responses/generalResponses/ConnectivityErrorResponse.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import ApiErrorResponse from "./ApiErrorResponse";

export default class ConnectivityErrorResponse extends ApiErrorResponse {
static errorCodes() {
return ["connectivity_error_code"];
}
}
51 changes: 49 additions & 2 deletions test/errorHandling.test.js
Original file line number Diff line number Diff line change
@@ -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 {

Expand Down Expand Up @@ -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());
});
});

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!')
});

0 comments on commit b6d6e4a

Please sign in to comment.