Skip to content

Commit 2cfe908

Browse files
committed
fix: add error handling
1 parent e0995f1 commit 2cfe908

File tree

7 files changed

+28
-18
lines changed

7 files changed

+28
-18
lines changed

SimpleList/App.xaml.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,26 +37,26 @@ public App()
3737
AppDomain.CurrentDomain.UnhandledException += (sender, args) =>
3838
{
3939
var exception = args.ExceptionObject as Exception;
40-
LogError("Unhandled Exception", exception);
40+
LogError("Unhandled Exception", sender, exception);
4141
};
4242
TaskScheduler.UnobservedTaskException += (sender, args) =>
4343
{
44-
LogError("Unobserved Task Exception", args.Exception);
44+
LogError("Unobserved Task Exception", sender, args.Exception);
4545
args.SetObserved();
4646
};
4747

4848
Application.Current.UnhandledException += (sender, args) =>
4949
{
50-
LogError("UI Thread Exception", args.Exception);
50+
LogError("UI Thread Exception", sender, args.Exception);
5151
args.Handled = true;
5252
};
53-
LogError("UI Thread Exception", new Exception());
53+
LogError("UI Thread Exception", new object { }, new Exception());
5454
}
5555

56-
private static void LogError(string title, Exception exception)
56+
public static void LogError(string title, object sender, Exception exception)
5757
{
5858
var logFilePath = Path.Combine(Environment.CurrentDirectory, "error.log");
59-
var logMessage = $"{DateTime.Now}: {title}\n{exception}\n\n";
59+
var logMessage = $"{DateTime.Now}: {title}\n{sender}\n{exception}\n\n";
6060

6161
File.AppendAllText(logFilePath, logMessage);
6262
}

SimpleList/Models/Tools/ShareCommunity.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
namespace SimpleList.Models
55
{
6-
public class Link
6+
public class ShareCommunityLink
77
{
88
public int ID { get; set; }
99
public DateTime CreatedAt { get; set; }
@@ -21,13 +21,13 @@ public class Link
2121
public class LinkResponse
2222
{
2323
public int code { get; set; }
24-
public Link data { get; set; }
24+
public ShareCommunityLink data { get; set; }
2525
}
2626

2727
public class LinksResponse
2828
{
2929
public int code { get; set; }
30-
public Link[] data { get; set; }
30+
public ShareCommunityLink[] data { get; set; }
3131
}
3232

3333
public class CreateLinkResponse : LinkResponse

SimpleList/Pages/Tools/ShareCommunity.xaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
d:DataContext="{x:Bind vm:ShareCommunityViewModel}">
1313

1414
<Page.Resources>
15-
<DataTemplate x:Key="LinkTemplate" x:DataType="models:Link">
15+
<DataTemplate x:Key="LinkTemplate" x:DataType="models:ShareCommunityLink">
1616
<Grid>
1717
<Grid.ColumnDefinitions>
1818
<ColumnDefinition />

SimpleList/Pages/Tools/ShareCommunity.xaml.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ private async void ShowLinkDetailsDialogAsync(object sender, Microsoft.UI.Xaml.R
2424
LinkDetails dialog = new()
2525
{
2626
XamlRoot = XamlRoot,
27-
DataContext = new LinkDetailsViewModel((sender as Button).DataContext as Link)
27+
DataContext = new LinkDetailsViewModel((sender as Button).DataContext as ShareCommunityLink)
2828
};
2929
await dialog.ShowAsync();
3030
}

SimpleList/SimpleList.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@
7373
</ItemGroup>
7474
<ItemGroup>
7575
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.4.0" />
76-
<PackageReference Include="CommunityToolkit.WinUI.Converters" Version="8.2.250402" />
76+
<PackageReference Include="CommunityToolkit.WinUI.Converters" Version="8.1.240916" />
7777
<PackageReference Include="CommunityToolkit.WinUI.Extensions" Version="8.2.250402" />
7878
<PackageReference Include="CommunityToolkit.WinUI.UI.Controls.Markdown" Version="7.1.2" />
7979
<PackageReference Include="Downloader" Version="3.3.4" />

SimpleList/ViewModels/Tools/LinkDetailsViewModel.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ namespace SimpleList.ViewModels.Tools
55
{
66
partial class LinkDetailsViewModel : ObservableObject
77
{
8-
public LinkDetailsViewModel(Link link)
8+
public LinkDetailsViewModel(ShareCommunityLink link)
99
{
1010
_link = link;
1111
}
@@ -14,7 +14,7 @@ public LinkDetailsViewModel(string linkId)
1414
{
1515
}
1616

17-
[ObservableProperty]private Link _link;
17+
[ObservableProperty]private ShareCommunityLink _link;
1818
public string Title => Link.title;
1919
public string Content => Link.content;
2020
public string Password => Link.password;

SimpleList/ViewModels/Tools/ShareCommunityViewModel.cs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22
using CommunityToolkit.Mvvm.Input;
33
using Microsoft.Extensions.Configuration;
44
using SimpleList.Models;
5+
using System;
56
using System.Collections.Generic;
67
using System.Net.Http;
78
using System.Text.Json;
89
using System.Text.Json.Serialization;
910
using System.Threading.Tasks;
11+
using WinUICommunity;
1012

1113
namespace SimpleList.ViewModels.Tools
1214
{
@@ -15,15 +17,23 @@ public partial class ShareCommunityViewModel : ObservableObject
1517
[RelayCommand]
1618
public async Task Refresh()
1719
{
18-
string response = await _client.GetStringAsync(_apiUrl + "/api/links");
19-
LinksResponse linksResponse = JsonSerializer.Deserialize(response, LinksResponseJsonContext.Default.LinksResponse);
20-
Links = linksResponse.data;
20+
try
21+
{
22+
string response = await _client.GetStringAsync(_apiUrl + "/api/links");
23+
LinksResponse linksResponse = JsonSerializer.Deserialize(response, LinksResponseJsonContext.Default.LinksResponse);
24+
Links = linksResponse.data;
25+
}
26+
catch (Exception e)
27+
{
28+
Growl.Error(e.Message);
29+
App.LogError("Error fetching links", this, e);
30+
}
2131
}
2232

2333
[JsonSerializable(typeof(LinksResponse), GenerationMode = JsonSourceGenerationMode.Metadata)]
2434
internal partial class LinksResponseJsonContext : JsonSerializerContext { }
2535

26-
[ObservableProperty]private IEnumerable<Link> links;
36+
[ObservableProperty] private IEnumerable<ShareCommunityLink> _links;
2737
private readonly HttpClient _client = new();
2838
private readonly string _apiUrl = new ConfigurationBuilder().AddJsonFile("appsettings.json").Build().GetSection("Tools:ShareCommunity:Url").Value;
2939
}

0 commit comments

Comments
 (0)