MCPcopy
hub / github.com/larksuite/cli / TestReportError

Function TestReportError

cmd/update/update_test.go:384–420  ·  view source on GitHub ↗

TestReportError pins reportError's two surfaces after the typed migration: human mode returns the typed error unchanged; JSON mode prints the legacy {ok:false, error:{type, message}} envelope and exits bare with the typed error's exit code (parity with the legacy explicit exit-code argument).

(t *testing.T)

Source from the content-addressed store, hash-verified

382// {ok:false, error:{type, message}} envelope and exits bare with the typed
383// error's exit code (parity with the legacy explicit exit-code argument).
384func TestReportError(t *testing.T) {
385 t.Run("human mode returns the typed error", func(t *testing.T) {
386 f, _, _ := newTestFactory(t)
387 typed := errs.NewAPIError(errs.SubtypeUnknown, "failed to prepare update: disk full")
388 err := reportError(&UpdateOptions{JSON: false}, f.IOStreams, "update_error", typed)
389 var apiErr *errs.APIError
390 if !errors.As(err, &apiErr) {
391 t.Fatalf("expected *errs.APIError, got %T: %v", err, err)
392 }
393 if apiErr != typed {
394 t.Errorf("reportError must return the typed error unchanged")
395 }
396 if got := output.ExitCodeOf(err); got != output.ExitAPI {
397 t.Errorf("exit code = %d, want %d (ExitAPI, legacy parity)", got, output.ExitAPI)
398 }
399 })
400
401 t.Run("json mode prints envelope and exits bare with typed code", func(t *testing.T) {
402 f, stdout, _ := newTestFactory(t)
403 typed := errs.NewNetworkError(errs.SubtypeNetworkTransport, "failed to check latest version: timeout")
404 err := reportError(&UpdateOptions{JSON: true}, f.IOStreams, "network", typed)
405 var bareErr *output.BareError
406 if !errors.As(err, &bareErr) {
407 t.Fatalf("expected bare *output.BareError, got %T: %v", err, err)
408 }
409 if bareErr.Code != output.ExitNetwork {
410 t.Errorf("bare exit code = %d, want %d", bareErr.Code, output.ExitNetwork)
411 }
412 out := stdout.String()
413 if !strings.Contains(out, `"type": "network"`) && !strings.Contains(out, `"type":"network"`) {
414 t.Errorf("JSON envelope missing type, got: %s", out)
415 }
416 if !strings.Contains(out, "failed to check latest version: timeout") {
417 t.Errorf("JSON envelope missing message, got: %s", out)
418 }
419 })
420}
421
422func TestUpdateInvalidVersion_JSON(t *testing.T) {
423 f, stdout, _ := newTestFactory(t)

Callers

nothing calls this directly

Calls 9

NewAPIErrorFunction · 0.92
ExitCodeOfFunction · 0.92
NewNetworkErrorFunction · 0.92
reportErrorFunction · 0.85
AsMethod · 0.80
newTestFactoryFunction · 0.70
RunMethod · 0.65
StringMethod · 0.45
ContainsMethod · 0.45

Tested by

no test coverage detected