Skip to content

Commit

Permalink
Handle errors from streaming gRPC call
Browse files Browse the repository at this point in the history
  • Loading branch information
thpani committed Aug 1, 2023
1 parent 1a8cc1b commit 06fc66a
Showing 1 changed file with 29 additions and 41 deletions.
70 changes: 29 additions & 41 deletions quint/src/quintVerifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,56 +225,44 @@ async function loadProtoDefViaReflection(): Promise<VerifyResult<ProtoPackageDef
const serverReflectionService = reflectionProtoDescriptor.grpc.reflection.v1alpha.ServerReflection
const reflectionClient = new serverReflectionService(APALACHE_SERVER_URI, grpc.credentials.createInsecure())

const protoDefResponses: ServerReflectionResponse[] = await new Promise((resolve, reject) => {
// Query reflection endpoint
const call = reflectionClient.ServerReflectionInfo()

// Query reflection endpoint
const response: VerifyResult<ServerReflectionResponse[]> = await new Promise<ServerReflectionResponse[]>((resolve, reject) => {
let responses: ServerReflectionResponse[] = []

call.on('data', (reflectionResponse: ServerReflectionResponse) => {
call.cancel()
responses.push(reflectionResponse)
})

call.on('error', (e: any) => {
if (e.code === grpc.status.CANCELLED) {
// Cancelled by us, so we have the response
resolve(responses)
} else {
// Ignore cancellation errors
// An error has occurred and the stream has been closed.
console.error(e)
reject(e)
}
})
const call = reflectionClient.ServerReflectionInfo()
call.on('data', (r: ServerReflectionResponse) => responses.push(r))
call.on('end', () => resolve(responses))
call.on('error', (e: grpc.StatusObject) => reject(e))

call.write({ file_containing_symbol: 'shai.cmdExecutor.CmdExecutor' })
})
}).then(right).catch(e => err(`Apalache gRPC endpoint is corrupted. Error reading from streaming API: ${e.details}`))

return response.chain((protoDefResponses: ServerReflectionResponse[]) => {
if (protoDefResponses.length === 0) {
return err(`Apalache gRPC endpoint is corrupted. Could not extract proto file: no response from reflection service`)
} else if (protoDefResponses.length > 1) {
return err(
`Apalache gRPC endpoint is corrupted. Could not extract proto file: multiple responses from reflection service`
)
}

if (protoDefResponses.length === 0) {
return err(`Apalache gRPC endpoint is corrupted. Could not extract proto file: no response from reflection service`)
} else if (protoDefResponses.length > 1) {
return err(
`Apalache gRPC endpoint is corrupted. Could not extract proto file: multiple responses from reflection service`
)
}
// We have a single response
const protoDefResponse = protoDefResponses[0]

// We have a single response
const protoDefResponse = protoDefResponses[0]
if ('error_response' in protoDefResponse) {
return err(
`Apalache gRPC endpoint is corrupted. Could not extract proto file: ${protoDefResponse.error_response.error_message}`
)
}

if ('error_response' in protoDefResponse) {
return err(
`Apalache gRPC endpoint is corrupted. Could not extract proto file: ${protoDefResponse.error_response.error_message}`
// Decode reflection response to FileDescriptorProto
let fileDescriptorProtos = protoDefResponse.file_descriptor_response.file_descriptor_proto.map(
bytes => protobufDescriptor.FileDescriptorProto.decode(bytes) as protobufDescriptor.IFileDescriptorProto
)
}

// Decode reflection response to FileDescriptorProto
let fileDescriptorProtos = protoDefResponse.file_descriptor_response.file_descriptor_proto.map(
bytes => protobufDescriptor.FileDescriptorProto.decode(bytes) as protobufDescriptor.IFileDescriptorProto
)

// Use proto-loader to load the FileDescriptorProto wrapped in a FileDescriptorSet
return right(proto.loadFileDescriptorSetFromObject({ file: fileDescriptorProtos }, grpcStubOptions))
// Use proto-loader to load the FileDescriptorProto wrapped in a FileDescriptorSet
return right(proto.loadFileDescriptorSetFromObject({ file: fileDescriptorProtos }, grpcStubOptions))
})
}

function loadProtoDefViaDistribution(dist: ApalacheDist): VerifyResult<ProtoPackageDefinition> {
Expand Down

0 comments on commit 06fc66a

Please sign in to comment.