diff --git a/exercises/concept/translation-service/.meta/config.json b/exercises/concept/translation-service/.meta/config.json index 10932f1883..694c26f511 100644 --- a/exercises/concept/translation-service/.meta/config.json +++ b/exercises/concept/translation-service/.meta/config.json @@ -3,7 +3,8 @@ "SleeplessByte" ], "contributors": [ - "AndrewLawendy" + "AndrewLawendy", + "themetar" ], "files": { "solution": [ diff --git a/exercises/concept/translation-service/api.js b/exercises/concept/translation-service/api.js index c4ab682f0a..a4023a23bc 100644 --- a/exercises/concept/translation-service/api.js +++ b/exercises/concept/translation-service/api.js @@ -1,4 +1,9 @@ -import { AbusiveClientError, NotAvailable, Untranslatable } from './errors'; +import { + AbusiveClientError, + NotAvailable, + Untranslatable, + ConnectionError, +} from './errors'; const mutex = { current: false }; @@ -85,13 +90,12 @@ export class ExternalApi { } if (this.values[text]) { - this.values[text].shift(); + setTimeout(() => { + this.values[text].shift(); - // If it's now available, yay, otherwise, nay - setTimeout( - () => callback(this.values[text][0] ? undefined : makeRandomError()), - 1, - ); + // If it's now available, yay, otherwise, nay + callback(this.values[text][0] ? undefined : makeRandomError()); + }, 1); return; } @@ -114,7 +118,7 @@ function rejectWithRandomDelay(value) { } function makeRandomError() { - return new Error(`Error code ${Math.ceil(Math.random() * 10000)}`); + return new ConnectionError(`Error code ${Math.ceil(Math.random() * 10000)}`); } class BadRequest extends Error { diff --git a/exercises/concept/translation-service/errors.js b/exercises/concept/translation-service/errors.js index 5f25ae705b..a3ed001521 100644 --- a/exercises/concept/translation-service/errors.js +++ b/exercises/concept/translation-service/errors.js @@ -25,3 +25,9 @@ export class Untranslatable extends Error { super('jIyajbe’'); } } + +export class ConnectionError extends Error { + constructor(message) { + super(message); + } +} diff --git a/exercises/concept/translation-service/service.spec.js b/exercises/concept/translation-service/service.spec.js index 32a72e014e..65bc922f0f 100644 --- a/exercises/concept/translation-service/service.spec.js +++ b/exercises/concept/translation-service/service.spec.js @@ -6,7 +6,7 @@ import { BatchIsEmpty, } from './service'; -import { NotAvailable, Untranslatable } from './errors'; +import { NotAvailable, Untranslatable, ConnectionError } from './errors'; import { ExternalApi } from './api'; describe('Free service', () => { @@ -134,7 +134,7 @@ describe('Request service', () => { test('it requests at most three times (does not retry thrice or more)', async () => { const actual = service.request('ghobe’'); - await expect(actual).rejects.toThrow(Error); + await expect(actual).rejects.toThrow(ConnectionError); }); }); @@ -182,7 +182,7 @@ describe('Premium service', () => { test('it requests at most three times (does not retry thrice or more)', async () => { const actual = service.premium('ghobe’', 0); - await expect(actual).rejects.toThrow(Error); + await expect(actual).rejects.toThrow(ConnectionError); }); test('it recognizes sufficient quality', async () => {