3

I am using AllowHttpStatus to disable exception throwing on HTTP errors and to handle exceptions by myself. Unfortunately, I still get an exception.

Flurl.Http.FlurlHttpException. Call failed with status code 500 (Internal Server Error): POST http://MyUrl Request

The code

var request = new Url("http://MyUrl").AllowHttpStatus();

var content = new FileContent(Conversion.SourceFile.FileInfo.ToString());

var task = request.PostAsync(content, model.CancellationToken);
using (var httpStream = await task.ReceiveStream())
using (var fileStream = new FileStream(DestinationLocation + @"\result." + model.DestinationFileFormat, FileMode.CreateNew))
{
    await httpStream.CopyToAsync(fileStream);
}

//This line is never reached if HTTP Exception is thrown by PostAsync   
if (task.Result.StatusCode != HttpStatusCode.OK)
{
    if (task.Result.StatusCode != HttpStatusCode.InternalServerError)
    {
        Logger.Main.LogCritical($"Exception {task.Result.ReasonPhrase}");
    }

    throw new ApiException(ResponseMessageType.ConversionFailed);
}

Why is AllowHttpStatus not working as expected?

DavidG
  • 113,891
  • 12
  • 217
  • 223
Tomas
  • 17,551
  • 43
  • 152
  • 257
  • Furl apears to be not a .NET Library, so it propably ignores such an atribute. | As for how to handle it: HTTP errors tend to be in the Exogenous realm of this classification: https://blogs.msdn.microsoft.com/ericlippert/2008/09/10/vexing-exceptions/ – Christopher Nov 12 '19 at 12:39
  • @Christopher Flurl **is** a .NET library – DavidG Nov 12 '19 at 12:50
  • @DavidG: I could not find it in the .NET or .NET core documentation. Only as a seperate thing on GitHub, Nuget and it's own page. At best it is a inofficial companion Library. | But it is good to know it does follow those Attributes. Or at least the code it (likely) wraps around does. – Christopher Nov 12 '19 at 12:52
  • 1
    @Christopher So you meant to say it's not part of the core libraries, which you could say for 99% of the .NET ecosystem. – DavidG Nov 12 '19 at 12:54
  • @DavidG Maybe we got a different definition of Core Libraries? This is what I call CoreLibraries/Part of .NET: https://learn.microsoft.com/en-us/dotnet/api/ | Furl has all the signs of being a 3rd Party Library for .NET. Same as very .exe and .dll I ever made. Wich is fully useable per the MSIL/CLR rules, but not a offcial thing. So it might miss some patterns/rules. – Christopher Nov 12 '19 at 13:14
  • @Christopher But you didn't say "Core", that's the confusion here. – DavidG Nov 12 '19 at 14:07
  • @Christopher Yes, Flurl is a 3rd party library, but I'm not sure how that's relevant to the question. When you say "it propably ignores such an atribute" and " it might miss some patterns/rules", I'm not sure what exactly are you're referring to, but the `AllowHttpStatus` method is defined in Flurl. It's not ignoring its own methods. – Todd Menier Nov 12 '19 at 14:08
  • @ToddMenier I said: "Furl apears to be not a .NET Library, so it propably ignores such an atribute." | As it turns out from the accepted answer, it was just the Atribute being improperly used. | I said: "But it is good to know it does follow those Attributes. Or at least the code it (likely) wraps around does." Admitting I was wrong. | So what is the issue? – Christopher Nov 12 '19 at 14:13

1 Answers1

1

AllowHttpStatus takes parameters. Without them, it has no effect. So in this case you need to pass it HttpStatusCode.InternalServerError for example:

var request = new Url("http://MyUrl").AllowHttpStatus(HttpStatusCode.InternalServerError);

Alternatively use AllowAnyHttpStatus instead:

var request = new Url("http://MyUrl").AllowAnyHttpStatus();
DavidG
  • 113,891
  • 12
  • 217
  • 223