@@ -15,7 +15,7 @@ import * as TE from 'fp-ts/lib/TaskEither';
15
15
import * as t from 'io-ts' ;
16
16
import { PathReporter } from 'io-ts/lib/PathReporter' ;
17
17
import { MinimalEndpointInstance , TypeOfEndpointInstance } from '../endpoints' ;
18
- import { APIError } from '../errors/APIError' ;
18
+ import { APIError , fromAxiosError , isAPIError } from '../errors/APIError' ;
19
19
import { toValidationError } from '../errors/ValidationError' ;
20
20
import { trexLogger } from '../logger' ;
21
21
@@ -24,21 +24,37 @@ export const apiLogger = trexLogger.extend('API');
24
24
export const toAPIError = ( e : unknown ) : APIError => {
25
25
// eslint-disable-next-line
26
26
apiLogger . error ( 'An error occurred %O' , e ) ;
27
+ if ( isAPIError ( e ) ) {
28
+ return e ;
29
+ }
30
+
31
+ if ( axios . isAxiosError ( e ) ) {
32
+ return fromAxiosError ( e ) ;
33
+ }
34
+
27
35
if ( e instanceof Error ) {
28
36
if ( e . message === 'Network Error' ) {
29
- return new APIError (
30
- 502 ,
31
- 'Network Error' ,
32
- 'The API endpoint is not reachable' ,
33
- [ "Be sure you're connected to internet." ]
34
- ) ;
37
+ return new APIError ( 'Network Error' , {
38
+ kind : 'NetworkError' ,
39
+ status : '500' ,
40
+ meta : [
41
+ 'The API endpoint is not reachable' ,
42
+ "Be sure you're connected to internet." ,
43
+ ] ,
44
+ } ) ;
35
45
}
36
- return new APIError ( 500 , 'UnknownError' , e . message , [ ] ) ;
46
+ return new APIError ( e . message , {
47
+ kind : 'ClientError' ,
48
+ meta : e . stack ,
49
+ status : '500' ,
50
+ } ) ;
37
51
}
38
52
39
- return new APIError ( 500 , 'UnknownError' , 'An error occurred' , [
40
- JSON . stringify ( e ) ,
41
- ] ) ;
53
+ return new APIError ( 'An error occurred' , {
54
+ kind : 'ClientError' ,
55
+ meta : JSON . stringify ( e ) ,
56
+ status : '500' ,
57
+ } ) ;
42
58
} ;
43
59
44
60
const liftFetch = < B > (
@@ -51,11 +67,7 @@ const liftFetch = <B>(
51
67
TE . chain ( ( content ) => {
52
68
return pipe (
53
69
decode ( content ) ,
54
- E . mapLeft ( ( e ) : APIError => {
55
- const details = PathReporter . report ( E . left ( e ) ) ;
56
- apiLogger . error ( 'toAPIError Validation failed %O' , details ) ;
57
- return toValidationError ( 'Validation failed' , details ) ;
58
- } ) ,
70
+ E . mapLeft ( ( e ) => toValidationError ( 'Validation failed' , e ) ) ,
59
71
TE . fromEither
60
72
) ;
61
73
} )
@@ -128,7 +140,7 @@ export const MakeHTTPClient = (client: AxiosInstance): HTTPClient => {
128
140
TE . mapLeft ( ( e ) : APIError => {
129
141
const details = PathReporter . report ( E . left ( e ) ) ;
130
142
apiLogger . error ( 'MakeHTTPClient Validation failed %O' , details ) ;
131
- return toValidationError ( 'Validation failed' , details ) ;
143
+ return toValidationError ( 'Validation failed' , e ) ;
132
144
} ) ,
133
145
TE . chain ( ( input ) => {
134
146
const url = e . getPath ( input . params ) ;
0 commit comments