-
Notifications
You must be signed in to change notification settings - Fork 896
fix: prevent silent exits on error (#1139) and fix whiteboard API encoding (#1155) #1162
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Ellien-Tang
wants to merge
1
commit into
larksuite:main
Choose a base branch
from
Ellien-Tang:fix/1139-silent-exit-and-1155-encoding
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| // Copyright (c) 2026 Lark Technologies Pte. Ltd. | ||
| // SPDX-License-Identifier: MIT | ||
|
|
||
| package client | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "unicode/utf8" | ||
|
|
||
| "golang.org/x/text/encoding" | ||
| "golang.org/x/text/encoding/simplifiedchinese" | ||
| "golang.org/x/text/encoding/unicode" | ||
| ) | ||
|
|
||
| // autoDecodeBody attempts to detect and convert non-UTF-8 response bodies to | ||
| // valid UTF-8. Some Lark APIs (e.g. whiteboard) occasionally return JSON with | ||
| // Chinese characters in encodings other than UTF-8 despite the Content-Type | ||
| // header claiming otherwise. See https://github.com/larksuite/cli/issues/1155 | ||
| func autoDecodeBody(body []byte) []byte { | ||
| // Fast path: already valid UTF-8 (common case). | ||
| if utf8.Valid(body) { | ||
| return body | ||
| } | ||
|
|
||
| // Strip UTF-8 BOM if present. | ||
| body = bytes.TrimPrefix(body, unicode.UTF8BOM) | ||
|
|
||
| // Try encodings in order of likelihood for Lark/Feishu APIs. | ||
| candidates := []struct { | ||
| enc encoding.Encoding | ||
| name string | ||
| }{ | ||
| {simplifiedchinese.GBK, "GBK"}, | ||
| {simplifiedchinese.GB18030, "GB18030"}, | ||
| {simplifiedchinese.HZGB2312, "HZ-GB2312"}, | ||
| } | ||
|
|
||
| for _, c := range candidates { | ||
| if decoded, err := c.enc.NewDecoder().Bytes(body); err == nil && utf8.Valid(decoded) { | ||
| return decoded | ||
| } | ||
| } | ||
|
|
||
| // Return original unchanged if no conversion succeeded. | ||
| return body | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Guard all
fdereferences inhandleRootErrorto avoid nil panic paths.The new fallback at Lines 217-225 is good, but
fis still unconditionally dereferenced at Line 247 and Lines 256-259. Iffis nil in the exceptional path this block targets,handleRootErrorcan 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