{"record":{"id":"83146798f67a6dcf","repo":"subhra74/xdm","slug":"invalid-response","errorCode":null,"errorMessage":"Invalid response","messagePattern":"Invalid response","errorType":"http","errorClass":"HttpException","httpStatus":null,"severity":"error","filePath":"app/XDM/XDM.Core/Clients/Http/WebRequestExtensions.cs","lineNumber":27,"sourceCode":"using XDM.Core.Util;\r\n\r\nnamespace XDM.Core.Clients.Http\r\n{\r\n    public static class WebRequestExtensions\r\n    {\r\n        public static void EnsureSuccessStatusCode(this HttpWebResponse response)\r\n        {\r\n            if (response.StatusCode != HttpStatusCode.OK && response.StatusCode != HttpStatusCode.PartialContent)\r\n            {\r\n                throw new HttpException(response.StatusDescription, null, response.StatusCode);\r\n            }\r\n        }\r\n\r\n        public static void EnsureSuccessStatusCode(HttpStatusCode statusCode, string? statusDescription)\r\n        {\r\n            if (statusCode != HttpStatusCode.OK && statusCode != HttpStatusCode.PartialContent)\r\n            {\r\n                throw new HttpException(statusDescription ?? \"Invalid response\", null, statusCode);\r\n            }\r\n        }\r\n\r\n        public static void Discard(this HttpWebResponse response)\r\n        {\r\n#if NET35\r\n            var bytes = new byte[8192];\r\n#else\r\n            var bytes = System.Buffers.ArrayPool<byte>.Shared.Rent(8192);\r\n#endif\r\n            try\r\n            {\r\n                using var stream = response.GetResponseStream();\r\n                while (true)\r\n                {\r\n                    int x = stream.Read(bytes, 0, bytes.Length);\r\n                    if (x == 0) break;\r\n                }\r","sourceCodeStart":9,"sourceCodeEnd":45,"githubUrl":"https://github.com/subhra74/xdm/blob/1ca5a25aae007826c859c81bea494e7c102e1242/app/XDM/XDM.Core/Clients/Http/WebRequestExtensions.cs#L9-L45","documentation":"This overload of EnsureSuccessStatusCode takes a statusCode and statusDescription directly (used with non-HttpWebResponse response paths) and throws an HttpException with the given description, or the fallback message 'Invalid response' when the description is null/empty, whenever the status is not 200 OK or 206 Partial Content.","triggerScenarios":"Calling EnsureSuccessStatusCode(HttpStatusCode, string?) with any status other than OK or PartialContent while the caller passed a null statusDescription, producing the message 'Invalid response'.","commonSituations":"HTTP client paths that do not build an HttpWebResponse (e.g. custom/redirect handling) pass a null description along with an error status; server returns 3xx/4xx/5xx that XDM treats as failure.","solutions":["Log or inspect the statusCode on the thrown HttpException to determine the real server response","Ensure the calling code passes a meaningful statusDescription so the error message is actionable","Check the URL and server behavior; non-200/206 statuses indicate the resource or auth is not valid","If the endpoint legitimately returns other success codes, handle the status before calling this extension"],"exampleFix":"// before\next.EnsureSuccessStatusCode(statusCode, null); // message becomes \"Invalid response\"\n// after\next.EnsureSuccessStatusCode(statusCode,\n    response?.StatusDescription ?? $\"Server returned {(int)statusCode} {statusCode}\");","handlingStrategy":"try-catch","validationCode":"// Check status yourself first so description is never null\nif (statusCode != HttpStatusCode.OK && statusCode != HttpStatusCode.PartialContent)\n{\n    Log.Warn($\"Non-success status {(int)statusCode}: {statusDescription}\");\n}\next.EnsureSuccessStatusCode(statusCode, statusDescription);","typeGuard":null,"tryCatchPattern":"try\n{\n    ext.EnsureSuccessStatusCode(statusCode, statusDescription);\n}\ncatch (HttpException ex)\n{\n    Log.Error($\"Request failed with status {(int)ex.StatusCode}\");\n    throw;\n}","preventionTips":["Always pass a meaningful statusDescription so the exception message is diagnostic","Handle non-200/206 statuses upstream when the endpoint uses other success codes","Branch on HttpException.StatusCode, not the message text ('Invalid response' is only a fallback)"],"tags":["http","http-status","response-validation"],"backgroundTag":"http-error-status","analyzedSha":"1ca5a25aae007826c859c81bea494e7c102e1242","analyzedAt":"2026-09-13T20:37:40.968Z","contentChangedAt":"2026-09-13T20:37:40.968Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}