diff --git a/cluster/client/client.go b/cluster/client/client.go index 214bf34d..b107ba5b 100644 --- a/cluster/client/client.go +++ b/cluster/client/client.go @@ -2,7 +2,6 @@ package client import ( "bytes" - "fmt" "io" "net/http" "net/url" @@ -235,14 +234,14 @@ func (c *APIClient) Snapshot(origin string) (io.ReadCloser, error) { func (c *APIClient) stream(method, path, contentType string, data io.Reader, origin string) (io.ReadCloser, error) { if len(c.Address) == 0 { - return nil, fmt.Errorf("no address defined") + return nil, newError(http.StatusInternalServerError, "no address defined") } address := "http://" + c.Address + path req, err := http.NewRequest(method, address, data) if err != nil { - return nil, err + return nil, newError(http.StatusInternalServerError, err.Error()) } req.Header.Add("X-Cluster-Origin", origin) @@ -253,7 +252,7 @@ func (c *APIClient) stream(method, path, contentType string, data io.Reader, ori status, body, err := c.request(req) if err != nil { - return nil, err + return nil, newError(http.StatusInternalServerError, err.Error()) } if status < 200 || status >= 300 { @@ -311,6 +310,15 @@ type Error struct { Details []string `json:"details" jsonschema:""` } +func newError(code int, details string) *Error { + return &Error{ + Code: code, + Details: []string{ + details, + }, + } +} + func (e Error) Error() string { return strings.Join(e.Details, ", ") } diff --git a/cluster/cluster.go b/cluster/cluster.go index ccb9e65c..a1f6a83b 100644 --- a/cluster/cluster.go +++ b/cluster/cluster.go @@ -185,6 +185,7 @@ type cluster struct { var ErrDegraded = errors.New("cluster is currently degraded") var ErrUnknownNode = errors.New("unknown node id") +var ErrUnknownProcess = errors.New("unknown process id") func New(config Config) (Cluster, error) { c := &cluster{ diff --git a/cluster/node/manager.go b/cluster/node/manager.go index d9eefc3e..53ad6428 100644 --- a/cluster/node/manager.go +++ b/cluster/node/manager.go @@ -155,7 +155,7 @@ func (p *Manager) NodeGet(id string) (*Node, error) { node, ok := p.nodes[id] if !ok { - return nil, fmt.Errorf("node not found: %s", id) + return nil, fmt.Errorf("%s: %w", id, ErrNodeNotFound) } return node, nil diff --git a/http/handler/api/cluster_process.go b/http/handler/api/cluster_process.go index f470d243..f8271293 100644 --- a/http/handler/api/cluster_process.go +++ b/http/handler/api/cluster_process.go @@ -211,7 +211,6 @@ func (h *ClusterHandler) ProcessGet(c echo.Context) error { pid := app.NewProcessID(id, domain) // Check the store for the process - // TODO: should check the leader because in larger cluster it needs time to get to all followers p, nodeid, err := h.cluster.ProcessGet("", pid, false) if err != nil { return api.Err(http.StatusNotFound, "", "process not found: %s in domain '%s'", pid.ID, pid.Domain) @@ -414,7 +413,7 @@ func (h *ClusterHandler) ProcessSetCommand(c echo.Context) error { if err := h.cluster.ProcessSetCommand("", pid, command.Command); err != nil { if cerr, ok := err.(api.Error); ok { - return api.Err(cerr.Code, "", "comm failed: %s", cerr.Error()) + return cerr } return api.Err(http.StatusNotFound, "", "command failed: %s", err.Error()) }