131 lines
5.7 KiB
C#
131 lines
5.7 KiB
C#
using bts.api;
|
|
using bts.api.core;
|
|
using bts.support;
|
|
using mbts;
|
|
using mbts.spark;
|
|
using mbts.ui;
|
|
using System;
|
|
using System.ComponentModel.Composition;
|
|
using System.Reactive.Linq;
|
|
using System.Windows.Threading;
|
|
using System.Windows;
|
|
using System.Threading.Tasks;
|
|
using mbts.ui.shell;
|
|
|
|
namespace bts.spark
|
|
{
|
|
[Export, Export(typeof(IShellDaemon))]
|
|
public class ErrorReporter : IShellDaemon, IDisposable {
|
|
readonly ErrorReportService _svc;
|
|
readonly string _username;
|
|
IDisposable _dispose;
|
|
|
|
[ImportingConstructor]
|
|
public ErrorReporter(ErrorReportService svc, IBus bus, [Import(LoginContainer.Exports.UserName)] string username) {
|
|
_svc = svc;
|
|
_username = username;
|
|
_dispose = bus.subscribe(SparkTags.Support).OnDispatcher().Subscribe(msg => show_support_window(msg, "Report a problem"));
|
|
}
|
|
|
|
public void Dispose() {
|
|
Disposable.dispose(ref _dispose);
|
|
}
|
|
|
|
public void report_error(string message, string title = null, MessageBoxImage? image = null) {
|
|
// Error reports are likely to arrive the api thread (which is not an STA thread)
|
|
Application.Current.Dispatcher.BeginInvoke(async () => {
|
|
var result = await BTSMessageBox.ShowAsync(message, title = "Error", BTSMessageBoxButtons.OK | BTSMessageBoxButtons.Support, image ?? MessageBoxImage.Error);
|
|
if (result == BTSMessageBoxButtons.Support) {
|
|
show_support_window(message, title);
|
|
}
|
|
});
|
|
}
|
|
|
|
public void show_support_window(string message, string title) {
|
|
view.controls.SupportRequestWindow.Show(_svc, new ErrorReport() {
|
|
User = _username,
|
|
ErrorTitle = title,
|
|
ErrorMessage = message,
|
|
});
|
|
}
|
|
}
|
|
|
|
static class ErrorReporterExtensions {
|
|
public static bool handle_error<T, TError>(this ErrorReporter error_reporter, Result<T> result, string template, TError error, bool error_ok) =>
|
|
handle_error(error_reporter, result, template, error, error_ok, false, true);
|
|
|
|
public static TRV handle_error<T, TError, TRV>(this ErrorReporter error_reporter, Result<T> result, string template, TError error, bool error_ok, TRV successful_rv, TRV unsuccessful_rv = default) =>
|
|
handle_error(error_reporter, result, template, _ => error, _ => error_ok, _ => successful_rv, () => unsuccessful_rv).result;
|
|
|
|
public static bool handle_error<T, TError>(this ErrorReporter error_reporter, Result<T> result, string template, Func<T, TError> get_error, Func<TError, bool> get_error_ok) =>
|
|
handle_error(error_reporter, result, template, get_error, get_error_ok, _ => true, () => false).result;
|
|
|
|
public static (TRV result, string error) handle_error<T, TError, TRV>(this ErrorReporter error_reporter, Result<T> result, string template, Func<T, TError> get_error, Func<TError, bool> get_error_ok, Func<T, TRV> get_successful_rv) =>
|
|
handle_error(error_reporter, result, template, get_error, get_error_ok, get_successful_rv, () => default);
|
|
|
|
public static (TRV result, string error) handle_error<T, TError, TRV>(
|
|
this ErrorReporter error_reporter, Result<T> result, string template,
|
|
Func<T, TError> get_error, Func<TError, bool> get_error_ok,
|
|
Func<T, TRV> get_successful_rv, Func<TRV> get_unsuccessful_rv
|
|
) {
|
|
if (result.success) {
|
|
TError error = get_error(result.value);
|
|
if (get_error_ok(error))
|
|
return (get_successful_rv(result.value), string.Empty);
|
|
else {
|
|
string error_msg = template.fmt(error);
|
|
error_reporter.report_error(error_msg);
|
|
return (get_unsuccessful_rv(), error_msg);
|
|
}
|
|
} else {
|
|
error_reporter.report_error(result.error_message);
|
|
return (get_unsuccessful_rv(), result.error_message);
|
|
}
|
|
}
|
|
|
|
public static async Task<(T value, bool success)> safe_execute<T>(this ErrorReporter error_reporter, Func<Task<Result<T>>> execute, Func<T, bool> get_success, Func<T, string> get_error_msg, string desc) {
|
|
try {
|
|
var result = await execute();
|
|
if (!result.success) {
|
|
_report_error(result.error_message);
|
|
} else if (!get_success(result.value)) {
|
|
_report_error(get_error_msg(result.value));
|
|
} else {
|
|
return (result.value, true);
|
|
}
|
|
} catch (Exception ex) {
|
|
_report_error(ex.ToString());
|
|
}
|
|
return (default, false);
|
|
//
|
|
void _report_error(string msg) =>
|
|
error_reporter.report_error($"Error {desc}: {msg}", AlgoStudioWindow.NAME, MessageBoxImage.Warning);
|
|
}
|
|
|
|
public static async Task<TResult> safe_execute<T, TResult>(
|
|
this ErrorReporter error_reporter,
|
|
Func<Task<Result<T>>> execute,
|
|
Func<T, bool> get_success,
|
|
Func<T, string> get_error_msg,
|
|
Func<T, bool, TResult> result_selector,
|
|
string desc) {
|
|
//
|
|
try {
|
|
var result = await execute();
|
|
if (!result.success) {
|
|
_report_error(result.error_message);
|
|
} else if (!get_success(result.value)) {
|
|
_report_error(get_error_msg(result.value));
|
|
} else {
|
|
return result_selector(result.value, true);
|
|
}
|
|
} catch (Exception ex) {
|
|
_report_error(ex.ToString());
|
|
}
|
|
return result_selector(default, false);
|
|
//
|
|
void _report_error(string msg) =>
|
|
error_reporter.report_error($"Error {desc}: {msg}", AlgoStudioWindow.NAME, MessageBoxImage.Warning);
|
|
}
|
|
}
|
|
} |