You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
56 lines
1.7 KiB
56 lines
1.7 KiB
using System.Diagnostics.CodeAnalysis;
|
|
using Recyclarr.Common.Extensions;
|
|
|
|
namespace Recyclarr.Cli.Processors.ErrorHandling;
|
|
|
|
public class FlurlHttpExceptionHandler(ILogger log) : IFlurlHttpExceptionHandler
|
|
{
|
|
[SuppressMessage("Design", "CA1031:Do not catch general exception types")]
|
|
public async Task ProcessServiceErrorMessages(IServiceErrorMessageExtractor extractor)
|
|
{
|
|
switch (extractor)
|
|
{
|
|
case {HttpStatusCode: 401}:
|
|
log.Error("Reason: Recyclarr is unauthorized to talk to the service. Is your `api_key` correct?");
|
|
break;
|
|
|
|
case {HttpStatusCode: null}:
|
|
log.Error("Reason: Problem connecting to service. Is your `base_url` correct?");
|
|
break;
|
|
|
|
default:
|
|
ProcessBody(await extractor.GetErrorMessage());
|
|
break;
|
|
}
|
|
}
|
|
|
|
private void ProcessBody(string responseBody)
|
|
{
|
|
var parser = new ErrorResponseParser(log, responseBody);
|
|
|
|
// Try to parse validation errors
|
|
if (parser.DeserializeList(s => s
|
|
.Select(x => x.GetProperty("errorMessage").GetString())
|
|
.NotNull(x => !string.IsNullOrEmpty(x))))
|
|
{
|
|
return;
|
|
}
|
|
|
|
// Try to parse single error message
|
|
if (parser.Deserialize(s => s.GetProperty("message").GetString()))
|
|
{
|
|
return;
|
|
}
|
|
|
|
// A list of errors with a title
|
|
if (parser.DeserializeServiceErrorList())
|
|
{
|
|
return;
|
|
}
|
|
|
|
// Last resort
|
|
log.Error(
|
|
"Reason: Unable to determine. Please report this as a bug and attach your `debug.log` and `verbose.log` files.");
|
|
}
|
|
}
|