Skip to content

Commit

Permalink
fix(hubl): improve error in chain (#18550)
Browse files Browse the repository at this point in the history
  • Loading branch information
julienrbrt authored Nov 27, 2023
1 parent 34b1634 commit 8a2c908
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 14 deletions.
28 changes: 15 additions & 13 deletions tools/hubl/internal/compat.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func loadFileDescriptorsGRPCReflection(ctx context.Context, client *grpc.ClientC
}

fdMap := map[string]*descriptorpb.FileDescriptorProto{}
waitListServiceRes := make(chan *grpc_reflection_v1alpha.ListServiceResponse)
waitListServiceRes := make(chan *grpc_reflection_v1alpha.ListServiceResponse) //nolint:staticcheck // we want to use the deprecated field
waitc := make(chan struct{})
go func() {
for {
Expand All @@ -60,25 +60,25 @@ func loadFileDescriptorsGRPCReflection(ctx context.Context, client *grpc.ClientC
case *grpc_reflection_v1alpha.ServerReflectionResponse_ErrorResponse:
panic(err)
case *grpc_reflection_v1alpha.ServerReflectionResponse_ListServicesResponse:
waitListServiceRes <- res.ListServicesResponse
waitListServiceRes <- res.ListServicesResponse //nolint:staticcheck // we want to use the deprecated field
case *grpc_reflection_v1alpha.ServerReflectionResponse_FileDescriptorResponse:
processFileDescriptorsResponse(res, fdMap)
_ = processFileDescriptorsResponse(res, fdMap)
}
}
}()

if err = reflectClient.Send(&grpc_reflection_v1alpha.ServerReflectionRequest{
if err = reflectClient.Send(&grpc_reflection_v1alpha.ServerReflectionRequest{ //nolint:staticcheck // we want to use the deprecated field
MessageRequest: &grpc_reflection_v1alpha.ServerReflectionRequest_ListServices{},
}); err != nil {
return nil, err
}

listServiceRes := <-waitListServiceRes

for _, response := range listServiceRes.Service {
err = reflectClient.Send(&grpc_reflection_v1alpha.ServerReflectionRequest{
for _, response := range listServiceRes.Service { //nolint:staticcheck // we want to use the deprecated field
err = reflectClient.Send(&grpc_reflection_v1alpha.ServerReflectionRequest{ //nolint:staticcheck // we want to use the deprecated field
MessageRequest: &grpc_reflection_v1alpha.ServerReflectionRequest_FileContainingSymbol{
FileContainingSymbol: response.Name,
FileContainingSymbol: response.Name, //nolint:staticcheck // we want to use the deprecated field
},
})
if err != nil {
Expand All @@ -87,7 +87,7 @@ func loadFileDescriptorsGRPCReflection(ctx context.Context, client *grpc.ClientC
}

for _, msgName := range interfaceImplNames {
err = reflectClient.Send(&grpc_reflection_v1alpha.ServerReflectionRequest{
err = reflectClient.Send(&grpc_reflection_v1alpha.ServerReflectionRequest{ //nolint:staticcheck // we want to use the deprecated field
MessageRequest: &grpc_reflection_v1alpha.ServerReflectionRequest_FileContainingSymbol{
FileContainingSymbol: msgName,
},
Expand Down Expand Up @@ -136,16 +136,18 @@ func loadFileDescriptorsGRPCReflection(ctx context.Context, client *grpc.ClientC
return fdSet, nil
}

func processFileDescriptorsResponse(res *grpc_reflection_v1alpha.ServerReflectionResponse_FileDescriptorResponse, fdMap map[string]*descriptorpb.FileDescriptorProto) {
for _, bz := range res.FileDescriptorResponse.FileDescriptorProto {
func processFileDescriptorsResponse(res *grpc_reflection_v1alpha.ServerReflectionResponse_FileDescriptorResponse, fdMap map[string]*descriptorpb.FileDescriptorProto) error {
for _, bz := range res.FileDescriptorResponse.FileDescriptorProto { //nolint:staticcheck // we want to use the deprecated field
fd := &descriptorpb.FileDescriptorProto{}
err := proto.Unmarshal(bz, fd)
if err != nil {
panic(err)
return fmt.Errorf("error unmarshalling file descriptor: %w", err)
}

fdMap[fd.GetName()] = fd
}

return nil
}

func missingFileDescriptors(fdMap map[string]*descriptorpb.FileDescriptorProto, cantFind map[string]bool) []string {
Expand Down Expand Up @@ -180,13 +182,13 @@ func addMissingFileDescriptors(ctx context.Context, client *grpc.ClientConn, fdM
}

if res, ok := in.MessageResponse.(*grpc_reflection_v1alpha.ServerReflectionResponse_FileDescriptorResponse); ok {
processFileDescriptorsResponse(res, fdMap)
_ = processFileDescriptorsResponse(res, fdMap)
}
}
}()

for _, file := range missingFiles {
err = reflectClient.Send(&grpc_reflection_v1alpha.ServerReflectionRequest{
err = reflectClient.Send(&grpc_reflection_v1alpha.ServerReflectionRequest{ //nolint:staticcheck // we want to use the deprecated field
MessageRequest: &grpc_reflection_v1alpha.ServerReflectionRequest_FileByFilename{
FileByFilename: file,
},
Expand Down
12 changes: 11 additions & 1 deletion tools/hubl/internal/remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,17 @@ func RemoteCommand(config *config.Config, configDir string) ([]*cobra.Command, e
chainCmd.AddCommand(KeyringCmd(chainInfo.Chain))

if err := appOpts.EnhanceRootCommandWithBuilder(chainCmd, builder); err != nil {
return nil, err
// when enriching the command with autocli fails, we add a command that
// will print the error and allow the user to reconfigure the chain instead
chainCmd.RunE = func(cmd *cobra.Command, args []string) error {
cmd.Printf("Error while loading AutoCLI data for %s: %+v\n", chain, err)
cmd.Printf("Attempt to reconfigure the chain using the %s flag\n", flags.FlagConfig)
if cmd.Flags().Changed(flags.FlagConfig) {
return reconfigure(cmd, config, configDir, chain)
}

return nil
}
}

commands = append(commands, chainCmd)
Expand Down

0 comments on commit 8a2c908

Please sign in to comment.