mirror of
https://github.com/datarhei/core.git
synced 2025-09-27 12:22:28 +08:00
Fix some error types
This commit is contained in:
@@ -2,7 +2,6 @@ package client
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"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) {
|
func (c *APIClient) stream(method, path, contentType string, data io.Reader, origin string) (io.ReadCloser, error) {
|
||||||
if len(c.Address) == 0 {
|
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
|
address := "http://" + c.Address + path
|
||||||
|
|
||||||
req, err := http.NewRequest(method, address, data)
|
req, err := http.NewRequest(method, address, data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, newError(http.StatusInternalServerError, err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
req.Header.Add("X-Cluster-Origin", origin)
|
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)
|
status, body, err := c.request(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, newError(http.StatusInternalServerError, err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
if status < 200 || status >= 300 {
|
if status < 200 || status >= 300 {
|
||||||
@@ -311,6 +310,15 @@ type Error struct {
|
|||||||
Details []string `json:"details" jsonschema:""`
|
Details []string `json:"details" jsonschema:""`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func newError(code int, details string) *Error {
|
||||||
|
return &Error{
|
||||||
|
Code: code,
|
||||||
|
Details: []string{
|
||||||
|
details,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (e Error) Error() string {
|
func (e Error) Error() string {
|
||||||
return strings.Join(e.Details, ", ")
|
return strings.Join(e.Details, ", ")
|
||||||
}
|
}
|
||||||
|
@@ -185,6 +185,7 @@ type cluster struct {
|
|||||||
|
|
||||||
var ErrDegraded = errors.New("cluster is currently degraded")
|
var ErrDegraded = errors.New("cluster is currently degraded")
|
||||||
var ErrUnknownNode = errors.New("unknown node id")
|
var ErrUnknownNode = errors.New("unknown node id")
|
||||||
|
var ErrUnknownProcess = errors.New("unknown process id")
|
||||||
|
|
||||||
func New(config Config) (Cluster, error) {
|
func New(config Config) (Cluster, error) {
|
||||||
c := &cluster{
|
c := &cluster{
|
||||||
|
@@ -155,7 +155,7 @@ func (p *Manager) NodeGet(id string) (*Node, error) {
|
|||||||
|
|
||||||
node, ok := p.nodes[id]
|
node, ok := p.nodes[id]
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("node not found: %s", id)
|
return nil, fmt.Errorf("%s: %w", id, ErrNodeNotFound)
|
||||||
}
|
}
|
||||||
|
|
||||||
return node, nil
|
return node, nil
|
||||||
|
@@ -211,7 +211,6 @@ func (h *ClusterHandler) ProcessGet(c echo.Context) error {
|
|||||||
pid := app.NewProcessID(id, domain)
|
pid := app.NewProcessID(id, domain)
|
||||||
|
|
||||||
// Check the store for the process
|
// 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)
|
p, nodeid, err := h.cluster.ProcessGet("", pid, false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return api.Err(http.StatusNotFound, "", "process not found: %s in domain '%s'", pid.ID, pid.Domain)
|
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 err := h.cluster.ProcessSetCommand("", pid, command.Command); err != nil {
|
||||||
if cerr, ok := err.(api.Error); ok {
|
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())
|
return api.Err(http.StatusNotFound, "", "command failed: %s", err.Error())
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user