-
Notifications
You must be signed in to change notification settings - Fork 93
/
Copy pathgatewayerror.ts
90 lines (81 loc) · 2.64 KB
/
gatewayerror.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*
* Copyright 2021 IBM All Rights Reserved.
*
* SPDX-License-Identifier: Apache-2.0
*/
import { ServiceError } from '@grpc/grpc-js';
import { gateway, google } from '@hyperledger/fabric-protos';
/**
* ErrorDetail contains the details of an error generated by an endorsing peer or ordering node.
* It contains the address of the node as well as the error message it generated.
*/
export interface ErrorDetail {
/**
* Fabric node endpoint address.
*/
address: string;
/**
* Error message returned by the node.
*/
message: string;
/**
* Member services provider to which the node is associated.
*/
mspId: string;
}
/**
* A GatewayError is thrown if an error is encountered while processing a transaction through the gateway.
* Since the gateway delegates much of the processing to other nodes (endorsing peers and orderers), then
* the error could have originated from one or more of those nodes. In that case, the details field will
* contain an array of ErrorDetail objects.
*/
export class GatewayError extends Error {
/**
* gRPC status code.
* @see [https://grpc.io/docs/guides/status-codes/](https://grpc.io/docs/guides/status-codes/)
* for descriptions of status codes.
*/
code: number;
/**
* gRPC error details.
*/
details: ErrorDetail[];
/**
* Raw underlying gRPC [ServiceError](https://grpc.github.io/grpc/node/grpc.html#~ServiceError).
*/
cause: ServiceError;
constructor(
properties: Readonly<{
code: number;
details: ErrorDetail[];
cause: ServiceError;
message?: string;
}>,
) {
super(properties.message);
this.name = GatewayError.name;
this.code = properties.code;
this.details = properties.details;
this.cause = properties.cause;
}
}
export function newGatewayError(err: ServiceError): GatewayError {
const metadata = err.metadata.get('grpc-status-details-bin');
const details = metadata
.flatMap((metadataValue) => google.rpc.Status.deserializeBinary(Buffer.from(metadataValue)).getDetailsList())
.map((statusDetail) => {
const endpointError = gateway.ErrorDetail.deserializeBinary(statusDetail.getValue_asU8());
const detail: ErrorDetail = {
address: endpointError.getAddress(),
message: endpointError.getMessage(),
mspId: endpointError.getMspId(),
};
return detail;
});
return new GatewayError({
message: err.message,
code: err.code,
details,
cause: err,
});
}