fix: prevent silent exits on error (#1139) and fix whiteboard API encoding (#1155)#1162
Conversation
|
CLI Fix Bot seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account. You have signed the CLA already but the status is still pending? Let us recheck it. |
📝 WalkthroughWalkthroughThe PR addresses silent failures in lark-cli by adding panic recovery, defensive error writer initialization, automatic response body encoding conversion for Chinese character sets, and error envelope fallbacks. These changes ensure diagnostics are always emitted to stderr, even in exceptional early-failure and encoding scenarios. ChangesSilent Failure Prevention and Diagnostic Output
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@cmd/root.go`:
- Around line 217-225: In handleRootError, guard every dereference of f: compute
errOut as shown and then replace all direct uses of f.IOStreams.ErrOut with
errOut, and wrap any access to f (e.g., f.Name, f.CommandPath, or other fields)
in nil checks (if f != nil { ... } else { use sensible fallbacks or omit values)
so the function never dereferences f when it's nil; specifically update the code
paths that currently reference f at the later return/log/formatting sites to use
errOut and conditional checks around f before reading any of its fields.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 0723f875-f98f-468f-91ee-6a807e5d6993
📒 Files selected for processing (5)
cmd/root.gointernal/client/encoding.gointernal/client/response.gointernal/output/errors.gomain.go
| // Defensive: f or its IOStreams may be nil in exceptional situations | ||
| // (e.g. early bootstrap failure, plugin corruption). Guarantee stderr | ||
| // is always a valid writer so diagnostics are never swallowed. | ||
| var errOut io.Writer | ||
| if f != nil && f.IOStreams != nil && f.IOStreams.ErrOut != nil { | ||
| errOut = f.IOStreams.ErrOut | ||
| } else { | ||
| errOut = os.Stderr | ||
| } |
There was a problem hiding this comment.
Guard all f dereferences in handleRootError to avoid nil panic paths.
The new fallback at Lines 217-225 is good, but f is still unconditionally dereferenced at Line 247 and Lines 256-259. If f is nil in the exceptional path this block targets, handleRootError can still panic.
Proposed fix
func handleRootError(f *cmdutil.Factory, err error) int {
@@
var errOut io.Writer
if f != nil && f.IOStreams != nil && f.IOStreams.ErrOut != nil {
errOut = f.IOStreams.ErrOut
} else {
errOut = os.Stderr
}
+ identity := ""
+ if f != nil {
+ identity = string(f.ResolvedIdentity)
+ }
@@
- applyNeedAuthorizationHint(f, err)
+ if f != nil {
+ applyNeedAuthorizationHint(f, err)
+ }
- if output.WriteTypedErrorEnvelope(errOut, err, string(f.ResolvedIdentity)) {
+ if output.WriteTypedErrorEnvelope(errOut, err, identity) {
return output.ExitCodeOf(err)
}
@@
if exitErr := asExitError(err); exitErr != nil {
- if !exitErr.Raw {
+ if !exitErr.Raw && f != nil {
// Raw errors (e.g. from `api` command via output.MarkRaw)
// preserve the original API error detail; skip enrichment
// which would clear it.
enrichMissingScopeError(f, exitErr)
enrichPermissionError(f, exitErr)
}
- output.WriteErrorEnvelope(errOut, exitErr, string(f.ResolvedIdentity))
+ output.WriteErrorEnvelope(errOut, exitErr, identity)
return exitErr.Code
}Also applies to: 245-247, 256-259
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@cmd/root.go` around lines 217 - 225, In handleRootError, guard every
dereference of f: compute errOut as shown and then replace all direct uses of
f.IOStreams.ErrOut with errOut, and wrap any access to f (e.g., f.Name,
f.CommandPath, or other fields) in nil checks (if f != nil { ... } else { use
sensible fallbacks or omit values) so the function never dereferences f when
it's nil; specifically update the code paths that currently reference f at the
later return/log/formatting sites to use errOut and conditional checks around f
before reading any of its fields.
This PR addresses two reported issues:
1. Fix silent exit with empty stderr (#1139)
When
lark-clifails in certain conditions (large--datapayload, keychain session conflict), it exits with code 1 but produces absolutely no output — stdout and stderr are both empty. This makes it impossible to diagnose the root cause in automated/scripted contexts.Changes:
handleRootError()to guarantee a valid stderr writer even whenFactory/IOStreamsare unexpectedly nil.WriteErrorEnvelope()now emits a minimal JSON envelope whenExitError.Detailis nil, preventing completely empty stderr.2. Fix Chinese character encoding in whiteboard API (#1155)
When fetching whiteboard node data via API, Chinese characters in the JSON response display as garbled text (e.g. "高" shows as "Ú«ÿ").
Changes:
autoDecodeBody()to detect and convert non-UTF-8 response bodies (GBK, GB18030, HZ-GB2312) that some Lark APIs return despite Content-Type claiming UTF-8.ParseJSONResponse().Fixes #1139
Fixes #1155
Summary by CodeRabbit