Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 29 additions & 19 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,16 @@ export class Client {
options?: object,
metadata?: Metadata
) {
if (!url) {
throw new Error('Error: url is required for server reflection');
}

if (!credentials) {
throw new Error(
'Error: credentials parameter is required for server reflection'
);
}

Comment on lines +60 to +69
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not have this error handled and caught in the supported API evaluation function? Because we want the consumer of this library to know even before they reach the stage of calling the reflection API.

If the constructor crashes at initialization, hence notifying the consumer of the problem in their implementation. The listServices function will never be invoked in the first place.

This mimics the behaviour we had earlier.

this.url = url;
this.credentials = credentials;
this.clientOptions = options;
Expand Down Expand Up @@ -114,27 +124,27 @@ export class Client {
supportedReflectionAPIVersions[
version as keyof typeof supportedReflectionAPIVersions
];
const {
service: servicePromise,
client: clientPromise,
} = protocolConfig;

const [protocolService, protocolClient] = await Promise.all([
servicePromise,
clientPromise,
]);

const grpcClientForProtocol = new protocolService.ServerReflectionClient(
this.url,
this.credentials,
this.clientOptions
);

const request = new protocolClient.ServerReflectionRequest();
try {
const {service: servicePromise, client: clientPromise} =
protocolConfig;

const [protocolService, protocolClient] = await Promise.all([
servicePromise,
clientPromise,
]);

const grpcClientForProtocol =
new protocolService.ServerReflectionClient(
this.url,
this.credentials,
this.clientOptions
);

request.setListServices('*');
const request = new protocolClient.ServerReflectionRequest();

request.setListServices('*');

try {
const [reflectionResponse] = await this.sendReflectionRequest(
request,
grpcClientForProtocol
Expand Down Expand Up @@ -174,7 +184,7 @@ export class Client {
});

const resultWithServiceError = evaluationResults.find(res => {
// Something is actually wrong with the gRPC service
// Something is actually wrong with the gRPC service or client
return res.error && res.error.code !== GrpcStatus.UNIMPLEMENTED;
});

Expand Down