Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed uncaught exception when XRefMap is invalid #1785

Merged
merged 1 commit into from
May 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
PathogenDavid marked this conversation as resolved.
Show resolved Hide resolved
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
Loading