Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Translation service] Constrain service api #2539

Merged
merged 3 commits into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion exercises/concept/translation-service/.meta/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
"SleeplessByte"
],
"contributors": [
"AndrewLawendy"
"AndrewLawendy",
"themetar"
],
"files": {
"solution": [
Expand Down
20 changes: 12 additions & 8 deletions exercises/concept/translation-service/api.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { AbusiveClientError, NotAvailable, Untranslatable } from './errors';
import {
AbusiveClientError,
NotAvailable,
Untranslatable,
ConnectionError,
} from './errors';

const mutex = { current: false };

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

Expand All @@ -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 {
Expand Down
6 changes: 6 additions & 0 deletions exercises/concept/translation-service/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,9 @@ export class Untranslatable extends Error {
super('jIyajbe’');
}
}

export class ConnectionError extends Error {
constructor(message) {
super(message);
}
}
6 changes: 3 additions & 3 deletions exercises/concept/translation-service/service.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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);
});
});

Expand Down Expand Up @@ -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 () => {
Expand Down