Skip to content

Commit

Permalink
Added Constructor for ErrorEvent.
Browse files Browse the repository at this point in the history
  • Loading branch information
KristofferStrube committed Aug 26, 2024
1 parent 43e7d8e commit 298ba09
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/KristofferStrube.Blazor.Window/Events/ErrorEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,20 @@ public class ErrorEvent : Event, IJSCreatable<ErrorEvent>
/// </summary>
protected readonly Lazy<Task<IJSObjectReference>> windowHelperTask;

/// <summary>
/// Creates an <see cref="ErrorEvent"/> using the standard constructor.
/// </summary>
/// <param name="jSRuntime">An <see cref="IJSRuntime"/> instance.</param>
/// <param name="type">The type of the <see cref="Event"/>.</param>
/// <param name="eventInitDict">Extra options for setting options specific to the the <see cref="ErrorEvent"/>.</param>
/// <returns>A new instance of an <see cref="ErrorEvent"/>.</returns>
public static async Task<ErrorEvent> CreateAsync(IJSRuntime jSRuntime, string type, ErrorEventInit? eventInitDict = null)
{
await using IJSObjectReference helper = await jSRuntime.GetHelperAsync();
IJSObjectReference jSInstance = await helper.InvokeAsync<IJSObjectReference>("constructErrorEvent", type, eventInitDict);
return new ErrorEvent(jSRuntime, jSInstance, new() { DisposesJSReference = true });
}

/// <inheritdoc/>
public static new async Task<ErrorEvent> CreateAsync(IJSRuntime jSRuntime, IJSObjectReference jSReference)
{
Expand Down
43 changes: 43 additions & 0 deletions src/KristofferStrube.Blazor.Window/Events/ErrorEventInit.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using KristofferStrube.Blazor.DOM;
using Microsoft.JSInterop;
using System.Text.Json.Serialization;

namespace KristofferStrube.Blazor.Window;

/// <summary>
/// The options specific to initializing an <see cref="ErrorEvent"/>.
/// </summary>
/// <remarks><see href="https://html.spec.whatwg.org/#erroreventinit">See the API definition here</see>.</remarks>
public class ErrorEventInit : EventInit
{
/// <summary>
/// It represents the error message.
/// </summary>
[JsonPropertyName("message")]
public string Message { get; set; } = "";

/// <summary>
/// It represents the URL of the script in which the error originally occurred.
/// </summary>
[JsonPropertyName("filename")]
public string FileName { get; set; } = "";

/// <summary>
/// It represents the line number where the error occurred in the script.
/// </summary>
[JsonPropertyName("lineno")]
public ulong LineNumber { get; set; } = 0;

/// <summary>
/// It represents the column number where the error occurred in the script.
/// </summary>
[JsonPropertyName("colno")]
public ulong ColumnNumber { get; set; } = 0;

/// <summary>
/// Where appropriate, it is set to the object representing the error (e.g., the exception object in the case of an uncaught exception).
/// </summary>
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
[JsonPropertyName("error")]
public IJSObjectReference? Error { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

export function setAttribute(object, attribute, value) { object[attribute] = value; }

export function constructErrorEvent(type, eventInitDict) {
return new ErrorEvent(type, eventInitDict);
}

// This is copied from Blazor.WebIDL as we should not depend on JS files from other packages as they could change.
export function formatError(error, extraErrorProperties) {
var name = error.name;
Expand Down

0 comments on commit 298ba09

Please sign in to comment.