Skip to content

Commit 432ca4e

Browse files
[Translation service] Constrain service api (#2539)
* [translation-service] Delay `request`'s work in api.js The "work" got executed immediately, and just the response was delayed with setTimeout. The user could make a next fetch without waiting for a response. Now the work itself is done in the timeout callback, getting delayed too. * [translation-service] Add `ConnectionError` error class Any Error child class satisfied tests that expected `toThrow(Error)`, meaning unintended behaviour still passed the tests. * [CI] Format code --------- Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
1 parent 7dcdd63 commit 432ca4e

File tree

4 files changed

+23
-12
lines changed

4 files changed

+23
-12
lines changed

exercises/concept/translation-service/.meta/config.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
"SleeplessByte"
44
],
55
"contributors": [
6-
"AndrewLawendy"
6+
"AndrewLawendy",
7+
"themetar"
78
],
89
"files": {
910
"solution": [

exercises/concept/translation-service/api.js

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
import { AbusiveClientError, NotAvailable, Untranslatable } from './errors';
1+
import {
2+
AbusiveClientError,
3+
NotAvailable,
4+
Untranslatable,
5+
ConnectionError,
6+
} from './errors';
27

38
const mutex = { current: false };
49

@@ -85,13 +90,12 @@ export class ExternalApi {
8590
}
8691

8792
if (this.values[text]) {
88-
this.values[text].shift();
93+
setTimeout(() => {
94+
this.values[text].shift();
8995

90-
// If it's now available, yay, otherwise, nay
91-
setTimeout(
92-
() => callback(this.values[text][0] ? undefined : makeRandomError()),
93-
1,
94-
);
96+
// If it's now available, yay, otherwise, nay
97+
callback(this.values[text][0] ? undefined : makeRandomError());
98+
}, 1);
9599
return;
96100
}
97101

@@ -114,7 +118,7 @@ function rejectWithRandomDelay(value) {
114118
}
115119

116120
function makeRandomError() {
117-
return new Error(`Error code ${Math.ceil(Math.random() * 10000)}`);
121+
return new ConnectionError(`Error code ${Math.ceil(Math.random() * 10000)}`);
118122
}
119123

120124
class BadRequest extends Error {

exercises/concept/translation-service/errors.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,9 @@ export class Untranslatable extends Error {
2525
super('jIyajbe’');
2626
}
2727
}
28+
29+
export class ConnectionError extends Error {
30+
constructor(message) {
31+
super(message);
32+
}
33+
}

exercises/concept/translation-service/service.spec.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
BatchIsEmpty,
77
} from './service';
88

9-
import { NotAvailable, Untranslatable } from './errors';
9+
import { NotAvailable, Untranslatable, ConnectionError } from './errors';
1010
import { ExternalApi } from './api';
1111

1212
describe('Free service', () => {
@@ -134,7 +134,7 @@ describe('Request service', () => {
134134
test('it requests at most three times (does not retry thrice or more)', async () => {
135135
const actual = service.request('ghobe’');
136136

137-
await expect(actual).rejects.toThrow(Error);
137+
await expect(actual).rejects.toThrow(ConnectionError);
138138
});
139139
});
140140

@@ -182,7 +182,7 @@ describe('Premium service', () => {
182182
test('it requests at most three times (does not retry thrice or more)', async () => {
183183
const actual = service.premium('ghobe’', 0);
184184

185-
await expect(actual).rejects.toThrow(Error);
185+
await expect(actual).rejects.toThrow(ConnectionError);
186186
});
187187

188188
test('it recognizes sufficient quality', async () => {

0 commit comments

Comments
 (0)