subhra74/xdm · error · HttpException

Invalid response

Error message

Invalid response

What it means

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.

Solutions

  1. Log or inspect the statusCode on the thrown HttpException to determine the real server response
  2. Ensure the calling code passes a meaningful statusDescription so the error message is actionable
  3. Check the URL and server behavior; non-200/206 statuses indicate the resource or auth is not valid
  4. If the endpoint legitimately returns other success codes, handle the status before calling this extension

Example fix

// before
ext.EnsureSuccessStatusCode(statusCode, null); // message becomes "Invalid response"
// after
ext.EnsureSuccessStatusCode(statusCode,
    response?.StatusDescription ?? $"Server returned {(int)statusCode} {statusCode}");
Defensive patterns

Strategy: try-catch

Validate before calling

// Check status yourself first so description is never null
if (statusCode != HttpStatusCode.OK && statusCode != HttpStatusCode.PartialContent)
{
    Log.Warn($"Non-success status {(int)statusCode}: {statusDescription}");
}
ext.EnsureSuccessStatusCode(statusCode, statusDescription);

Try / catch

try
{
    ext.EnsureSuccessStatusCode(statusCode, statusDescription);
}
catch (HttpException ex)
{
    Log.Error($"Request failed with status {(int)ex.StatusCode}");
    throw;
}

Prevention

When it happens

Trigger: Calling EnsureSuccessStatusCode(HttpStatusCode, string?) with any status other than OK or PartialContent while the caller passed a null statusDescription, producing the message 'Invalid response'.

Common situations: 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.

Related errors


AI-assisted analysis of subhra74/xdm@1ca5a25aae (2026-09-13). Data as JSON: /api/errors/83146798f67a6dcf. Report an issue: GitHub.

Appendix: source

Thrown at app/XDM/XDM.Core/Clients/Http/WebRequestExtensions.cs:27

using XDM.Core.Util;

namespace XDM.Core.Clients.Http
{
    public static class WebRequestExtensions
    {
        public static void EnsureSuccessStatusCode(this HttpWebResponse response)
        {
            if (response.StatusCode != HttpStatusCode.OK && response.StatusCode != HttpStatusCode.PartialContent)
            {
                throw new HttpException(response.StatusDescription, null, response.StatusCode);
            }
        }

        public static void EnsureSuccessStatusCode(HttpStatusCode statusCode, string? statusDescription)
        {
            if (statusCode != HttpStatusCode.OK && statusCode != HttpStatusCode.PartialContent)
            {
                throw new HttpException(statusDescription ?? "Invalid response", null, statusCode);
            }
        }

        public static void Discard(this HttpWebResponse response)
        {
#if NET35
            var bytes = new byte[8192];
#else
            var bytes = System.Buffers.ArrayPool<byte>.Shared.Rent(8192);
#endif
            try
            {
                using var stream = response.GetResponseStream();
                while (true)
                {
                    int x = stream.Read(bytes, 0, bytes.Length);
                    if (x == 0) break;
                }

View on GitHub (pinned to 1ca5a25aae)