Skip to content

Commit

Permalink
Merge pull request #53 from Zastai/update-dependencies
Browse files Browse the repository at this point in the history
Update dependencies & ad more error details
  • Loading branch information
Zastai authored Dec 18, 2023
2 parents 9c280dc + 7fb1b4c commit 6c6c514
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<!-- Package Versions -->
<ItemGroup>
<PackageVersion Include="JetBrains.Annotations" Version="2023.3.0" />
<PackageVersion Include="MetaBrainz.Common" Version="2.1.0" />
<PackageVersion Include="MetaBrainz.Common" Version="3.0.0" />
<PackageVersion Include="MetaBrainz.Common.Json" Version="6.0.0" />
<PackageVersion Include="System.Drawing.Common" Version="6.0.0" Condition=" '$(TargetFramework)' == 'net6.0' " />
<PackageVersion Include="System.Drawing.Common" Version="8.0.0" Condition=" '$(TargetFramework)' == 'net8.0' " />
Expand Down
57 changes: 49 additions & 8 deletions MetaBrainz.MusicBrainz.CoverArt/CoverArt.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
using System;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text.Json;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;

Expand All @@ -21,7 +23,7 @@ namespace MetaBrainz.MusicBrainz.CoverArt;

/// <summary>Class providing access to the CoverArt Archive API.</summary>
[PublicAPI]
public class CoverArt : IDisposable {
public partial class CoverArt : IDisposable {

#region Static Fields / Properties

Expand Down Expand Up @@ -734,6 +736,31 @@ private static async Task<IRelease> ParseReleaseAsync(HttpResponseMessage respon
return await jsonTask.ConfigureAwait(false) ?? throw new JsonException("Received a null release.");
}

// Error Response Contents:
// <!doctype html>
// <html lang=en>
// <title>404 Not Found</title>
// <h1>Not Found</h1>
// <p>No cover art found for release 968db8b7-c519-43e5-bb45-9f244c92b670</p>
#if NET7_0_OR_GREATER
[StringSyntax(StringSyntaxAttribute.Regex)]
#endif
private const string ErrorResponseContentPatternText =
@"^(?:.*\n)*\s*<title>(\d+)?\s*(.*?)\s*</title>\s*<h1>\s*(.*?)\s*</h1>\s*<p>\s*(.*?)\s*</p>\s*$";

#if NET6_0

private static readonly Regex TheErrorResponseContentPattern = new(CoverArt.ErrorResponseContentPatternText);

private static Regex ErrorResponseContentPattern() => CoverArt.TheErrorResponseContentPattern;

#else

[GeneratedRegex(CoverArt.ErrorResponseContentPatternText)]
private static partial Regex ErrorResponseContentPattern();

#endif

private async Task<HttpResponseMessage> PerformRequestAsync(string address, CancellationToken cancellationToken) {
var ts = CoverArt.TraceSource;
ts.TraceEvent(TraceEventType.Verbose, 1, "CAA REQUEST: GET {0}{1}", this.BaseUri, address);
Expand All @@ -751,13 +778,27 @@ private async Task<HttpResponseMessage> PerformRequestAsync(string address, Canc
}
catch (HttpError error) {
if (!string.IsNullOrEmpty(error.Content) && error.ContentHeaders?.ContentType?.MediaType == "text/html") {
// The contents seem to be of the form:
// <!doctype html>
// <html lang=en>
// <title>404 Not Found</title>
// <h1>Not Found</h1>
// <p>No cover art found for release 968db8b7-c519-43e5-bb45-9f244c92b670</p>
// FIXME: It may make sense to try and extract the contents of that paragraph for use in the exception.
var match = CoverArt.ErrorResponseContentPattern().Match(error.Content);
if (match.Success) {
var code = match.Groups[1].Success ? match.Groups[1].Value : null;
var title = match.Groups[2].Value;
var heading = match.Groups[3].Value;
var message = match.Groups[4].Value;
if (int.TryParse(code, NumberStyles.None, CultureInfo.InvariantCulture, out var status)) {
if (status != (int) error.Status) {
ts.TraceEvent(TraceEventType.Verbose, 5, "STATUS CODE MISMATCH: {0} <> {1}", status, (int) error.Status);
}
}
else {
ts.TraceEvent(TraceEventType.Verbose, 6, "STATUS CODE MISSING FROM TITLE");
status = (int) error.Status;
}
if (title != heading) {
ts.TraceEvent(TraceEventType.Verbose, 7, "TITLE/HEADING MISMATCH: '{0}' <> '{1}'", title, heading);
message = $"{heading}: {message}";
}
throw new HttpError((HttpStatusCode) status, title, error.Version, message, error);
}
}
throw;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<Project>

<Sdk Name="MetaBrainz.Build.Sdk" Version="3.1.1" />
<Sdk Name="MetaBrainz.Build.Sdk" Version="3.1.2" />

<PropertyGroup>
<Authors>Zastai</Authors>
Expand Down

0 comments on commit 6c6c514

Please sign in to comment.