Repository navigation
fix: classify agent errors by type, not by words in the message - #896
Conversation
discostu105
left a comment
There was a problem hiding this comment.
Verdict: the approach is sound, and the cobra/pflag detection and the timeout/context/refresh typing hold up. Two config and validation failures that used to be classified correctly now fall through to error, and the SDK changelog was not updated.
-
sdk/session/client.go:85and:94,sdk/session/config.go:991: should-fix. These are real config-file failures that are never wrapped asconfigError: a project.dtctl.yamlwhose${DT_ENVIRONMENT_URL}did not resolve, a local environment that is not a Dynatrace origin, and a token missing from a sealed config. They come fromNewClientFromConfigafterLoadhas succeeded, soconfigFile()never sees them.
Scenario: in a fresh clone withDT_ENVIRONMENT_URLunset,dtctl get workflows --agentgiveserror. On main it givesconfig_error, which per AGENT_MODE.md means "needs human repair".
Fix: give these errors a type in the SDK (for example aLocalConfigError), or wrapnewClientFromConfigerrors that are notContextNotFoundError/ErrNoCurrentContextand that come from config resolution. Add a test for the unresolved-variable case. -
cmd/error_class.go:96(isDtctlValidationMessage): should-fix. Narrowing the rule to a message prefix also drops malformed input that dtctl wraps.
Scenario:dtctl exec analyzer X --input '{bad' --agentreturnsfailed to parse input JSON: invalid character 'b' …(cmd/exec_analyzers.go:208). On main that isvalidation_error; it is nowerror. The same applies to the otherfailed to parse …: %wwrappers aroundencoding/jsoninpkg/applyandcmd/create_*. It also affectssession: invalid safety level(cmd/session.go:150).
Fix: classify*json.SyntaxError/*json.UnmarshalTypeError(and the YAML decoder's error type) asvalidation_errorwitherrors.As. This is the same idea as the rest of the PR. Add a table case for a wrapped*json.SyntaxError. -
sdk/CHANGELOG.md: should-fix (convention). The PR adds exported SDK API:session.ErrNoCurrentContext,session.ContextNotFoundError,session.TokenRefreshError,httpclient.TimeoutError/Timeoutf. It also changes what SDK callers can see:sdk/api/extension403 errors now wrap the*APIErrorand their text gains a: <api error>suffix. None of this is under[Unreleased], while comparable additions (ErrConfigNotFound) were recorded there. -
cmd/usage.go:168,:274: nit. The text changes fromno current context; select one…tono current context set; select one…. That is harmless, but the PR body says "all messages stay the same". Either reword the body or keep the old text with a small wrapper type.
Checked and fine: the cobra unknown-command regex and command-path check (including noArgs groups and plugin dispatch); GetSpecifiedName against the old UnknownFlagName for the shorthand form; auth refresh wrapping (the old substring also matched failed to refresh tokens); and that no remaining 401/403 path drops its APIError. CI: 22/22 checks green.
cc8ecc7 to
66075f1
Compare
|
Thanks for the review. All four points are fixed in 66075f1 (amended, one push):
CI: 22/22 checks green. The first run hit a data race in |
The agent error code, the wrong-URL hints and the re-login hint were picked
by substring of the error message, so a quoted name decided them: a missing
dashboard "timeout-overview" reported `timeout`, a workflow
"team-config-sync" `config_error`, an entity "HOST-403" earned wrong-URL
hints. classifyCommandLineError likewise rewrote any error quoting
"unknown command"/"unknown flag" as a cobra usage error, dropping its type,
exit code and suggestions.
Errors are now typed where they originate and classified with errors.Is/As:
- config load/save failures (configError) and session.ConfigError for a
config that loaded but cannot be used (unresolved ${VAR} environment,
non-Dynatrace local origin, token missing from a sealed config);
session.ErrNoCurrentContext, session.ContextNotFoundError,
session.TokenRefreshError
- httpclient.TimeoutError (Timeout() bool) for dtctl's own waits; deadlines
and network timeouts are recognised by type in every invocation mode
- malformed input: *json.SyntaxError, *json.UnmarshalTypeError,
*yaml.TypeError and yaml.v3's own syntax errors, anywhere in the chain;
invalid embedding Session options are invalidInputError
- wrong-URL hints only for 401/403 and DNS/connection-refused errors;
extension 403s now wrap the APIError instead of dropping it
- cobra's unknown command error is recognised by its exact shape and the
command path it names; unknown flags by *pflag.NotExistError
- validation_error keeps one text fallback: a message dtctl wrote that starts
with "invalid " or "validation failed"
The invalid-flag-value case of #819 was already fixed on main; this adds a
regression test for it.
Agent-visible changes (not breaking per AGENTS.md: agent output may change in any minor release): A missing or unselected
context is `context_error` (was `error`); a client-side deadline in the CLI
is `timeout` (was `error`); an extension 403 is `permission_denied` with
exit code 3 (was `error`, exit 1) and its message gains the API error;
errors that merely mention config/timeout/invalid/context or 401/403 in a
quoted name or server text are `error` and no longer get wrong-URL hints;
`dtctl get` without a resource is `error` (was `config_error`). The `dtctl
usage` message reads "no current context set; ..." (was "no current
context; ...").
Fixes #818
Fixes #829
Fixes #819
66075f1 to
f0b69f9
Compare
What broke
classifyGenericError,isURLRelatedErrorandisTokenRefreshErrormatched substrings of the message, so quoted names decided the outcome. A missing dashboardtimeout-overviewcame back astimeout, a workflowteam-config-syncasconfig_error, and an entityHOST-403got the wrong-URL hints. On main,dtctl get --agenteven reportsconfig_error, because the list of resources it prints contains "extension-configs".classifyCommandLineErrorrewrote any error whose message contained "unknown command" or "unknown flag" as a cobra usage error. That dropped the error's type, its exit code and its suggestions.Approach
Errors now carry their type from where they are created, and are classified with
errors.Is/errors.As:configError. A config that loads but cannot be used is asession.ConfigError: an unresolved${VAR}environment, a local environment that is not a Dynatrace origin, or a token missing from a sealed config.session.ErrNoCurrentContext,session.ContextNotFoundErrorandsession.TokenRefreshError.httpclient.TimeoutError(implementsTimeout() bool) for dtctl's own waits. Deadlines and network timeouts are recognised by type in every invocation mode.*json.SyntaxError,*json.UnmarshalTypeError,*yaml.TypeErrorand yaml.v3's own syntax errors are recognised anywhere in the error chain. Invalid embeddingSessionoptions becomeinvalidInputError.APIErrorinstead of dropping it.*pflag.NotExistError.validation_errorkeeps one, for messages dtctl wrote itself that start withinvalidorvalidation failed.Apart from the changes listed below, error messages are unchanged.
Breaking (agent-visible)
The set of error codes and the envelope schema are unchanged. Some errors now get a different code:
context_error(it used to beerror).timeout(it used to beerror).permission_deniedwith exit code 3, and its message gains the API error.dtctl getwithout a resource is nowerror(it used to beconfig_error).error, and they no longer get the wrong-URL hints.Message change: in
dtctl usage, the text now reads "no current context set; …" instead of "no current context; …".#819 (an invalid flag value followed by
--agent) was already fixed on main by #864. This PR adds a regression test for it.Verification
go build ./...,go vet,golangci-lint(new code): clean.cmd, the touchedpkg/packages, andsdk/{session,httpclient,api/...}.cmd/error_class_test.goandsdk/session/client_test.gocover the cases from the issues and from the review.sdk/CHANGELOG.mdupdated.Agent error codes change as listed above. Per AGENTS.md this is not a breaking change (agent output may change in any minor release).
Fixes #818
Fixes #829
Fixes #819