Skip to content

Commit

Permalink
feat: add option to copy error information
Browse files Browse the repository at this point in the history
  • Loading branch information
anna-is-cute committed Apr 17, 2024
1 parent fa889f5 commit e68ae91
Showing 1 changed file with 49 additions and 2 deletions.
51 changes: 49 additions & 2 deletions Ui/NotificationProgressManager.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Diagnostics;
using System.Numerics;
using System.Text;
using Dalamud.Interface.ImGuiNotification;
Expand Down Expand Up @@ -110,6 +111,7 @@ private State UpdateNotif(IActiveNotification notif, DownloadTask task) {
var state = task.State;
var sData = task.StateData;
var sMax = task.StateDataMax;
var error = task.Error;

var setIcon = !(this.LastSeenState.TryGetValue(task.TaskId, out var lastState) && lastState == state);
if (setIcon && this.GetStateIcon(state) is { } icon) {
Expand All @@ -132,8 +134,12 @@ private State UpdateNotif(IActiveNotification notif, DownloadTask task) {
var title = sb.ToString();

notif.Title = string.IsNullOrWhiteSpace(title) ? null : title;
notif.Content = sMax == 0
? $"{state.Name()} {sData:N0}"
notif.Content = state == State.Errored
? error == null
? $"{state.Name()}"
: $"{state.Name()} ({error.GetType().Name})"
: sMax == 0
? $"{state.Name()} ({sData:N0})"
: $"{state.Name()} ({sData:N0} / {sMax:N0})";
notif.Progress = sMax == 0
? 0
Expand Down Expand Up @@ -166,6 +172,47 @@ private State UpdateNotif(IActiveNotification notif, DownloadTask task) {
task.OpenModInPenumbra();
}
};
} else if (state == State.Errored && error != null) {
notif.DrawActions += args => {
var copiedTimer = new Stopwatch();
if (copiedTimer.ElapsedMilliseconds > 1_500) {
copiedTimer.Reset();
}
var widthAvail = args.MaxCoord.X - args.MinCoord.X;
ImGui.PushID($"notif-download-{task.TaskId}");
using var popId = new OnDispose(ImGui.PopID);
var label = copiedTimer.IsRunning
? "Copied!"
: "Copy error information";
if (ImGui.Button($"{label}###copy-error-information", new Vector2(widthAvail, 0))) {
var sb = new StringBuilder();
sb.Append("```\n");
var i = 0;
foreach (var ex in error.AsEnumerable()) {
if (i != 0) {
sb.Append('\n');
}
i += 1;
sb.Append($"Error type: {ex.GetType().FullName}\n");
sb.Append($" Message: {ex.Message}\n");
sb.Append($" HResult: 0x{unchecked((uint) ex.HResult):X8}\n");
if (ex.StackTrace is { } trace) {
sb.Append(trace);
sb.Append('\n');
}
}
sb.Append("```");
ImGui.SetClipboardText(sb.ToString());
copiedTimer.Start();
}
};
}

return state;
Expand Down

0 comments on commit e68ae91

Please sign in to comment.