{"record":{"id":"4fcf20f7b0a4a6eb","repo":"AlexxIT/go2rtc","slug":"xiaomi-s","errorCode":null,"errorMessage":"xiaomi: %s","messagePattern":"xiaomi: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/xiaomi/cloud.go","lineNumber":466,"sourceCode":"\t\treturn nil, err\n\t}\n\n\tplaintext, err := crypt(signedNonce, ciphertext)\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\n\tvar res1 struct {\n\t\tCode    int             `json:\"code\"`\n\t\tMessage string          `json:\"message\"`\n\t\tResult  json.RawMessage `json:\"result\"`\n\t}\n\tif err = json.Unmarshal(plaintext, &res1); err != nil {\n\t\treturn nil, err\n\t}\n\n\tif res1.Code != 0 {\n\t\treturn nil, errors.New(\"xiaomi: \" + res1.Message)\n\t}\n\n\treturn res1.Result, nil\n}\n\nfunc readLoginResponse(rc io.ReadCloser, v any) ([]byte, error) {\n\tdefer rc.Close()\n\n\tbody, err := io.ReadAll(rc)\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\n\tbody, ok := bytes.CutPrefix(body, []byte(\"&&&START&&&\"))\n\tif !ok {\n\t\treturn nil, fmt.Errorf(\"xiaomi: %s\", body)\n\t}\n","sourceCodeStart":448,"sourceCodeEnd":484,"githubUrl":"https://github.com/AlexxIT/go2rtc/blob/c245815e75e2a5fd60b4290f12bfc04e55a984d3/pkg/xiaomi/cloud.go#L448-L484","documentation":"After decrypting the Xiaomi cloud response, Request unmarshals it and checks the application-level code field. Xiaomi APIs return code 0 on success; any other code is turned into this error with the API's message appended (e.g. 'xiaomi: invalid token'), representing a business-logic failure inside an HTTP-200 response.","triggerScenarios":"Calling xiaomi cloud Request when the API responds 200 but with a non-zero code in the decrypted payload: invalid/expired service token, device ID mismatch, unsupported command, or account permission issues.","commonSituations":"Tokens expired after Xiaomi session rotation; issuing commands to devices not owned by the logged-in account; device offline errors surfaced via the code/message; wrong request parameters.","solutions":["Read the appended message to learn the exact API-reported cause","Re-login to refresh service tokens if the message mentions token/auth","Validate device DIDs and command parameters against the Xiaomi API spec","Retry only for transient device-state messages (e.g. device offline) after confirming the device is online"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"res, err := cloud.Request(client, url, payload)\nif err != nil {\n    var apiErr string\n    if strings.HasPrefix(err.Error(), \"xiaomi: \") {\n        apiErr = strings.TrimPrefix(err.Error(), \"xiaomi: \")\n        // route on apiErr: token issues -> re-login; device offline -> retry later\n    }\n    return err\n}","preventionTips":["Log the API message for every non-zero code","Re-login when messages reference tokens/expiry","Validate DIDs and command params before sending"],"tags":["xiaomi","cloud","api","error-response"],"backgroundTag":"api-error-response","analyzedSha":"c245815e75e2a5fd60b4290f12bfc04e55a984d3","analyzedAt":"2026-09-07T11:47:02.965Z","contentChangedAt":"2026-09-07T11:47:02.965Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}