{"record":{"id":"6df55386c7a7cb90","repo":"reactiveui/refit","slug":"response-is-successful-cannot-create-an-apiexcept","errorCode":null,"errorMessage":"Response is successful, cannot create an ApiException.","messagePattern":"Response is successful, cannot create an ApiException\\.","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Refit/ApiException.cs","lineNumber":207,"sourceCode":"    /// <param name=\"httpMethod\">The HTTP method used to send the request.</param>\n    /// <param name=\"response\">The HTTP Response message.</param>\n    /// <param name=\"refitSettings\">Refit settings used to sent the request.</param>\n    /// <param name=\"innerException\">Add an inner exception to the <see cref=\"ApiException\"/>.</param>\n    /// <returns>A newly created <see cref=\"ApiException\"/>.</returns>\n    /// <exception cref=\"ArgumentException\"><paramref name=\"response\"/> carries a success status code, so there is no failure to represent.</exception>\n    [SuppressMessage(\"Usage\", \"VSTHRD200:Use \\\"Async\\\" suffix for async methods\", Justification = \"Public API name preserved for backwards compatibility.\")]\n    public static Task<ApiException> Create(\n        HttpRequestMessage message,\n        HttpMethod httpMethod,\n        HttpResponseMessage response,\n        RefitSettings refitSettings,\n        Exception? innerException)\n    {\n        ArgumentExceptionHelper.ThrowIfNull(response);\n\n        if (response.IsSuccessStatusCode)\n        {\n            throw new ArgumentException(\"Response is successful, cannot create an ApiException.\", nameof(response));\n        }\n\n        var exceptionMessage = CreateMessage(response.StatusCode, response.ReasonPhrase);\n        return Create(\n            exceptionMessage,\n            message,\n            httpMethod,\n            response,\n            refitSettings,\n            innerException);\n    }\n\n    /// <summary>Create an instance of <see cref=\"ApiException\"/> with a custom exception message.</summary>\n    /// <param name=\"exceptionMessage\">A custom exception message.</param>\n    /// <param name=\"message\">The HTTP Request message used to send the request.</param>\n    /// <param name=\"httpMethod\">The HTTP method used to send the request.</param>\n    /// <param name=\"response\">The HTTP Response message.</param>\n    /// <param name=\"refitSettings\">Refit settings used to send the request.</param>","sourceCodeStart":189,"sourceCodeEnd":225,"githubUrl":"https://github.com/reactiveui/refit/blob/b455f65ecc4c97d092317e349cb775f9cfc6bcdf/src/Refit/ApiException.cs#L189-L225","documentation":"Thrown by ApiException.Create when the supplied HttpResponseMessage has a successful status code. ApiException represents a failure, so constructing one from a 2xx response is a programming error and is rejected at the argument-validation step before any content parsing.","triggerScenarios":"Calling ApiException.Create(...) (or a code path that calls it) with a response whose IsSuccessStatusCode is true. Usually this is reached indirectly — e.g. a custom HttpMessageHandler or a factory that builds an ApiException unconditionally for every response, not just failures.","commonSituations":"Writing custom error-handling/middleware that wraps ApiException.Create without first checking IsSuccessStatusCode; a test helper that fabricates exceptions for all responses; refactoring that changed which responses flow into the error path.","solutions":["Guard the call site: only invoke ApiException.Create when !response.IsSuccessStatusCode.","If building exceptions in a custom handler/factory, branch on the status code first.","In tests, ensure the HttpResponseMessage you feed in carries an error status (>=400)."],"exampleFix":"// before\nvar ex = await ApiException.Create(req, method, response, settings);\n\n// after\nif (!response.IsSuccessStatusCode)\n{\n    var ex = await ApiException.Create(req, method, response, settings);\n    // handle error\n}","handlingStrategy":"validation","validationCode":"// Only build an ApiException for failure responses.\nif (!response.IsSuccessStatusCode)\n{\n    var ex = await ApiException.Create(req, method, response, settings);\n    // ... handle\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always branch on IsSuccessStatusCode before constructing an ApiException.","In custom handlers/factories, treat 2xx as success and never route them to the error builder.","In tests, give fabricated responses a 4xx/5xx status if they're meant to become ApiExceptions."],"tags":["api-exception","validation","status-code"],"backgroundTag":null,"analyzedSha":"b455f65ecc4c97d092317e349cb775f9cfc6bcdf","analyzedAt":"2026-08-13T21:20:57.878Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}