11import fs from 'node:fs' ;
22import { Agent } from 'node:https' ;
33
4- import { AuthError , CheckSwitcherError , CriteriaError , SnapshotServiceError } from './exceptions/index.js' ;
4+ import { CheckSwitcherError , ClientError , RemoteError } from './exceptions/index.js' ;
55import FetchFacade from './utils/fetchFacade.js' ;
66import * as util from './utils/index.js' ;
77import { GlobalAuth } from './globals/globalAuth.js' ;
88
99let httpClient ;
1010
11- const getConnectivityError = ( code ) => `Connection has been refused - ${ code } ` ;
12-
1311const getHeader = ( token ) => {
1412 return {
1513 'Authorization' : `Bearer ${ token } ` ,
@@ -19,7 +17,7 @@ const getHeader = (token) => {
1917
2018export function setCerts ( certPath ) {
2119 if ( ! fs . existsSync ( certPath ) ) {
22- throw new Error ( `Invalid certificate path '${ certPath } '` ) ;
20+ throw new RemoteError ( `Invalid certificate path '${ certPath } '` ) ;
2321 }
2422
2523 httpClient = new Agent ( {
@@ -36,7 +34,7 @@ export function getEntry(input) {
3634 return undefined ;
3735 }
3836
39- let entry = [ ] ;
37+ const entry = [ ] ;
4038 for ( const inputValues of input ) {
4139 entry . push ( {
4240 strategy : inputValues [ 0 ] ,
@@ -67,9 +65,9 @@ export async function auth(context) {
6765 return response . json ( ) ;
6866 }
6967
70- throw new Error ( `[auth] failed with status ${ response . status } ` ) ;
68+ throw new RemoteError ( `[auth] failed with status ${ response . status } ` ) ;
7169 } catch ( e ) {
72- throw new AuthError ( e . errno ? getConnectivityError ( e . errno ) : e . message ) ;
70+ throw errorHandler ( e ) ;
7371 }
7472}
7573
@@ -95,10 +93,10 @@ export async function checkCriteria(key, input, showDetail = false) {
9593 if ( response . status == 200 ) {
9694 return response . json ( ) ;
9795 }
98-
99- throw new Error ( `[checkCriteria] failed with status ${ response . status } ` ) ;
96+
97+ throw new RemoteError ( `[checkCriteria] failed with status ${ response . status } ` ) ;
10098 } catch ( e ) {
101- throw new CriteriaError ( e . errno ? getConnectivityError ( e . errno ) : e . message ) ;
99+ throw errorHandler ( e ) ;
102100 }
103101}
104102
@@ -112,15 +110,15 @@ export async function checkSwitchers(switcherKeys) {
112110 } ) ;
113111
114112 if ( response . status != 200 ) {
115- throw new Error ( `[checkSwitchers] failed with status ${ response . status } ` ) ;
113+ throw new RemoteError ( `[checkSwitchers] failed with status ${ response . status } ` ) ;
116114 }
117115
118116 const json = response . json ( ) ;
119117 if ( json . not_found . length ) {
120118 throw new CheckSwitcherError ( json . not_found ) ;
121119 }
122120 } catch ( e ) {
123- throw new CriteriaError ( e . errno ? getConnectivityError ( e . errno ) : e . message ) ;
121+ throw errorHandler ( e ) ;
124122 }
125123}
126124
@@ -136,9 +134,9 @@ export async function checkSnapshotVersion(version) {
136134 return response . json ( ) ;
137135 }
138136
139- throw new Error ( `[checkSnapshotVersion] failed with status ${ response . status } ` ) ;
137+ throw new RemoteError ( `[checkSnapshotVersion] failed with status ${ response . status } ` ) ;
140138 } catch ( e ) {
141- throw new SnapshotServiceError ( e . errno ? getConnectivityError ( e . errno ) : e . message ) ;
139+ throw errorHandler ( e ) ;
142140 }
143141}
144142
@@ -171,8 +169,16 @@ export async function resolveSnapshot(domain, environment, component) {
171169 return JSON . stringify ( response . json ( ) , null , 4 ) ;
172170 }
173171
174- throw new Error ( `[resolveSnapshot] failed with status ${ response . status } ` ) ;
172+ throw new RemoteError ( `[resolveSnapshot] failed with status ${ response . status } ` ) ;
175173 } catch ( e ) {
176- throw new SnapshotServiceError ( e . errno ? getConnectivityError ( e . errno ) : e . message ) ;
174+ throw errorHandler ( e ) ;
177175 }
176+ }
177+
178+ function errorHandler ( e ) {
179+ if ( ! ( e instanceof ClientError ) ) {
180+ throw new RemoteError ( e . errno ? `Connection has been refused - ${ e . errno } ` : e . message ) ;
181+ }
182+
183+ throw e ;
178184}
0 commit comments