mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-06 19:17:28 +01:00
sw-block, volume: errors are the JSON error body on stdout
ops chunk failures print {code,message,retryable} on stdout, the same body
the routes answer (relayed as-is for an HTTP error), with a log line on
stderr and a non-zero exit (2 for usage errors) (B9 W2 appendix,
2026-11-15 14:30Z). CLI-side failures use the routes' codes: bad_request
for usage, remote_unavailable (retryable) when blockvolume or the filer
cannot be reached, internal otherwise; catalog refusals keep their code.
Messages no longer repeat the code: CatalogError.Detail is the message
and cause without it, used by the routes and the CLI.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
f947bd4063
commit
341c80b2fb
5 files changed
+153
-73
No files matched your search
@@ -4,8 +4,9 @@ package main
|
||||
// cut, snapshot and detach call a blockvolume's /chunk/* routes
|
||||
// (--status-addr). snapshots, snapshot-delete and fork read and write the
|
||||
// filer directly (--chunk-filer, --chunk-path), so they work while the
|
||||
// volume is offline. Each prints the operation's JSON body on stdout; a
|
||||
// failure prints "<code>: <message> (retryable=...)" on stderr and exits 1.
|
||||
// volume is offline. Each prints the operation's JSON body on stdout. A
|
||||
// failure prints the routes' error body {code,message,retryable} on stdout
|
||||
// and a log line on stderr, and exits non-zero (2 for usage errors).
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
@@ -42,8 +43,7 @@ var dialChunkFiler = func(addr string) (chunkstore.Filer, func() error, error) {
|
||||
|
||||
func runOpsChunk(args []string, stdout, stderr io.Writer) int {
|
||||
if len(args) == 0 {
|
||||
fmt.Fprintln(stderr, "sw-block ops chunk: expected cut|snapshot|snapshots|snapshot-delete|fork|detach")
|
||||
return ops.VolumeStatusExitInvalid
|
||||
return chunkFail("", stdout, stderr, ops.VolumeStatusExitInvalid, codeBadRequestCLI, "expected cut|snapshot|snapshots|snapshot-delete|fork|detach|show", false)
|
||||
}
|
||||
switch args[0] {
|
||||
case "cut":
|
||||
@@ -59,8 +59,7 @@ func runOpsChunk(args []string, stdout, stderr io.Writer) int {
|
||||
case "fork":
|
||||
return chunkFork(args[1:], stdout, stderr)
|
||||
default:
|
||||
fmt.Fprintf(stderr, "sw-block ops chunk: unknown subcommand %q\n", args[0])
|
||||
return ops.VolumeStatusExitInvalid
|
||||
return chunkFail("", stdout, stderr, ops.VolumeStatusExitInvalid, codeBadRequestCLI, fmt.Sprintf("unknown subcommand %q", args[0]), false)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -100,33 +99,29 @@ func chunkPost(name, statusAddr, route string, timeout time.Duration, body map[s
|
||||
body["timeoutMs"] = timeout.Milliseconds()
|
||||
data, err := json.Marshal(body)
|
||||
if err != nil {
|
||||
fmt.Fprintf(stderr, "sw-block ops chunk %s: %v\n", name, err)
|
||||
return chunkExitFailed
|
||||
return chunkFail(name, stdout, stderr, chunkExitFailed, codeInternalCLI, err.Error(), false)
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(context.Background(), timeout+5*time.Second)
|
||||
defer cancel()
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodPost, strings.TrimRight(base, "/")+route, bytes.NewReader(data))
|
||||
if err != nil {
|
||||
fmt.Fprintf(stderr, "sw-block ops chunk %s: %v\n", name, err)
|
||||
return ops.VolumeStatusExitInvalid
|
||||
return chunkFail(name, stdout, stderr, ops.VolumeStatusExitInvalid, codeBadRequestCLI, err.Error(), false)
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
fmt.Fprintf(stderr, "sw-block ops chunk %s: %v\n", name, err)
|
||||
return chunkExitFailed
|
||||
return chunkFail(name, stdout, stderr, chunkExitFailed, codeUnavailableCLI, "blockvolume not reachable: "+err.Error(), true)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
out, err := io.ReadAll(io.LimitReader(resp.Body, chunkResponseLimit+1))
|
||||
if err != nil {
|
||||
fmt.Fprintf(stderr, "sw-block ops chunk %s: read response: %v\n", name, err)
|
||||
return chunkExitFailed
|
||||
return chunkFail(name, stdout, stderr, chunkExitFailed, codeUnavailableCLI, "read the response: "+err.Error(), true)
|
||||
}
|
||||
if int64(len(out)) > chunkResponseLimit {
|
||||
// Refuse rather than relay a truncated body; the operation may have
|
||||
// happened, and its result is what would be lost.
|
||||
fmt.Fprintf(stderr, "sw-block ops chunk %s: HTTP %d response exceeds %d bytes; not relaying a truncated body\n", name, resp.StatusCode, chunkResponseLimit)
|
||||
return chunkExitFailed
|
||||
return chunkFail(name, stdout, stderr, chunkExitFailed, codeInternalCLI,
|
||||
fmt.Sprintf("HTTP %d response exceeds %d bytes; not relaying a truncated body", resp.StatusCode, chunkResponseLimit), false)
|
||||
}
|
||||
if resp.StatusCode == http.StatusOK {
|
||||
if err := writeAll(stdout, out); err != nil {
|
||||
@@ -135,15 +130,14 @@ func chunkPost(name, statusAddr, route string, timeout time.Duration, body map[s
|
||||
}
|
||||
return ops.VolumeStatusExitOK
|
||||
}
|
||||
var e struct {
|
||||
Code string `json:"code"`
|
||||
Message string `json:"message"`
|
||||
Retryable bool `json:"retryable"`
|
||||
var e chunkErrBody
|
||||
if json.Unmarshal(out, &e) != nil || e.Code == "" {
|
||||
return chunkFail(name, stdout, stderr, chunkExitFailed, codeInternalCLI, fmt.Sprintf("HTTP %d: %s", resp.StatusCode, strings.TrimSpace(string(out))), false)
|
||||
}
|
||||
if json.Unmarshal(out, &e) == nil && e.Code != "" {
|
||||
fmt.Fprintf(stderr, "sw-block ops chunk %s: %s: %s (retryable=%v)\n", name, e.Code, e.Message, e.Retryable)
|
||||
} else {
|
||||
fmt.Fprintf(stderr, "sw-block ops chunk %s: HTTP %d: %s\n", name, resp.StatusCode, strings.TrimSpace(string(out)))
|
||||
// The route's error body is already the CLI's error body: relay it.
|
||||
fmt.Fprintf(stderr, "sw-block ops chunk %s: %s: %s\n", name, e.Code, e.Message)
|
||||
if err := writeAll(stdout, out); err != nil {
|
||||
fmt.Fprintf(stderr, "sw-block ops chunk %s: write the error: %v\n", name, err)
|
||||
}
|
||||
return chunkExitFailed
|
||||
}
|
||||
@@ -160,11 +154,10 @@ func chunkCut(args []string, stdout, stderr io.Writer) int {
|
||||
fs.Uint64Var(&minLSN, "min-lsn", 0, "fail unless the cut reaches this LSN")
|
||||
fs.DurationVar(&timeout, "timeout", 60*time.Second, "operation bound (capped at the server's OpDeadline)")
|
||||
if err := fs.Parse(args); err != nil {
|
||||
return ops.VolumeStatusExitInvalid
|
||||
return chunkFail(chunkName(fs), stdout, stderr, ops.VolumeStatusExitInvalid, codeBadRequestCLI, err.Error(), false)
|
||||
}
|
||||
if volumeID == "" || statusAddr == "" {
|
||||
fmt.Fprintln(stderr, "sw-block ops chunk cut: --volume and --status-addr are required")
|
||||
return ops.VolumeStatusExitInvalid
|
||||
return chunkFail("cut", stdout, stderr, ops.VolumeStatusExitInvalid, codeBadRequestCLI, "--volume and --status-addr are required", false)
|
||||
}
|
||||
return chunkPost("cut", statusAddr, "/chunk/cut", timeout, map[string]any{"volumeID": volumeID, "minLSN": minLSN}, stdout, stderr)
|
||||
}
|
||||
@@ -186,11 +179,10 @@ func chunkSnapshot(args []string, stdout, stderr io.Writer) int {
|
||||
fs.Var(labels, "label", "key=value label (repeatable)")
|
||||
fs.DurationVar(&timeout, "timeout", 60*time.Second, "operation bound (capped at the server's OpDeadline)")
|
||||
if err := fs.Parse(args); err != nil {
|
||||
return ops.VolumeStatusExitInvalid
|
||||
return chunkFail(chunkName(fs), stdout, stderr, ops.VolumeStatusExitInvalid, codeBadRequestCLI, err.Error(), false)
|
||||
}
|
||||
if volumeID == "" || statusAddr == "" || name == "" {
|
||||
fmt.Fprintln(stderr, "sw-block ops chunk snapshot: --volume, --status-addr and --name are required")
|
||||
return ops.VolumeStatusExitInvalid
|
||||
return chunkFail("snapshot", stdout, stderr, ops.VolumeStatusExitInvalid, codeBadRequestCLI, "--volume, --status-addr and --name are required", false)
|
||||
}
|
||||
if requestID == "" {
|
||||
requestID = newRequestID()
|
||||
@@ -210,45 +202,44 @@ func chunkDetach(args []string, stdout, stderr io.Writer) int {
|
||||
fs.StringVar(&statusAddr, "status-addr", "", "blockvolume status address or URL (with --chunk-control-endpoint)")
|
||||
fs.DurationVar(&timeout, "timeout", 120*time.Second, "operation bound (capped at the server's OpDeadline)")
|
||||
if err := fs.Parse(args); err != nil {
|
||||
return ops.VolumeStatusExitInvalid
|
||||
return chunkFail(chunkName(fs), stdout, stderr, ops.VolumeStatusExitInvalid, codeBadRequestCLI, err.Error(), false)
|
||||
}
|
||||
if volumeID == "" || statusAddr == "" {
|
||||
fmt.Fprintln(stderr, "sw-block ops chunk detach: --volume and --status-addr are required")
|
||||
return ops.VolumeStatusExitInvalid
|
||||
return chunkFail("detach", stdout, stderr, ops.VolumeStatusExitInvalid, codeBadRequestCLI, "--volume and --status-addr are required", false)
|
||||
}
|
||||
return chunkPost("detach", statusAddr, "/chunk/detach", timeout, map[string]any{"volumeID": volumeID}, stdout, stderr)
|
||||
}
|
||||
|
||||
// chunkCatalog opens the catalog under --chunk-path on --chunk-filer.
|
||||
func chunkCatalog(name, filer, base string, stderr io.Writer) (*chunkstore.Catalog, func() error, bool) {
|
||||
// It returns exit -1 on success, else the exit code after reporting.
|
||||
func chunkCatalog(name, filer, base string, stdout, stderr io.Writer) (*chunkstore.Catalog, func() error, int) {
|
||||
if filer == "" {
|
||||
fmt.Fprintf(stderr, "sw-block ops chunk %s: --chunk-filer is required\n", name)
|
||||
return nil, nil, false
|
||||
return nil, nil, chunkFail(name, stdout, stderr, ops.VolumeStatusExitInvalid, codeBadRequestCLI, "--chunk-filer is required", false)
|
||||
}
|
||||
f, closeFn, err := dialChunkFiler(filer)
|
||||
if err != nil {
|
||||
fmt.Fprintf(stderr, "sw-block ops chunk %s: %v\n", name, err)
|
||||
return nil, nil, false
|
||||
return nil, nil, chunkFail(name, stdout, stderr, ops.VolumeStatusExitInvalid, codeBadRequestCLI, err.Error(), false)
|
||||
}
|
||||
cat, err := chunkstore.NewCatalog(f, base)
|
||||
if err != nil {
|
||||
_ = closeFn()
|
||||
fmt.Fprintf(stderr, "sw-block ops chunk %s: %v\n", name, err)
|
||||
return nil, nil, false
|
||||
return nil, nil, chunkFail(name, stdout, stderr, ops.VolumeStatusExitInvalid, codeBadRequestCLI, err.Error(), false)
|
||||
}
|
||||
return cat, closeFn, true
|
||||
return cat, closeFn, -1
|
||||
}
|
||||
|
||||
// chunkResult prints v as JSON, or err as "<code>: <message>".
|
||||
func chunkResult(name string, v any, err error, stdout, stderr io.Writer) int {
|
||||
if err != nil {
|
||||
var ce *chunkstore.CatalogError
|
||||
if errors.As(err, &ce) {
|
||||
fmt.Fprintf(stderr, "sw-block ops chunk %s: %s: %s (retryable=%v)\n", name, ce.Code, err, ce.Retryable)
|
||||
} else {
|
||||
fmt.Fprintf(stderr, "sw-block ops chunk %s: %v\n", name, err)
|
||||
switch {
|
||||
case errors.As(err, &ce):
|
||||
return chunkFail(name, stdout, stderr, chunkExitFailed, ce.Code, ce.Detail(), ce.Retryable)
|
||||
case errors.Is(err, context.DeadlineExceeded):
|
||||
return chunkFail(name, stdout, stderr, chunkExitFailed, chunkstore.CodeDeadlineExceeded, err.Error(), true)
|
||||
default:
|
||||
return chunkFail(name, stdout, stderr, chunkExitFailed, codeInternalCLI, err.Error(), false)
|
||||
}
|
||||
return chunkExitFailed
|
||||
}
|
||||
var buf bytes.Buffer
|
||||
if err := json.NewEncoder(&buf).Encode(v); err != nil {
|
||||
@@ -262,6 +253,38 @@ func chunkResult(name string, v any, err error, stdout, stderr io.Writer) int {
|
||||
return ops.VolumeStatusExitOK
|
||||
}
|
||||
|
||||
// CLI-side error codes, the same as the routes' (B9 §2.2 and the rulings
|
||||
// of 2026-11-15).
|
||||
const (
|
||||
codeBadRequestCLI = "bad_request"
|
||||
codeInternalCLI = "internal"
|
||||
codeUnavailableCLI = "remote_unavailable"
|
||||
)
|
||||
|
||||
type chunkErrBody struct {
|
||||
Code string `json:"code"`
|
||||
Message string `json:"message"`
|
||||
Retryable bool `json:"retryable"`
|
||||
}
|
||||
|
||||
// chunkFail reports a failure (appendix 14:30Z): the error body
|
||||
// {code,message,retryable} on stdout, as the routes answer it, a log line on
|
||||
// stderr, and the exit code. The message does not repeat the code.
|
||||
func chunkFail(name string, stdout, stderr io.Writer, exit int, code, message string, retryable bool) int {
|
||||
prefix := "sw-block ops chunk"
|
||||
if name != "" {
|
||||
prefix += " " + name
|
||||
}
|
||||
fmt.Fprintf(stderr, "%s: %s: %s\n", prefix, code, message)
|
||||
body, _ := json.Marshal(chunkErrBody{Code: code, Message: message, Retryable: retryable})
|
||||
if err := writeAll(stdout, append(body, '\n')); err != nil {
|
||||
fmt.Fprintf(stderr, "%s: write the error: %v\n", prefix, err)
|
||||
}
|
||||
return exit
|
||||
}
|
||||
|
||||
func chunkName(fs *flag.FlagSet) string { return strings.TrimPrefix(fs.Name(), "sw-block ops chunk ") }
|
||||
|
||||
// writeAll writes p and fails on a failed or short write: a lost result
|
||||
// must not exit 0.
|
||||
func writeAll(w io.Writer, p []byte) error {
|
||||
@@ -289,15 +312,14 @@ func chunkSnapshots(args []string, stdout, stderr io.Writer) int {
|
||||
fs.BoolVar(&includeDeleting, "include-deleting", false, "also list snapshots marked deleting")
|
||||
fs.DurationVar(&timeout, "timeout", 30*time.Second, "filer timeout")
|
||||
if err := fs.Parse(args); err != nil {
|
||||
return ops.VolumeStatusExitInvalid
|
||||
return chunkFail(chunkName(fs), stdout, stderr, ops.VolumeStatusExitInvalid, codeBadRequestCLI, err.Error(), false)
|
||||
}
|
||||
if volumeID == "" {
|
||||
fmt.Fprintln(stderr, "sw-block ops chunk snapshots: --volume is required")
|
||||
return ops.VolumeStatusExitInvalid
|
||||
return chunkFail("snapshots", stdout, stderr, ops.VolumeStatusExitInvalid, codeBadRequestCLI, "--volume is required", false)
|
||||
}
|
||||
cat, closeFn, ok := chunkCatalog("snapshots", filer, base, stderr)
|
||||
if !ok {
|
||||
return ops.VolumeStatusExitInvalid
|
||||
cat, closeFn, exit := chunkCatalog("snapshots", filer, base, stdout, stderr)
|
||||
if exit >= 0 {
|
||||
return exit
|
||||
}
|
||||
defer closeFn()
|
||||
ctx, cancel := context.WithTimeout(context.Background(), timeout)
|
||||
@@ -318,15 +340,14 @@ func chunkSnapshotDelete(args []string, stdout, stderr io.Writer) int {
|
||||
fs.StringVar(&base, "chunk-path", defaultChunkPathCLI, "filer directory holding one directory per volume")
|
||||
fs.DurationVar(&timeout, "timeout", 30*time.Second, "filer timeout")
|
||||
if err := fs.Parse(args); err != nil {
|
||||
return ops.VolumeStatusExitInvalid
|
||||
return chunkFail(chunkName(fs), stdout, stderr, ops.VolumeStatusExitInvalid, codeBadRequestCLI, err.Error(), false)
|
||||
}
|
||||
if volumeID == "" || name == "" {
|
||||
fmt.Fprintln(stderr, "sw-block ops chunk snapshot-delete: --volume and --name are required")
|
||||
return ops.VolumeStatusExitInvalid
|
||||
return chunkFail("snapshot-delete", stdout, stderr, ops.VolumeStatusExitInvalid, codeBadRequestCLI, "--volume and --name are required", false)
|
||||
}
|
||||
cat, closeFn, ok := chunkCatalog("snapshot-delete", filer, base, stderr)
|
||||
if !ok {
|
||||
return ops.VolumeStatusExitInvalid
|
||||
cat, closeFn, exit := chunkCatalog("snapshot-delete", filer, base, stdout, stderr)
|
||||
if exit >= 0 {
|
||||
return exit
|
||||
}
|
||||
defer closeFn()
|
||||
ctx, cancel := context.WithTimeout(context.Background(), timeout)
|
||||
@@ -351,19 +372,18 @@ func chunkFork(args []string, stdout, stderr io.Writer) int {
|
||||
fs.Var(labels, "label", "key=value label (repeatable)")
|
||||
fs.DurationVar(&timeout, "timeout", 6*time.Minute, "filer timeout (the fork's own HEAD deadline is OpDeadline)")
|
||||
if err := fs.Parse(args); err != nil {
|
||||
return ops.VolumeStatusExitInvalid
|
||||
return chunkFail(chunkName(fs), stdout, stderr, ops.VolumeStatusExitInvalid, codeBadRequestCLI, err.Error(), false)
|
||||
}
|
||||
if source == "" || snapshot == "" || target == "" {
|
||||
fmt.Fprintln(stderr, "sw-block ops chunk fork: --source, --snapshot and --target are required")
|
||||
return ops.VolumeStatusExitInvalid
|
||||
return chunkFail("fork", stdout, stderr, ops.VolumeStatusExitInvalid, codeBadRequestCLI, "--source, --snapshot and --target are required", false)
|
||||
}
|
||||
if requestID == "" {
|
||||
requestID = newRequestID()
|
||||
fmt.Fprintf(stderr, "sw-block ops chunk fork: request id %s (reuse it to retry)\n", requestID)
|
||||
}
|
||||
cat, closeFn, ok := chunkCatalog("fork", filer, base, stderr)
|
||||
if !ok {
|
||||
return ops.VolumeStatusExitInvalid
|
||||
cat, closeFn, exit := chunkCatalog("fork", filer, base, stdout, stderr)
|
||||
if exit >= 0 {
|
||||
return exit
|
||||
}
|
||||
defer closeFn()
|
||||
ctx, cancel := context.WithTimeout(context.Background(), timeout)
|
||||
|
||||
@@ -45,8 +45,11 @@ func sizedServer(t *testing.T, n int) *httptest.Server {
|
||||
func TestOpsChunk_OversizedResponseIsRefused(t *testing.T) {
|
||||
var out, errb bytes.Buffer
|
||||
code := run([]string{"ops", "chunk", "cut", "--volume", "v1", "--status-addr", sizedServer(t, chunkResponseLimit+1).URL}, &out, &errb)
|
||||
if code != chunkExitFailed || out.Len() != 0 || !strings.Contains(errb.String(), "exceeds") {
|
||||
t.Fatalf("200 body one byte over the limit: exit %d, %d bytes relayed, stderr %q; want a refusal and nothing relayed", code, out.Len(), errb.String())
|
||||
if code != chunkExitFailed || out.Len() > 1024 || !strings.Contains(errb.String(), "exceeds") {
|
||||
t.Fatalf("200 body one byte over the limit: exit %d, %d bytes on stdout, stderr %q; want a refusal, not the body", code, out.Len(), errb.String())
|
||||
}
|
||||
if e := chunkErrorOut(t, out.String()); e.Code != "internal" {
|
||||
t.Fatalf("200 body one byte over the limit: %+v, want the internal error body", e)
|
||||
}
|
||||
out.Reset()
|
||||
errb.Reset()
|
||||
|
||||
@@ -87,14 +87,57 @@ func TestOpsChunk_HTTPCommandsSendSpecRequests(t *testing.T) {
|
||||
func TestOpsChunk_HTTPFailureIsTyped(t *testing.T) {
|
||||
srv, _ := chunkServer(t, 409, `{"code":"not_primary","message":"volume v1 is not a healthy primary here","retryable":false}`)
|
||||
code, out, errs := runChunk("cut", "--volume", "v1", "--status-addr", srv.URL)
|
||||
if code != chunkExitFailed || out != "" || !strings.Contains(errs, "not_primary: volume v1 is not a healthy primary here (retryable=false)") {
|
||||
t.Fatalf("refused cut: %d %q %q", code, out, errs)
|
||||
if e := chunkErrorOut(t, out); code != chunkExitFailed || e.Code != "not_primary" || e.Message != "volume v1 is not a healthy primary here" || e.Retryable ||
|
||||
!strings.Contains(errs, "not_primary: volume v1 is not a healthy primary here") {
|
||||
t.Fatalf("refused cut: %d %q %q; want the route's error body on stdout", code, out, errs)
|
||||
}
|
||||
if code, _, _ := runChunk("cut", "--volume", "v1"); code != 2 {
|
||||
t.Fatalf("cut without --status-addr: exit %d, want 2", code)
|
||||
if code, out, _ := runChunk("cut", "--volume", "v1"); code != 2 || chunkErrorOut(t, out).Code != "bad_request" {
|
||||
t.Fatalf("cut without --status-addr: exit %d %q, want 2 and bad_request", code, out)
|
||||
}
|
||||
if code, _, _ := runChunk("snapshot", "--volume", "v1", "--status-addr", srv.URL, "--name", "s1", "--label", "nokey"); code != 2 {
|
||||
t.Fatalf("a label without '=': exit %d, want 2", code)
|
||||
if code, out, _ := runChunk("snapshot", "--volume", "v1", "--status-addr", srv.URL, "--name", "s1", "--label", "nokey"); code != 2 || chunkErrorOut(t, out).Code != "bad_request" {
|
||||
t.Fatalf("a label without '=': exit %d %q, want 2 and bad_request", code, out)
|
||||
}
|
||||
}
|
||||
|
||||
// chunkErrorOut decodes the CLI's error body from stdout.
|
||||
func chunkErrorOut(t *testing.T, out string) chunkErrBody {
|
||||
t.Helper()
|
||||
var e chunkErrBody
|
||||
if err := json.Unmarshal([]byte(out), &e); err != nil || e.Code == "" {
|
||||
t.Fatalf("stdout is not an error body: %q (%v)", out, err)
|
||||
}
|
||||
if strings.Contains(e.Message, e.Code) {
|
||||
t.Fatalf("the error message repeats its code: %+v", e)
|
||||
}
|
||||
return e
|
||||
}
|
||||
|
||||
// Every failure is the error body on stdout and a non-zero exit (appendix
|
||||
// 14:30Z): usage, an unreachable blockvolume, a catalog refusal.
|
||||
func TestOpsChunk_ErrorsAreJSONOnStdout(t *testing.T) {
|
||||
if code, out, _ := runChunk("nope"); code != 2 || chunkErrorOut(t, out).Code != "bad_request" {
|
||||
t.Fatalf("unknown subcommand: exit %d %q", code, out)
|
||||
}
|
||||
if code, out, _ := runChunk("cut", "--bogus"); code != 2 || chunkErrorOut(t, out).Code != "bad_request" {
|
||||
t.Fatalf("unknown flag: exit %d %q", code, out)
|
||||
}
|
||||
dead := httptest.NewServer(http.NotFoundHandler())
|
||||
addr := dead.URL
|
||||
dead.Close()
|
||||
if code, out, _ := runChunk("cut", "--volume", "v1", "--status-addr", addr); code != chunkExitFailed {
|
||||
t.Fatalf("unreachable blockvolume: exit %d %q", code, out)
|
||||
} else if e := chunkErrorOut(t, out); e.Code != "remote_unavailable" || !e.Retryable {
|
||||
t.Fatalf("unreachable blockvolume: %+v, want remote_unavailable and retryable", e)
|
||||
}
|
||||
|
||||
mem := chunkstore.NewMemFiler()
|
||||
prev := dialChunkFiler
|
||||
dialChunkFiler = func(string) (chunkstore.Filer, func() error, error) { return mem, func() error { return nil }, nil }
|
||||
t.Cleanup(func() { dialChunkFiler = prev })
|
||||
if code, out, _ := runChunk("fork", "--chunk-filer", "f:1", "--source", "v1", "--snapshot", "s9", "--target", "v2", "--request-id", "r"); code != chunkExitFailed {
|
||||
t.Fatalf("fork of a missing snapshot: exit %d %q", code, out)
|
||||
} else if e := chunkErrorOut(t, out); e.Code != "snapshot_not_found" || e.Retryable {
|
||||
t.Fatalf("fork of a missing snapshot: %+v", e)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -113,6 +113,15 @@ func (e *CatalogError) Error() string {
|
||||
|
||||
func (e *CatalogError) Unwrap() error { return e.Err }
|
||||
|
||||
// Detail is the error without its code: the message, and the cause if any
|
||||
// (an error body carries the code in its own field).
|
||||
func (e *CatalogError) Detail() string {
|
||||
if e.Err != nil {
|
||||
return fmt.Sprintf("%s: %v", e.Message, e.Err)
|
||||
}
|
||||
return e.Message
|
||||
}
|
||||
|
||||
// CodeOf returns the control-plane code of err, or "" when it has none.
|
||||
func CodeOf(err error) string {
|
||||
var ce *CatalogError
|
||||
|
||||
@@ -147,7 +147,12 @@ func writeChunkErr(w http.ResponseWriter, status int, code string, retryable boo
|
||||
// writeChunkFailure answers err with its B9 code and HTTP status.
|
||||
func writeChunkFailure(w http.ResponseWriter, err error) {
|
||||
status, code, retryable := chunkErrorCode(err)
|
||||
writeChunkJSON(w, status, chunkErrorBody{Code: code, Message: err.Error(), Retryable: retryable})
|
||||
msg := err.Error()
|
||||
var ce *chunkstore.CatalogError
|
||||
if errors.As(err, &ce) {
|
||||
msg = ce.Detail() // the code is in its own field
|
||||
}
|
||||
writeChunkJSON(w, status, chunkErrorBody{Code: code, Message: msg, Retryable: retryable})
|
||||
}
|
||||
|
||||
// chunkErrorCode maps an operation's error to (HTTP status, code,
|
||||
|
||||
Reference in new issue
Block a user