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?
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();