github.com/turbot/steampipe@v1.7.0-rc.0.0.20240517123944-7cef272d4458/pkg/pluginmanager_service/grpc/start_failure.go (about)

     1  package grpc
     2  
     3  import (
     4  	sdkplugin "github.com/turbot/steampipe-plugin-sdk/v5/plugin"
     5  	"github.com/turbot/steampipe-plugin-sdk/v5/sperr"
     6  	"strings"
     7  )
     8  
     9  // HandleStartFailure is used to handle errors when starting both Steampipe plugins an dthe plugin manage
    10  // (which is itself a GRPC plugin)
    11  //
    12  // When starting a GRPC plugin, a specific handshake sequence is expected on stdout.
    13  // (This is automatically written in the case of a successfulty startup)
    14  // If the handshae is missing (because the startup failed or anything else was written to stdout)
    15  // we get the error "Unrecognized remote plugin message"
    16  //
    17  // If the plugin startup fails with an error panic, it constructs a message string
    18  // starting with the prefix  "Plugin startup failed: " , detailing the error.
    19  //
    20  // This function checks whether the error returned from startup is "Unrecognized remote plugin message",
    21  // and if so, it looks for ""Plugin startup failed: " in the plugin message and if found,
    22  // extracts the underlying error message. This is returnerd as an error
    23  func HandleStartFailure(err error) error {
    24  	// extract the plugin message
    25  	_, pluginMessage, found := strings.Cut(err.Error(), sdkplugin.UnrecognizedRemotePluginMessage)
    26  	if !found {
    27  		return err
    28  	}
    29  	pluginMessage, _, found = strings.Cut(pluginMessage, sdkplugin.UnrecognizedRemotePluginMessageSuffix)
    30  	if !found {
    31  		return err
    32  	}
    33  
    34  	// if this was an error during startup, reraise an error with the error string
    35  	_, pluginError, found := strings.Cut(pluginMessage, sdkplugin.PluginStartupFailureMessage)
    36  	if !found {
    37  		return err
    38  	}
    39  
    40  	if strings.Contains(pluginMessage, sdkplugin.PluginStartupFailureMessage) {
    41  		return sperr.New(pluginError)
    42  	}
    43  	return err
    44  }