node-tools: a stopping tool runner answers the typed fault stopping (H-A-542)
A call the runner does not take once its stop began was answered nothing, so its caller's wait ended in a bare "timeout" and a build step's publish failed (main.440). It is now answered with error-kind stopping beside its words, on a tool's reply and a seat proof's alike; the calling runner keeps that kind and hands it to its bundle as the JSON-RPC error's data, so mesh-sdk's stdio tells it by its kind. A call cut at the drain bound stays unanswered (H-A-369). Builds: design 47 H2 H-A-542 (amends H-A-369)
This commit is contained in:
@@ -688,7 +688,28 @@ func (c *Conn) servedOn(module, tool string) []Served {
|
||||
type reply struct {
|
||||
Result any `json:"result,omitempty"`
|
||||
Error string `json:"error,omitempty"`
|
||||
Node string `json:"node,omitempty"`
|
||||
// ErrorKind is the typed fault beside Error, when the handler's error names one (ErrorKind() string): a stopping
|
||||
// tool runner's stopping (design 47 H2 H-A-542).
|
||||
ErrorKind string `json:"error-kind,omitempty"`
|
||||
Node string `json:"node,omitempty"`
|
||||
}
|
||||
|
||||
// KindedError is an answer's error with its typed fault (`error-kind`, design 47 H2 H-A-542): a stopping tool runner's
|
||||
// stopping. Its words are the answerer's; ErrorKind is told apart by its kind, never by its words.
|
||||
type KindedError struct{ Words, Kind string }
|
||||
|
||||
func (e *KindedError) Error() string { return e.Words }
|
||||
|
||||
// ErrorKind is the typed fault the answer named.
|
||||
func (e *KindedError) ErrorKind() string { return e.Kind }
|
||||
|
||||
// errorKindOf is the typed fault err names (ErrorKind() string), "" for none.
|
||||
func errorKindOf(err error) string {
|
||||
var kinded interface{ ErrorKind() string }
|
||||
if errors.As(err, &kinded) {
|
||||
return kinded.ErrorKind()
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// answerOn answers one subject with one handler, and says which machine answered (ADR 0159).
|
||||
@@ -715,7 +736,7 @@ func (c *Conn) answerCalledOn(subject, queue string, h CalledHandler) (func(), e
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
r.Error = err.Error()
|
||||
r.Error, r.ErrorKind = err.Error(), errorKindOf(err)
|
||||
} else {
|
||||
r.Result = nullable(result)
|
||||
}
|
||||
@@ -950,13 +971,18 @@ func (c *Conn) askOnce(subject string, data []byte, within time.Duration) (Answe
|
||||
// answerOf reads one answer.
|
||||
func answerOf(data []byte) (Answered, error) {
|
||||
var r struct {
|
||||
Result json.RawMessage `json:"result"`
|
||||
Error string `json:"error"`
|
||||
Node string `json:"node"`
|
||||
Result json.RawMessage `json:"result"`
|
||||
Error string `json:"error"`
|
||||
ErrorKind string `json:"error-kind"`
|
||||
Node string `json:"node"`
|
||||
}
|
||||
if err := json.Unmarshal(data, &r); err != nil {
|
||||
return Answered{}, err
|
||||
}
|
||||
if r.Error != "" && r.ErrorKind != "" {
|
||||
// A typed fault (a stopping tool runner's stopping, design 47 H2 H-A-542) reaches the caller as such.
|
||||
return Answered{}, &KindedError{Words: r.Error, Kind: r.ErrorKind}
|
||||
}
|
||||
if r.Error != "" {
|
||||
return Answered{}, errors.New(r.Error)
|
||||
}
|
||||
|
||||
@@ -243,3 +243,37 @@ func TestACallWaitsTheBoundItsVerbNamesAndTimesOutThere(t *testing.T) {
|
||||
t.Errorf("the call waited %s, want its own bound of 200ms, not the tool runner's %s", took, bus.RequestTimeout)
|
||||
}
|
||||
}
|
||||
|
||||
// stoppingFault is the error a stopping tool runner's drain hands for a call it does not take (launch's afterStop).
|
||||
type stoppingFault struct{}
|
||||
|
||||
func (stoppingFault) Error() string { return "not taken: the tool runner is stopping" }
|
||||
func (stoppingFault) ErrorKind() string { return "stopping" }
|
||||
|
||||
// A call a stopping tool runner does not take is answered the typed fault stopping, never silence, and its caller is
|
||||
// handed that kind as such, beside the answerer's words; a plain refusal carries no kind (design 47 H2 H-A-542).
|
||||
func TestAStoppingRunnersFaultReachesItsCallerByItsKind(t *testing.T) {
|
||||
url := mt.URL(t)
|
||||
server := dial(t, url, "ledger", "anchor", "")
|
||||
stop, err := server.HandleSubjectCalled("mesh.mod.ledger.tool.who", func(_, _ string, body json.RawMessage) (any, error) {
|
||||
if strings.Contains(string(body), "plain") {
|
||||
return nil, errors.New("ledger keeps no such key")
|
||||
}
|
||||
return nil, fmt.Errorf("ledger's who: %w", stoppingFault{})
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer stop()
|
||||
server.Flush()
|
||||
asker := dial(t, url, "node-tools", "shanks", "shanks.node-tools")
|
||||
_, err = asker.Ask("ledger.who", map[string]any{}, "")
|
||||
var kinded interface{ ErrorKind() string }
|
||||
if !errors.As(err, &kinded) || kinded.ErrorKind() != "stopping" || !strings.Contains(err.Error(), "the tool runner is stopping") {
|
||||
t.Errorf("the call was answered %v, want the typed fault stopping with the answerer's words", err)
|
||||
}
|
||||
_, err = asker.Ask("ledger.who", map[string]any{"plain": true}, "")
|
||||
if err == nil || errors.As(err, &kinded) || err.Error() != "ledger keeps no such key" {
|
||||
t.Errorf("a plain refusal was answered %v, want its words and no kind", err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -195,8 +195,8 @@ func (c *Conn) SeatProve(module, subject string, body json.RawMessage) (json.Raw
|
||||
}
|
||||
|
||||
// SeatAnswer answers every proof on a subject the module's seat traffic lists among its answers, with what
|
||||
// answer returns. A returned error is answered as `{"error": "<words>"}`, never as silence, but for one that says it is
|
||||
// unanswered (a proof after the stop or cut at the bound).
|
||||
// answer returns. A returned error is answered as `{"error": "<words>"}`, with its `error-kind` when it names one (a proof
|
||||
// after the stop: stopping, design 47 H2 H-A-542), never as silence, but for one that says it is unanswered (cut at the bound).
|
||||
func (c *Conn) SeatAnswer(module, subject string, answer func(subject string, body json.RawMessage) (json.RawMessage, error)) (func(), error) {
|
||||
t, err := c.trafficOf(module)
|
||||
if err != nil {
|
||||
@@ -220,14 +220,19 @@ func (c *Conn) SeatAnswer(module, subject string, answer func(subject string, bo
|
||||
return
|
||||
}
|
||||
out, err := answer(msg.Subject, body)
|
||||
// A proof arriving after the runner's stop, or cut at its bound, is answered nothing, as answerCalledOn does
|
||||
// (design 47 H2 §813 (12), H-A-369).
|
||||
// A proof cut at the runner's drain bound is answered nothing, as answerCalledOn does (design 47 H2 §813 (12),
|
||||
// H-A-369); one after the stop is answered stopping below (H-A-542).
|
||||
var unanswered interface{ Unanswered() bool }
|
||||
if errors.As(err, &unanswered) && unanswered.Unanswered() {
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
b, _ := json.Marshal(map[string]string{"error": err.Error()})
|
||||
said := map[string]string{"error": err.Error()}
|
||||
// A typed fault beside the words: a stopping tool runner's stopping (design 47 H2 H-A-542).
|
||||
if kind := errorKindOf(err); kind != "" {
|
||||
said["error-kind"] = kind
|
||||
}
|
||||
b, _ := json.Marshal(said)
|
||||
_ = msg.Respond(b)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -181,6 +181,9 @@ func TestAProofIsAskedAndAnsweredAndNeverKept(t *testing.T) {
|
||||
if string(body) == `{"cut":true}` {
|
||||
return nil, unansweredProof{}
|
||||
}
|
||||
if string(body) == `{"stopping":true}` {
|
||||
return nil, stoppingFault{}
|
||||
}
|
||||
return json.RawMessage(`{"linked":true}`), nil
|
||||
})
|
||||
if err != nil {
|
||||
@@ -195,6 +198,11 @@ func TestAProofIsAskedAndAnsweredAndNeverKept(t *testing.T) {
|
||||
if string(answer) != `{"linked":true}` {
|
||||
t.Fatalf("the proof was answered %s", answer)
|
||||
}
|
||||
// A proof after the stop is answered the typed fault stopping, never silence (design 47 H2 H-A-542).
|
||||
if raw, err := conn.SeatProve("telegram", "mesh.seat.intake.proof.code.telegram", json.RawMessage(`{"stopping":true}`)); err != nil ||
|
||||
!strings.Contains(string(raw), `"error-kind":"stopping"`) {
|
||||
t.Errorf("a proof after the stop was answered %s, %v; want its error-kind stopping", raw, err)
|
||||
}
|
||||
// A proof arriving after the stop, or cut at the bound, is answered nothing (design 47 H2 §813 (12), H-A-369): the
|
||||
// answerer handles proofs in order, so the first reply on one inbox after a cut proof and a whole one is the whole one's.
|
||||
raw, err := nats.Connect(mt.URL(t))
|
||||
|
||||
Reference in New Issue
Block a user