Skip to content

Commit

Permalink
fix: throw err when fail to get service name list (#230)
Browse files Browse the repository at this point in the history
* Throw err when fail to get service name list

* Address the comments: update error message; unit test for New API constructor
  • Loading branch information
summer-ji-eng committed Feb 6, 2020
1 parent 8d73e62 commit 5621dc8
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
1 change: 1 addition & 0 deletions typescript/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,5 @@ async function main() {

main().catch(err => {
console.error(err);
process.exit(1);
});
5 changes: 5 additions & 0 deletions typescript/src/schema/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@ export class API {
this.port = port ?? this.port ?? '443';
serviceNamesList.push(service.name || this.naming.name);
});
if (serviceNamesList.length === 0) {
throw new Error(
`Can't find ${this.naming.name}'s service names, please make sure that services are defined in the proto file.`
);
}
this.mainServiceName = options.mainServiceName || serviceNamesList[0];
}

Expand Down
20 changes: 20 additions & 0 deletions typescript/test/unit/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ describe('schema/api.ts', () => {
fd.name = 'google/cloud/test/v1/test.proto';
fd.package = 'google.cloud.test.v1';
fd.service = [new plugin.google.protobuf.ServiceDescriptorProto()];
fd.service[0].name = 'ZService';
fd.service[0].options = {
'.google.api.defaultHost': 'hostname.example.com:443',
};
const api = new API([fd], 'google.cloud.test.v1', {
grpcServiceConfig: new plugin.grpc.service_config.ServiceConfig(),
});
Expand All @@ -36,6 +40,10 @@ describe('schema/api.ts', () => {
fd1.name = 'google/cloud/test/v1/test.proto';
fd1.package = 'google.cloud.test.v1';
fd1.service = [new plugin.google.protobuf.ServiceDescriptorProto()];
fd1.service[0].name = 'ZService';
fd1.service[0].options = {
'.google.api.defaultHost': 'hostname.example.com:443',
};
const fd2 = new plugin.google.protobuf.FileDescriptorProto();
fd2.name = 'google/longrunning/operation.proto';
fd2.package = 'google.longrunning';
Expand Down Expand Up @@ -132,4 +140,16 @@ describe('schema/api.ts', () => {
'../../protos/google/cloud/example/v1/test.proto',
]);
});

it('should throw error when the service name is not found', () => {
const fd = new plugin.google.protobuf.FileDescriptorProto();
fd.name = 'google/cloud/test/v1/test.proto';
fd.package = 'google.cloud.test.v1';
fd.service = [new plugin.google.protobuf.ServiceDescriptorProto()];
assert.throws(() => {
const api = new API([fd], 'google.cloud.test.v1', {
grpcServiceConfig: new plugin.grpc.service_config.ServiceConfig(),
});
});
});
});

0 comments on commit 5621dc8

Please sign in to comment.