{"record":{"id":"24d4254946ee4846","repo":"JeffreySu/WeiXinMPSDK","slug":"sendtype","errorCode":null,"errorMessage":"sendType","messagePattern":"sendType","errorType":"validation","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"src/Senparc.Weixin/Senparc.Weixin/CommonAPIs/CommonJsonSend.cs","lineNumber":219,"sourceCode":"                            {\n                                writer.Write(jsonString);\n                                writer.Flush();\n                            }\n                            ms.Seek(0, SeekOrigin.Begin);\n\n                            WeixinTrace.SendApiPostDataLog(url, jsonString);//记录Post的Json数据\n\n                            //PostGetJson方法中将使用WeixinTrace记录结果\n                            return Post.PostGetJson<T>(CommonDI.CommonSP, url, null, ms,\n                                timeOut: timeOut,\n                                contentType: contentType,\n                                afterReturnText: postFailAction,\n                                checkValidationResult: checkValidationResult);\n                        }\n\n                    //TODO:对于特定的错误类型自动进行一次重试，如40001（目前的问题是同样40001会出现在不同的情况下面）\n                    default:\n                        throw new ArgumentOutOfRangeException(\"sendType\");\n                }\n            }\n            catch (ErrorJsonResultException ex)\n            {\n                ex.Url = urlFormat;\n                throw;\n            }\n        }\n\n        #endregion\n\n\n        #region 异步方法\n\n        /// <summary>\n        /// 向需要AccessToken的API发送消息的公共方法\n        /// </summary>\n        /// <param name=\"accessToken\">这里的AccessToken是通用接口的AccessToken，非OAuth的。如果不需要，可以为null，此时urlFormat不要提供{0}参数</param>","sourceCodeStart":201,"sourceCodeEnd":237,"githubUrl":"https://github.com/JeffreySu/WeiXinMPSDK/blob/be573f6f94bdbf718dd5f6cdecb137fbc7ff651e/src/Senparc.Weixin/Senparc.Weixin/CommonAPIs/CommonJsonSend.cs#L201-L237","documentation":"CommonJsonSend.Send dispatches on the CommonJsonSendType parameter. The switch's default arm throws ArgumentOutOfRangeException(\"sendType\") when sendType is a value outside the implemented Post/Get cases (or an undefined enum cast).","triggerScenarios":"Calling CommonJsonSend.Send with sendType = (CommonJsonSendType)99 (undefined), or a library version where a sendType value exists in the enum but Send's switch lacks its case.","commonSituations":"Reading sendType from config/appsettings as an int and casting without validating; upgrading/downgrading Senparc.Weixin versions where enum members and switch cases diverge; copy-pasting code that used a value valid only for SendAsync or another overload.","solutions":["Only pass CommonJsonSendType.Post or CommonJsonSendType.Get (values implemented by Send); validate/whitelist before casting from ints or strings.","Use Enum.TryParse<CommonJsonSendType> and check the parsed value is a defined, supported member before calling.","If a newer enum value is needed, upgrade the library or send the request directly via Senparc.Weixin.HttpUtility/RequestUtility instead.","Log the offending sendType value to find where the bad value originates."],"exampleFix":"// before\nvar type = (CommonJsonSendType)int.Parse(config[\"sendType\"]);\nCommonJsonSend.Send<T>(url, data, type);\n// after\nif (!Enum.TryParse(config[\"sendType\"], out CommonJsonSendType type)\n    || (type != CommonJsonSendType.Post && type != CommonJsonSendType.Get))\n{\n    type = CommonJsonSendType.Post; // or reject the config\n}\nCommonJsonSend.Send<T>(url, data, type);","handlingStrategy":"validation","validationCode":"if (!Enum.IsDefined(typeof(CommonJsonSendType), sendType))\n    throw new ArgumentException($\"Unsupported sendType: {sendType}\");\nif (sendType != CommonJsonSendType.Post && sendType != CommonJsonSendType.Get)\n    throw new ArgumentException($\"Send does not implement {sendType}\");","typeGuard":"bool IsSupportedSendType(CommonJsonSendType t) =>\n    t == CommonJsonSendType.Post || t == CommonJsonSendType.Get;","tryCatchPattern":"try\n{\n    CommonJsonSend.Send<T>(url, data, sendType);\n}\ncatch (ArgumentOutOfRangeException)\n{\n    Log($\"sendType {sendType} not supported by Send; falling back to Post.\");\n    CommonJsonSend.Send<T>(url, data, CommonJsonSendType.Post);\n}","preventionTips":["Never cast raw ints/strings to CommonJsonSendType without Enum.IsDefined.","Whitelist only Post/Get for Send; use SendAsync's supported set for async.","Pin the library version and re-test enum coverage after upgrades.","Centralize sendType selection in one helper with validation."],"tags":["argument","enum","http","senparc-weixin"],"backgroundTag":"invalid-enum-argument","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"}