diff --git a/packages/shared/src/porter.ts b/packages/shared/src/porter.ts index b01d25e2..1808b435 100644 --- a/packages/shared/src/porter.ts +++ b/packages/shared/src/porter.ts @@ -175,15 +175,15 @@ export class PorterClient { const localConfig = { ...config, baseURL: porterUrl.toString() }; try { resp = await axios.request(localConfig); + if (resp.status === HttpStatusCode.Ok) { + return resp; + } } catch (e) { lastError = e; continue; } - if (resp.status === HttpStatusCode.Ok) { - return resp; - } } - if (lastError !== undefined) { + if (lastError) { throw lastError; } throw new Error( diff --git a/packages/shared/test/porter.test.ts b/packages/shared/test/porter.test.ts index e4c7e637..ab9455b7 100644 --- a/packages/shared/test/porter.test.ts +++ b/packages/shared/test/porter.test.ts @@ -41,10 +41,10 @@ const mockGetUrsulas = (ursulas: Ursula[] = fakeUrsulas()): SpyInstance => { status: HttpStatusCode.Ok, data: fakePorterUrsulas(ursulas), }); + case fakePorterUris[1]: + return Promise.resolve({ status: HttpStatusCode.BadRequest, data: '' }); case fakePorterUris[0]: - throw new Error(); - default: - throw Promise.resolve({ status: HttpStatusCode.BadRequest }); + throw new Error(`Test error`); } }); }; @@ -107,9 +107,13 @@ describe('PorterClient', () => { it('returns error in case all porters fail', async () => { const ursulas = fakeUrsulas(); mockGetUrsulas(ursulas); - let porterClient = new PorterClient([fakePorterUris[0], fakePorterUris[1]]); - expect(porterClient.getUrsulas(ursulas.length)).rejects.toThrowError(); + let porterClient = new PorterClient([fakePorterUris[1]]); + expect(porterClient.getUrsulas(ursulas.length)).rejects.toThrowError( + Error(`Porter returns bad response: 400 - `), + ); porterClient = new PorterClient([fakePorterUris[1], fakePorterUris[0]]); - expect(porterClient.getUrsulas(ursulas.length)).rejects.toThrowError(); + expect(porterClient.getUrsulas(ursulas.length)).rejects.toThrowError( + Error(`Test error`), + ); }); });