11import type { ServerSurfaceCall } from '@grpc/grpc-js/build/src/server-call' ;
2- import type { Class } from '@matrixai/errors ' ;
3- import type { Host , Port } from 'network/types' ;
4- import type Proxy from 'network/Proxy' ;
5- import type { ConnectionInfoGet } from './types' ;
2+ import type ErrorPolykey from '../ErrorPolykey ' ;
3+ import type { Host , Port } from '../ network/types' ;
4+ import type Proxy from '../ network/Proxy' ;
5+ import type { ConnectionInfoGet , AgentClientErrors } from './types' ;
66import * as validationErrors from '../validation/errors' ;
77
88/**
99 * Array of errors that are always considered to be "client errors"
1010 * (4xx errors in HTTP) in the context of the agent service
1111 */
12- const defaultClientErrors : Array < Class < Error > > = [
12+ const defaultClientErrors : AgentClientErrors = [
1313 validationErrors . ErrorValidation ,
1414] ;
1515
@@ -33,19 +33,53 @@ function connectionInfoGetter(proxy: Proxy): ConnectionInfoGet {
3333 * context of a given handler can be supplied in the `extraClientErrors`
3434 * argument
3535 */
36- function isClientError (
37- e : Error ,
38- extraClientErrors ?: Array < Class < Error > > ,
36+ function isAgentClientError (
37+ thrownError : ErrorPolykey < any > ,
38+ extraClientErrors ?: AgentClientErrors ,
3939) : boolean {
4040 for ( const error of defaultClientErrors ) {
41- if ( e instanceof error ) return true ;
41+ if ( Array . isArray ( error ) ) {
42+ let e = thrownError ;
43+ let matches = true ;
44+ for ( const eType of error ) {
45+ if ( e == null ) {
46+ matches = false ;
47+ break ;
48+ }
49+ if ( ! ( e instanceof eType ) ) {
50+ matches = false ;
51+ break ;
52+ }
53+ e = e . cause ;
54+ }
55+ if ( matches ) return true ;
56+ } else if ( thrownError instanceof error ) {
57+ return true ;
58+ }
4259 }
4360 if ( extraClientErrors ) {
4461 for ( const error of extraClientErrors ) {
45- if ( e instanceof error ) return true ;
62+ if ( Array . isArray ( error ) ) {
63+ let e = thrownError ;
64+ let matches = true ;
65+ for ( const eType of error ) {
66+ if ( e == null ) {
67+ matches = false ;
68+ break ;
69+ }
70+ if ( ! ( e instanceof eType ) ) {
71+ matches = false ;
72+ break ;
73+ }
74+ e = e . cause ;
75+ }
76+ if ( matches ) return true ;
77+ } else if ( thrownError instanceof error ) {
78+ return true ;
79+ }
4680 }
4781 }
4882 return false ;
4983}
5084
51- export { connectionInfoGetter , isClientError } ;
85+ export { connectionInfoGetter , isAgentClientError } ;
0 commit comments