{"record":{"id":"835f9f71cdcc3865","repo":"JeffreySu/WeiXinMPSDK","slug":"requestmethod-tenpayhttpclient","errorCode":null,"errorMessage":"requestMethod","messagePattern":"requestMethod","errorType":"exception","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"src/Senparc.Weixin.TenPay/Senparc.Weixin.TenPayV3/TenPayHttpClient/TenPayHttpClient.cs","lineNumber":248,"sourceCode":"            request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue(\"*/*\"));\n\n            var userAgentValues = UserAgentValues.Instance;\n            request.Headers.UserAgent.Add(new ProductInfoHeaderValue(\"Senparc.Weixin.TenPayV3-C#\", userAgentValues.TenPayV3Version));\n            request.Headers.UserAgent.Add(new ProductInfoHeaderValue($\"(Senparc.Weixin {userAgentValues.SenparcWeixinVersion})\"));\n            request.Headers.UserAgent.Add(new ProductInfoHeaderValue(\".NET\", userAgentValues.RuntimeVersion));\n            request.Headers.UserAgent.Add(new ProductInfoHeaderValue($\"({userAgentValues.OSVersion})\"));\n        }\n\n        protected HttpMethod GetHttpMethod(ApiRequestMethod requestMethod)\n        {\n            return requestMethod switch\n            {\n                ApiRequestMethod.GET => HttpMethod.Get,\n                ApiRequestMethod.POST => HttpMethod.Post,\n                ApiRequestMethod.PUT => HttpMethod.Put,\n                ApiRequestMethod.PATCH => HttpMethod.Patch,\n                ApiRequestMethod.DELETE => HttpMethod.Delete,\n                _ => throw new ArgumentOutOfRangeException(nameof(requestMethod)),\n            };\n        }\n\n        protected async Task<string> GenerateAuthorizationHeader(HttpRequestMessage request)\n        {\n            string method = request.Method.ToString();\n            string body = \"\";\n            if (method == \"POST\" || method == \"PUT\" || method == \"PATCH\")\n            {\n                var content = request.Content;\n                body = await content.ReadAsStringAsync();\n            }\n\n            string uri = request.RequestUri.PathAndQuery;\n            var timestamp = DateTimeOffset.Now.ToUnixTimeSeconds();\n            string nonce = Path.GetRandomFileName();\n\n            string message = $\"{method}\\n{uri}\\n{timestamp}\\n{nonce}\\n{body}\\n\";","sourceCodeStart":230,"sourceCodeEnd":266,"githubUrl":"https://github.com/JeffreySu/WeiXinMPSDK/blob/be573f6f94bdbf718dd5f6cdecb137fbc7ff651e/src/Senparc.Weixin.TenPay/Senparc.Weixin.TenPayV3/TenPayHttpClient/TenPayHttpClient.cs#L230-L266","documentation":"GetHttpMethod maps the ApiRequestMethod enum to System.Net.Http.HttpMethod; any value outside GET/POST/PUT/PATCH/DELETE falls into the default arm and throws ArgumentOutOfRangeException(nameof(requestMethod)). The message is just the parameter name.","triggerScenarios":"Passing an undefined/invalid ApiRequestMethod value (e.g. an int cast that doesn't correspond to a defined enum member) into SendAsync/request building, typically via GetHttpMethod.","commonSituations":"Enum deserialized from config or a database string that produced a non-member value; casting an int constant to ApiRequestMethod; a custom extension that introduced a new verb the switch doesn't handle.","solutions":["Use only defined ApiRequestMethod members: GET, POST, PUT, PATCH, DELETE.","Validate/parse inbound values with Enum.TryParse<ApiRequestMethod> before passing them on.","If you need a verb the library doesn't map, extend ApiRequestMethod and the switch, or use a lower-level HttpClient path."],"exampleFix":"// before\nvar method = (ApiRequestMethod)99; // invalid\n// after\nif (!Enum.TryParse<ApiRequestMethod>(raw, out var method))\n    method = ApiRequestMethod.GET;","handlingStrategy":"type-guard","validationCode":"if (!Enum.IsDefined(typeof(ApiRequestMethod), method))\n    throw new ArgumentException($\"Unsupported ApiRequestMethod: {method}\");","typeGuard":"bool IsValidMethod(ApiRequestMethod m) => m is ApiRequestMethod.GET or ApiRequestMethod.POST or ApiRequestMethod.PUT or ApiRequestMethod.PATCH or ApiRequestMethod.DELETE;","tryCatchPattern":"try { await client.SendAsync(method, url); }\ncatch (ArgumentOutOfRangeException ex) { logger.LogError(ex, \"Unknown HTTP method enum value\"); }","preventionTips":["Only assign ApiRequestMethod from named enum members, never raw ints.","Parse external input with Enum.TryParse and reject unknown values early.","Add unit tests covering every verb your app uses."],"tags":["argument","enum","http-method","wechat-pay"],"backgroundTag":"invalid-enum-value","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"}