File tree Expand file tree Collapse file tree 3 files changed +12
-6
lines changed Expand file tree Collapse file tree 3 files changed +12
-6
lines changed Original file line number Diff line number Diff line change @@ -42,13 +42,11 @@ export default class Client {
42
42
43
43
async request ( path , options = { } ) {
44
44
try {
45
- const fetchOptions = {
45
+ const response = await fetch ( this . baseURL + path , {
46
46
headers : this . headers ,
47
47
timeout : this . timeout ,
48
48
...options ,
49
- } ;
50
-
51
- const response = await fetch ( this . baseURL + path , fetchOptions ) ;
49
+ } ) ;
52
50
53
51
if ( ! response . ok ) {
54
52
throw new Error ( await response . text ( ) ) ;
Original file line number Diff line number Diff line change 1
1
class DetectLanguageError extends Error { }
2
2
3
3
function handleError ( error ) {
4
- const message = error ?. response ?. data ?. error ?. message || error . message ;
4
+ let message ;
5
+
6
+ try {
7
+ const json = JSON . parse ( error . message ) ;
8
+ message = json ?. error ?. message || error . message ;
9
+ } catch ( e ) {
10
+ message = error . message ;
11
+ }
12
+
5
13
const apiError = new DetectLanguageError ( message ) ;
6
14
7
15
apiError . stack = error . stack ;
Original file line number Diff line number Diff line change @@ -26,7 +26,7 @@ describe('detect', () => {
26
26
expect ( result [ 0 ] . score ) . to . be . a ( 'number' ) ;
27
27
} ) ;
28
28
29
- it ( 'detects language ' , async ( ) => {
29
+ it ( 'fails with invalid API key ' , async ( ) => {
30
30
detectLanguage = new DetectLanguage ( 'invalid' ) ;
31
31
32
32
await expect ( detectLanguage . detect ( 'hello' ) ) . to . be . rejectedWith ( 'Invalid API key' ) ;
You can’t perform that action at this time.
0 commit comments