@@ -6,7 +6,7 @@ import { ProgrammerIsRequiredForUploadError } from './cli-protocol/cc/arduino/cl
6
6
type ProtoError = typeof ProgrammerIsRequiredForUploadError ;
7
7
const protoErrorsMap = new Map < string , ProtoError > ( [
8
8
[
9
- 'type.googleapis.com/ cc.arduino.cli.commands.v1.ProgrammerIsRequiredForUploadError' ,
9
+ 'cc.arduino.cli.commands.v1.ProgrammerIsRequiredForUploadError' ,
10
10
ProgrammerIsRequiredForUploadError ,
11
11
] ,
12
12
// handle other cli defined errors here
@@ -22,30 +22,33 @@ export namespace ServiceError {
22
22
return arg instanceof Error && isStatusObject ( arg ) ;
23
23
}
24
24
25
- export function isInstanceOf ( arg : unknown , type : unknown ) : boolean {
25
+ export function isInstanceOf < ProtoError > (
26
+ arg : unknown ,
27
+ type : new ( ...args : unknown [ ] ) => ProtoError
28
+ ) : arg is ProtoError {
26
29
if ( ! isStatusObject ( arg ) ) {
27
30
return false ;
28
31
}
29
32
30
- const bin = arg . metadata . get ( 'grpc-status-details-bin' ) [ 0 ] ;
33
+ try {
34
+ const bin = arg . metadata . get ( 'grpc-status-details-bin' ) [ 0 ] ;
35
+ const uint8Array =
36
+ typeof bin === 'string'
37
+ ? stringToUint8Array ( bin )
38
+ : new Uint8Array ( bin . buffer , bin . byteOffset , bin . byteLength ) ;
31
39
32
- const uint8Array =
33
- typeof bin === 'string'
34
- ? stringToUint8Array ( bin )
35
- : new Uint8Array ( bin . buffer , bin . byteOffset , bin . byteLength ) ;
40
+ const errors = Status . deserializeBinary ( uint8Array )
41
+ . getDetailsList ( )
42
+ . map ( ( details ) => {
43
+ const typeName = details . getTypeName ( ) ;
44
+ const ErrorType = protoErrorsMap . get ( typeName ) ;
45
+ return ErrorType ?. deserializeBinary ( details . getValue_asU8 ( ) ) ;
46
+ } ) ;
36
47
37
- const errors = Status . deserializeBinary ( uint8Array )
38
- . getDetailsList ( )
39
- . map ( ( details ) => {
40
- const typeUrl = details . getTypeUrl ( ) ;
41
- const ErrorType = protoErrorsMap . get ( typeUrl ) ;
42
- return ErrorType ?. deserializeBinary ( details . getValue_asU8 ( ) ) ;
43
- } ) ;
44
-
45
- return ! ! errors . find ( ( error ) => {
46
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
47
- return error && error instanceof < any > type ;
48
- } ) ;
48
+ return ! ! errors . find ( ( error ) => error && error instanceof type ) ;
49
+ } catch {
50
+ return false ;
51
+ }
49
52
}
50
53
51
54
function isStatusObject ( arg : unknown ) : arg is StatusObject {
0 commit comments