JeffreySu/WeiXinMPSDK · error · ErrorJsonResultException
微信请求发生错误!错误代码: ,说明:
Error message
微信请求发生错误!错误代码:{0},说明:{1} What it means
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.
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.
Example fix
// before
var result = CommonApi.SendMenu(...); // throws on errcode != 0
// after
try
{
var result = CommonApi.SendMenu(...);
}
catch (ErrorJsonResultException ex)
{
Log($"WeChat API failed: {ex.JsonResult.errcode} {ex.JsonResult.errmsg}");
if (ex.JsonResult.errcode == ReturnCode.获取access_token时AppSecret错误或者access_token无效)
AccessTokenContainer.Remove(appId); // force refresh then retry
} Defensive patterns
Strategy: try-catch
Validate before calling
// Ensure credentials are configured before calling WeChat APIs
if (string.IsNullOrEmpty(Config.SenparcWeixinSetting.WeixinAppId) ||
string.IsNullOrEmpty(Config.SenparcWeixinSetting.WeixinAppSecret))
throw new InvalidOperationException("Weixin AppId/AppSecret not configured."); Type guard
bool IsWechatApiError(ErrorJsonResultException ex) => ex.JsonResult != null && ex.JsonResult.errcode != ReturnCode.请求成功;
Try / catch
try
{
var result = TryCommonApi(...);
}
catch (ErrorJsonResultException ex)
{
switch (ex.JsonResult.errcode)
{
case ReturnCode.获取access_token时AppSecret错误或者access_token无效:
AccessTokenContainer.Remove(appId); break; // refresh token
default:
Log($"WeChat error {(int)ex.JsonResult.errcode}: {ex.JsonResult.errmsg}"); break;
}
} Prevention
- 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.
When it happens
Trigger: 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.
Common situations: Expired or wrong AppSecret/AccessToken (40001/42001), invalid appid, IP not whitelisted, exceeding WeChat API rate limits, malformed API parameters rejected by WeChat.
Related errors
- 微信请求发生错误(CommonApi.GetToken)!错误代码:
- 微信请求发生错误(CommonApi.GetStableAccessToken)!错误代码:
- 微信请求发生错误(CommonApi.GetStableAccessTokenAsync)!错误代码:
- MsgType: 在RequestMessageFactory中没有对应的处理程序!
- 请使用异步方法 OnExecutingAsync()
AI-assisted analysis of JeffreySu/WeiXinMPSDK@be573f6f94 (2026-09-12).
Data as JSON: /api/errors/673139ca66397679.
Report an issue: GitHub.
Appendix: source
Thrown at src/Senparc.Weixin/Senparc.Weixin/CommonAPIs/ApiHandlerWapper/ApiHandlerWapperBase.cs:211
try
{
if (accessToken == null)
{
var accessTokenResult = accessTokenContainer_GetAccessTokenResultFunc(appId, false); //AccessTokenContainer.GetAccessTokenResult(appId, false);
accessToken = accessTokenResult.access_token;
}
result = fun(accessToken);
//当系统不抛出异常,且当前返回结果不成功,且允许重试的时候,在内部抛出一个异常,以便进行 Retry
if (!Config.ThrownWhenJsonResultFaild
&& result is WxJsonResult
&& (result as WxJsonResult).errcode != ReturnCode.请求成功
&& retryIfFaild
)
{
var errorResult = result as WxJsonResult;
throw new ErrorJsonResultException(
string.Format("微信请求发生错误!错误代码:{0},说明:{1}",
(int)errorResult.errcode, errorResult.errmsg), null, errorResult);
}
}
catch (ErrorJsonResultException ex)
{
if (retryIfFaild
&& appId != null //如果 appId 为 null,已经没有重试的意义(直接提供的 AccessToken 是错误的)
//&& ex.JsonResult.errcode == ReturnCode.获取access_token时AppSecret错误或者access_token无效)
&& invalidCredentialValues.Contains((int)ex.JsonResult.errcode))
{
//尝试重新验证
var accessTokenResult = accessTokenContainer_GetAccessTokenResultFunc(appId, true);//AccessTokenContainer.GetAccessTokenResult(appId, true);
//强制获取并刷新最新的AccessToken
accessToken = accessTokenResult.access_token;
result = TryCommonApiBase(platformType,
accessTokenContainer_GetFirstOrDefaultAppIdFunc,
accessTokenContainer_CheckRegisteredFunc,View on GitHub (pinned to be573f6f94)