Fix some error types

This commit is contained in:
Ingo Oppermann
2025-04-16 15:07:34 +02:00
parent 2fbb2b1ab3
commit ae157fbacf
4 changed files with 15 additions and 7 deletions

View File

@@ -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, ", ")
}

View File

@@ -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{

View File

@@ -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

View File

@@ -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())
}