{"record":{"id":"5cb849cd192b2658","repo":"subhra74/xdm","slug":"response-statusdescription","errorCode":null,"errorMessage":"response.StatusDescription","messagePattern":"response\\.StatusDescription","errorType":"http","errorClass":"HttpException","httpStatus":null,"severity":"error","filePath":"app/XDM/XDM.Core/Clients/Http/WebRequestExtensions.cs","lineNumber":19,"sourceCode":"﻿using System;\r\nusing System.IO;\r\nusing System.Linq;\r\nusing System.Net;\r\nusing System.Text;\r\nusing System.Text.RegularExpressions;\r\nusing TraceLog;\r\nusing XDM.Core;\r\nusing 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","sourceCodeStart":1,"sourceCodeEnd":37,"githubUrl":"https://github.com/subhra74/xdm/blob/1ca5a25aae007826c859c81bea494e7c102e1242/app/XDM/XDM.Core/Clients/Http/WebRequestExtensions.cs#L1-L37","documentation":"WebRequestExtensions.EnsureSuccessStatusCode(HttpWebResponse) validates that the response status is 200 OK or 206 Partial Content. Any other status throws an HttpException whose message is the response's StatusDescription (e.g. 'Not Found', 'Forbidden'). This is XDM's way of turning non-success HTTP statuses into a typed exception.","triggerScenarios":"Calling EnsureSuccessStatusCode on an HttpWebResponse whose StatusCode is anything other than OK (200) or PartialContent (206), such as 404, 403, 500, or 302 responses that were not redirected.","commonSituations":"Download URL returns 404 after the file was moved or deleted; server returns 403 due to missing/expired auth cookies or referer checks; server errors (5xx) during download; range requests rejected with 416.","solutions":["Inspect the HttpException StatusCode property to identify the exact HTTP status and handle it (e.g. refresh auth for 403, re-query the URL for 404)","Verify the download URL is still valid by fetching it fresh; expired links commonly 404 or 410","Re-acquire cookies/headers from the browser if the server returns 401/403","If the server misbehaves (5xx), retry later or fall back to a different mirror/quality"],"exampleFix":"// before\nresponse.EnsureSuccessStatusCode(); // throws on any non-200/206\n// after\ntry\n{\n    response.EnsureSuccessStatusCode();\n}\ncatch (HttpException ex)\n{\n    if ((int)ex.StatusCode == 404) RefreshDownloadUrl();\n    else if ((int)ex.StatusCode == 403) RefreshAuthCookies();\n    else throw;\n}","handlingStrategy":"try-catch","validationCode":"// Validate expected status before calling the extension\nif (response.StatusCode != HttpStatusCode.OK && response.StatusCode != HttpStatusCode.PartialContent)\n{\n    Log.Warn($\"Server returned {(int)response.StatusCode} {response.StatusDescription}\");\n}\nresponse.EnsureSuccessStatusCode();","typeGuard":null,"tryCatchPattern":"try\n{\n    response.EnsureSuccessStatusCode();\n}\ncatch (HttpException ex)\n{\n    switch ((int)ex.StatusCode)\n    {\n        case 404: RefreshDownloadUrl(); break;\n        case 401: case 403: RefreshAuthCookies(); break;\n        default: throw; // server error, retry later\n    }\n}","preventionTips":["Catch HttpException and branch on StatusCode rather than the text message","Refresh cookies/URLs from the browser for 401/403 before retrying","Treat 5xx as transient and back off before retrying","Validate that download URLs are still live before starting long downloads"],"tags":["http","http-status","download"],"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"}