Files
Wilken Rivera 606e6c48f1 internal: reorganise registry/HCP code
This commit reorganises the code for both the registry/API and the
Orchestrator/Registry.

The main difference with the previous version is how stuff is exposed.
Now we only expose a Registry interface to the outside (previously named
Orchestrator), which has several implementations: null is the default,
and is returned if HCP is not enabled.

The other implementations being HCL/JSON, both private to the hcp
sub-package.

The api (previously `registry') is the set of functionality that
abstracts and calls the HCP API.
It was meant to be merged with the `hcp' package, but because of a
dependency loop with the datasources, both are separated for now.
2022-11-14 13:31:35 -05:00

37 lines
894 B
Go

package api
import (
"fmt"
"strings"
"google.golang.org/grpc/codes"
)
const (
_ = iota
InvalidClientConfig
)
// ClientError represents a generic error for the Cloud Packer Service client.
type ClientError struct {
StatusCode uint
Err error
}
// Error returns the string message for some ClientError.
func (c *ClientError) Error() string {
return fmt.Sprintf("status %d: err %v", c.StatusCode, c.Err)
}
// CheckErrorCode checks the error string for err for some code and returns true
// if the code is found. Ideally this function should use status.FromError
// https://pkg.go.dev/google.golang.org/grpc/status#pkg-functions but that
// doesn't appear to work for all of the Cloud Packer Service response errors.
func CheckErrorCode(err error, code codes.Code) bool {
if err == nil {
return false
}
return strings.Contains(err.Error(), fmt.Sprintf("Code:%d", code))
}