writeBumpCooldown schrieb als einziger Handler am apperr-Weg vorbei direkt eine 429-Antwort, weil der Body neben dem Fehler Nutzdaten braucht. apperr.Error kann das jetzt selbst: Extra legt Felder neben "error", RetryAfter wird zum gleichnamigen Header. handleBump liest Stand und bumpt in einer Abfrage (CTE mit UPDATE ... RETURNING) statt UPDATE plus Nachlese-SELECT; die Antwort ist ein Struct. TestBump prüft die Cooldown-Antwort jetzt auch inhaltlich. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
54 lines
1.4 KiB
Go
54 lines
1.4 KiB
Go
package apperr
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestModifiersCopy(t *testing.T) {
|
|
t.Parallel()
|
|
sentinel := NotFound("x.missing", "Fehlt.")
|
|
cause := errors.New("ursache")
|
|
tests := []struct {
|
|
name string
|
|
err *Error
|
|
}{
|
|
{"At", sentinel.At("feld")},
|
|
{"WithFields", sentinel.WithFields(map[string]string{"a": "b"})},
|
|
{"WithCause", sentinel.WithCause(cause)},
|
|
{"WithExtra", sentinel.WithExtra(map[string]any{"n": 1})},
|
|
{"WithRetryAfter", sentinel.WithRetryAfter(time.Minute)},
|
|
}
|
|
for _, tt := range tests {
|
|
if sentinel.Field != "" || sentinel.Fields != nil || sentinel.Unwrap() != nil ||
|
|
sentinel.Extra != nil || sentinel.RetryAfter != 0 {
|
|
t.Fatalf("%s hat das Sentinel verändert: %+v", tt.name, sentinel)
|
|
}
|
|
if !errors.Is(fmt.Errorf("verpackt: %w", tt.err), sentinel) {
|
|
t.Errorf("%s: Kopie gilt nicht mehr als das Sentinel", tt.name)
|
|
}
|
|
}
|
|
if !errors.Is(sentinel.WithCause(cause), cause) {
|
|
t.Error("Ursache nicht über errors.Is erreichbar")
|
|
}
|
|
}
|
|
|
|
func TestFrom(t *testing.T) {
|
|
t.Parallel()
|
|
tests := []struct {
|
|
err error
|
|
want Kind
|
|
}{
|
|
{Invalid("a.b", "x"), KindInvalid},
|
|
{fmt.Errorf("verpackt: %w", Conflict("a.c", "x")), KindConflict},
|
|
{errors.New("roh"), KindInternal},
|
|
}
|
|
for _, tt := range tests {
|
|
if got := From(tt.err).Kind; got != tt.want {
|
|
t.Errorf("From(%v).Kind = %v, erwartet %v", tt.err, got, tt.want)
|
|
}
|
|
}
|
|
}
|