larksuite/cli · error
failed to parse response: %w
Error message
failed to parse response: %w
What it means
VerifyUserToken successfully called the token-verification API, but json.Unmarshal on apiResp.RawBody failed, so the verification verdict could not be determined. The %w wrap preserves the unmarshal error (syntax error, type mismatch).
Source
Thrown at internal/auth/verify.go:34
// VerifyUserToken calls /authen/v1/user_info to confirm the token is accepted server-side.
// Returns nil on success or an error describing why the server rejected the token.
func VerifyUserToken(ctx context.Context, sdk *lark.Client, accessToken string) error {
apiResp, err := sdk.Do(ctx, &larkcore.ApiReq{
HttpMethod: http.MethodGet,
ApiPath: PathUserInfoV1,
SupportedAccessTokenTypes: []larkcore.AccessTokenType{larkcore.AccessTokenTypeUser},
}, larkcore.WithUserAccessToken(accessToken))
if err != nil {
return err
}
logSDKResponse(PathUserInfoV1, apiResp)
var resp struct {
Code int `json:"code"`
Msg string `json:"msg"`
}
if err := json.Unmarshal(apiResp.RawBody, &resp); err != nil {
return fmt.Errorf("failed to parse response: %w", err)
}
if resp.Code != 0 {
return fmt.Errorf("[%d] %s", resp.Code, resp.Msg)
}
return nil
}
View on GitHub (pinned to 7fd6ef3c07)
Solutions
- Dump apiResp.RawBody to see what was actually returned; an HTML page means the request never hit the API — check base URL/proxy.
- Check the HTTP status of apiResp; a 5xx with plain text body points at a gateway problem, retry later.
- Confirm the code and endpoint match the current Feishu API version (field shapes may change).
- If RawBody is empty, verify the API call path that produced apiResp.
Defensive patterns
Strategy: type-guard
Validate before calling
// before verifying, sanity-check that the raw body looks like JSON
if len(apiResp.RawBody) == 0 || apiResp.RawBody[0] != '{' {
return fmt.Errorf("verify endpoint returned non-JSON (HTTP %d): %.200q", apiResp.StatusCode, apiResp.RawBody)
} Type guard
func isParseErr(err error) bool {
var syn *json.SyntaxError
var typ *json.UnmarshalTypeError
return errors.As(err, &syn) || errors.As(err, &typ)
} Try / catch
if err := auth.VerifyUserToken(ctx, token); err != nil {
if isParseErr(err) {
return fmt.Errorf("verify response shape changed or gateway returned HTML; check base URL/API version: %w", err)
}
return err
} Prevention
- Assert the verify endpoint URL matches the current Feishu API version.
- Pin/verify the expected response shape {code:int,msg:string} in tests.
- Log RawBody and status on parse failure for fast diagnosis.
- Guard against gateways returning HTML by checking content-type before unmarshal.
When it happens
Trigger: json.Unmarshal(apiResp.RawBody, &resp) fails in VerifyUserToken — the verification endpoint returned a non-JSON body (HTML error page, empty body) or a body whose shapes don't match {code:int, msg:string}.
Common situations: Gateway outage returning HTML; wrong endpoint/base URL; a version change where the verify endpoint returns a differently-shaped JSON (e.g. string code); truncated response on flaky networks.
Understand the failure class
- Parsing and encoding errors: unexpected token, malformed input — why parsers reject input and how to find the real culprit.
Related errors
- failed to parse user info: %w
- failed to parse response: %w
- Device authorization failed: HTTP %d – response not JSON
- [%d] %s
- file provider JSON parse error: %w
AI-assisted analysis of larksuite/cli@7fd6ef3c07 (2026-09-04).
Data as JSON: /api/errors/37ae93e649a28c0c.
Report an issue: GitHub.