Skip to content

Commit

Permalink
Merge pull request #1785 from PathogenDavid/issue-1507
Browse files Browse the repository at this point in the history
Fixed uncaught exception when XRefMap is invalid
  • Loading branch information
glopesdev authored May 10, 2024
2 parents ced27db + faad80b commit d55338f
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 10 deletions.
19 changes: 19 additions & 0 deletions Bonsai.Editor/DocumentationException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;

namespace Bonsai.Editor
{
internal sealed class DocumentationException : Exception
{
public DocumentationException()
: base()
{ }

public DocumentationException(string message)
: base(message)
{ }

public DocumentationException(string message, Exception innerException)
: base(message, innerException)
{ }
}
}
29 changes: 21 additions & 8 deletions Bonsai.Editor/DocumentationHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Net;
using System.Net.Cache;
using System.Threading.Tasks;
using YamlDotNet.Core;
using YamlDotNet.Serialization;
using YamlDotNet.Serialization.NamingConventions;

Expand Down Expand Up @@ -36,16 +37,14 @@ static async Task<Dictionary<string, string>> GetXRefMapAsync(string baseUrl, pa
throw new ArgumentException("No downstream URLs have been specified.", nameof(hrefs));
}

WebException lastException = default;
Exception lastException = default;
for (int i = 0; i < hrefs.Length; i++)
{
try { return await GetXRefMapAsync($"{baseUrl}{hrefs[i]}"); }
catch (WebException ex) when (ex.Response is HttpWebResponse httpResponse &&
httpResponse.StatusCode is HttpStatusCode.NotFound)
{
lastException = ex;
continue;
}
catch (WebException ex) when (ex.Response is HttpWebResponse { StatusCode: HttpStatusCode.NotFound })
{ lastException ??= ex; } // Always prefer a DocumentationException as it'll be more specific
catch (DocumentationException ex)
{ lastException = ex; }
}

throw lastException;
Expand All @@ -61,11 +60,25 @@ static async Task<Dictionary<string, string>> GetXRefMapAsync(string baseUrl)
using var response = await request.GetResponseAsync();
var stream = response.GetResponseStream();
using var reader = new StreamReader(stream);

if (reader.ReadLine().Trim() != "### YamlMime:XRefMap")
throw new DocumentationException("The documentation server did not respond with a cross-reference map.");

var deserializer = new DeserializerBuilder()
.WithNamingConvention(CamelCaseNamingConvention.Instance)
.IgnoreUnmatchedProperties()
.Build();
var xrefmap = deserializer.Deserialize<XRefMap>(reader);

XRefMap xrefmap;
try { xrefmap = deserializer.Deserialize<XRefMap>(reader); }
catch (YamlException ex)
{
throw new DocumentationException("The cross-reference map returned by the documentation server is malformed.", ex);
}

if (xrefmap.References is null)
throw new DocumentationException("The cross-reference map returned by the documentation server is malformed.");

return xrefmap.References.ToDictionary(
reference => reference.Uid,
reference => $"{baseUrl}/{reference.Href}");
Expand Down
4 changes: 2 additions & 2 deletions Bonsai.Editor/EditorForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2366,12 +2366,12 @@ private async Task OpenDocumentationAsync(string assemblyName, string uid)
var message = $"The specified operator {uid} was not found in the documentation for {assemblyName}.";
editorSite.ShowError(ex, message);
}
catch (SystemException ex) when (ex is WebException || ex is NotSupportedException)
catch (Exception ex) when (ex is WebException or NotSupportedException or DocumentationException)
{
var message = $"The documentation for the module {assemblyName} is not available. {{0}}";
editorSite.ShowError(ex, message);
}
catch (SystemException ex)
catch (Exception ex)
{
editorSite.ShowError(ex);
}
Expand Down

0 comments on commit d55338f

Please sign in to comment.