Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
return function(call, callback) {
var testValues = call.metadata.get('x-grpc-test');
var firstTestValue = undefined;
if (testValues != undefined && testValues.length > 0) {
firstTestValue = testValues[0];
}
// Add more gRPC statuses as needed.
switch (firstTestValue) {
case 'ABORTED':
callback({
code: grpc.status.ABORTED, // 10
message: 'ABORTED',
});
break;
case 'INTERNAL':
callback({
code: grpc.status.INTERNAL, // 13
message: 'INTERNAL',
});
break;
case 'DATA_LOSS':
callback({
code: grpc.status.DATA_LOSS, // 15
message: 'DATA_LOSS',
});
break;
default:
stream.on('error', (error) => {
if (error.code === grpc.status.CANCELLED) {
// Capture error when call is cancelled
return;
}
throw error;
});
this.streams.push(stream);
let post = function (error: Error, result: TxExecution) {
if (error) return reject(error)
// Handle execution reversions
if (result.hasException()) {
// Decode error message if there is one otherwise default
if (result.getResult().getReturn().length === 0) {
return reject(callErrorFromStatus({
code: grpc.status.ABORTED,
metadata: new Metadata(),
details: 'Execution Reverted',
}))
} else {
// Strip first 4 bytes(function signature) the decode as a string
return reject(callErrorFromStatus({
code: grpc.status.ABORTED,
metadata: new Metadata(),
details: coder.rawDecode(['string'], Buffer.from(result.getResult().getReturn_asU8().slice(4)))[0],
}))
}
}
// Meta Data (address, caller, height, etc)
let returnObj: TransactionResult = {
contractAddress: Buffer.from(result.getReceipt().getContractaddress_asU8()).toString('hex').toUpperCase(),
height: result.getHeader().getHeight(),
index: result.getHeader().getIndex(),
hash: Buffer.from(result.getHeader().getTxhash_asU8()).toString('hex').toUpperCase(),
type: result.getHeader().getTxtype(),
result: result.getResult().toObject(),
tx: result.getEnvelope().toObject(),
caller: convert.recApply(convert.bytesTB, result.getEnvelope().getSignatoriesList().map(sig => sig.getAddress_asU8())),
}
if (txe.hasException()) {
// Decode error message if there is one otherwise default
if (result.getReturn().length === 0) {
return reject(
callErrorFromStatus({
code: grpc.status.ABORTED,
metadata: new Metadata(),
details: 'Execution Reverted',
}),
);
} else {
// Strip first 4 bytes(function signature) the decode as a string
return reject(
callErrorFromStatus({
code: grpc.status.ABORTED,
metadata: new Metadata(),
details: coder.decode(['string'], Buffer.from(result.getReturn_asU8().slice(4)))[0],
}),
);
}
}
// Meta Data (address, caller, height, etc)
const envelope = txe.getEnvelope();
const receipt = txe.getReceipt();
if (!header || !envelope || !receipt) {
return reject(new Error(``));
}
let caller: string | string[] = envelope.getSignatoriesList().map((sig) => sig.getAddress_asB64().toUpperCase());
if (caller.length === 1) {
caller = caller[0];
client.makeUnaryRequest(path, serialize, deserialize, {}, metadata, {}, (error, response) => {
Assert.strictEqual(error.code, Grpc.status.OUT_OF_RANGE);
Assert.strictEqual(error.details, 'Invalid deadline');
barrier.pass();
});
client.makeUnaryRequest(path, serialize, deserialize, {}, metadata, {}, (error, response) => {
Assert.strictEqual(error.code, Grpc.status.DEADLINE_EXCEEDED);
Assert.strictEqual(error.details, 'Deadline exceeded');
barrier.pass();
});
call.on('error', (err) => {
Assert(err);
Assert.strictEqual(err.code, Grpc.status.UNIMPLEMENTED);
barrier.pass();
});
function serverError (err) {
return {
code: grpc.status.UNKNOWN,
message: err.toString()
}
}
stream.on('error', (err: Error) => err.code === grpc.status.CANCELLED || callback(err, null));
return stream;
function doEchoAbort(call, callback) {
callback({
code: grpc.status.ABORTED,
message: 'Aborted from server side.'
});
}