{"record":{"id":"673139ca66397679","repo":"JeffreySu/WeiXinMPSDK","slug":"0-1","errorCode":null,"errorMessage":"微信请求发生错误！错误代码：{0}，说明：{1}","messagePattern":"微信请求发生错误！错误代码：(.+?)，说明：(.+?)","errorType":"error_code","errorClass":"ErrorJsonResultException","httpStatus":null,"severity":"error","filePath":"src/Senparc.Weixin/Senparc.Weixin/CommonAPIs/ApiHandlerWapper/ApiHandlerWapperBase.cs","lineNumber":211,"sourceCode":"\n            try\n            {\n                if (accessToken == null)\n                {\n                    var accessTokenResult = accessTokenContainer_GetAccessTokenResultFunc(appId, false); //AccessTokenContainer.GetAccessTokenResult(appId, false);\n                    accessToken = accessTokenResult.access_token;\n                }\n                result = fun(accessToken);\n\n                //当系统不抛出异常，且当前返回结果不成功，且允许重试的时候，在内部抛出一个异常，以便进行 Retry\n                if (!Config.ThrownWhenJsonResultFaild\n                    && result is WxJsonResult\n                    && (result as WxJsonResult).errcode != ReturnCode.请求成功\n                    && retryIfFaild\n                    )\n                {\n                    var errorResult = result as WxJsonResult;\n                    throw new ErrorJsonResultException(\n                          string.Format(\"微信请求发生错误！错误代码：{0}，说明：{1}\",\n                                          (int)errorResult.errcode, errorResult.errmsg), null, errorResult);\n                }\n            }\n            catch (ErrorJsonResultException ex)\n            {\n                if (retryIfFaild\n                    && appId != null    //如果 appId 为 null，已经没有重试的意义（直接提供的 AccessToken 是错误的）\n                                        //&& ex.JsonResult.errcode == ReturnCode.获取access_token时AppSecret错误或者access_token无效)\n                    && invalidCredentialValues.Contains((int)ex.JsonResult.errcode))\n                {\n                    //尝试重新验证\n                    var accessTokenResult = accessTokenContainer_GetAccessTokenResultFunc(appId, true);//AccessTokenContainer.GetAccessTokenResult(appId, true);\n                                                                                                       //强制获取并刷新最新的AccessToken\n                    accessToken = accessTokenResult.access_token;\n                    result = TryCommonApiBase(platformType,\n                                accessTokenContainer_GetFirstOrDefaultAppIdFunc,\n                                accessTokenContainer_CheckRegisteredFunc,","sourceCodeStart":193,"sourceCodeEnd":229,"githubUrl":"https://github.com/JeffreySu/WeiXinMPSDK/blob/be573f6f94bdbf718dd5f6cdecb137fbc7ff651e/src/Senparc.Weixin/Senparc.Weixin/CommonAPIs/ApiHandlerWapper/ApiHandlerWapperBase.cs#L193-L229","documentation":"TryCommonApiBase wraps synchronous Weixin API calls with retry and access-token refresh logic. When the API returns a WxJsonResult whose errcode is not ReturnCode.请求成功 (0) and retryIfFaild is true (after retries fail), it throws ErrorJsonResultException carrying the Weixin error code and message.","triggerScenarios":"Calling any synchronous Weixin API wrapped by TryCommonApi (e.g. API获取AccessToken, menu APIs) and WeChat's server returns a non-zero errcode (40001 invalid credential, 45009 rate limit, etc.) that survives retry.","commonSituations":"Expired or wrong AppSecret/AccessToken (40001/42001), invalid appid, IP not whitelisted, exceeding WeChat API rate limits, malformed API parameters rejected by WeChat.","solutions":["Read ex.JsonResult.errcode and fix the underlying cause: refresh the access token, correct appid/secret, or fix request parameters.","Ensure Config.SenparcWeixinSetting has correct AppId/AppSecret and that the server IP is in the WeChat official-account IP whitelist.","Catch ErrorJsonResultException in your code and handle known recoverable codes (e.g. re-accesstoken then retry once).","Upgrade the library if the errcode handling/retry behavior seems wrong for your WeChat API version."],"exampleFix":"// before\nvar result = CommonApi.SendMenu(...); // throws on errcode != 0\n// after\ntry\n{\n    var result = CommonApi.SendMenu(...);\n}\ncatch (ErrorJsonResultException ex)\n{\n    Log($\"WeChat API failed: {ex.JsonResult.errcode} {ex.JsonResult.errmsg}\");\n    if (ex.JsonResult.errcode == ReturnCode.获取access_token时AppSecret错误或者access_token无效)\n        AccessTokenContainer.Remove(appId); // force refresh then retry\n}","handlingStrategy":"try-catch","validationCode":"// Ensure credentials are configured before calling WeChat APIs\nif (string.IsNullOrEmpty(Config.SenparcWeixinSetting.WeixinAppId) ||\n    string.IsNullOrEmpty(Config.SenparcWeixinSetting.WeixinAppSecret))\n    throw new InvalidOperationException(\"Weixin AppId/AppSecret not configured.\");","typeGuard":"bool IsWechatApiError(ErrorJsonResultException ex) => ex.JsonResult != null && ex.JsonResult.errcode != ReturnCode.请求成功;","tryCatchPattern":"try\n{\n    var result = TryCommonApi(...);\n}\ncatch (ErrorJsonResultException ex)\n{\n    switch (ex.JsonResult.errcode)\n    {\n        case ReturnCode.获取access_token时AppSecret错误或者access_token无效:\n            AccessTokenContainer.Remove(appId); break; // refresh token\n        default:\n            Log($\"WeChat error {(int)ex.JsonResult.errcode}: {ex.JsonResult.errmsg}\"); break;\n    }\n}","preventionTips":["Keep access tokens in a shared cache for multi-instance deployments.","Whitelist server IPs in the WeChat admin console.","Monitor errcodes 40001/42001/45009 and alert on spikes.","Validate appid/secret at startup with a token fetch."],"tags":["wechat","api","errcode","access-token"],"backgroundTag":"api-error-response","analyzedSha":"be573f6f94bdbf718dd5f6cdecb137fbc7ff651e","analyzedAt":"2026-09-12T10:01:50.733Z","contentChangedAt":"2026-09-12T10:01:50.733Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}