Skip to content

Commit d924383

Browse files
Merge pull request #13 from AndrewKeepCoding/feat/implement-winui3localizer-exceptions
feat: Implement WinUI3Localizer exceptions
2 parents d45b771 + 81f405f commit d924383

7 files changed

+78
-23
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using System;
2+
3+
namespace WinUI3Localizer;
4+
5+
public class FailedToGetAvailableLanguagesException : LocalizerException
6+
{
7+
public FailedToGetAvailableLanguagesException(string? message = null, Exception? innerException = null)
8+
: base(message, innerException)
9+
{
10+
}
11+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System;
2+
3+
namespace WinUI3Localizer;
4+
5+
public class FailedToGetLocalizedStringException : LocalizerException
6+
{
7+
public FailedToGetLocalizedStringException(string uid, string? message = null, Exception? innerException = null)
8+
: base(message, innerException)
9+
{
10+
Uid = uid;
11+
}
12+
13+
public string Uid { get; }
14+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System;
2+
3+
namespace WinUI3Localizer;
4+
5+
public class FailedToSetLanguageException : LocalizerException
6+
{
7+
public FailedToSetLanguageException(string oldLanguage, string newLanguage, string? message = null, Exception? innerException = null)
8+
: base(message, innerException)
9+
{
10+
OldLanguage = oldLanguage;
11+
NewLanguage = newLanguage;
12+
}
13+
14+
public string OldLanguage { get; }
15+
16+
public string NewLanguage { get; }
17+
}

WinUI3Localizer/LocalizerException.cs renamed to WinUI3Localizer/Exceptions/LocalizerException.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
using System;
1+
using System;
22

33
namespace WinUI3Localizer;
44

55
public class LocalizerException : Exception
66
{
7-
public LocalizerException(string message, Exception? innerException = null)
7+
public LocalizerException(string? message = null, Exception? innerException = null)
88
: base(message, innerException)
99
{
1010
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using System;
2+
3+
namespace WinUI3Localizer;
4+
5+
public class LocalizerIsAlreadyBuiltException : LocalizerException
6+
{
7+
public LocalizerIsAlreadyBuiltException(string? message = null, Exception? innerException = null)
8+
: base(message, innerException)
9+
{
10+
}
11+
}

WinUI3Localizer/Localizer.cs

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -52,16 +52,12 @@ public IEnumerable<string> GetAvailableLanguages()
5252
.Select(x => x.Language)
5353
.ToArray();
5454
}
55-
catch (LocalizerException)
56-
{
57-
throw;
58-
}
5955
catch (Exception exception)
6056
{
61-
ThrowLocalizerException(exception, "Failed to get available languages.");
57+
FailedToGetAvailableLanguagesException localizerException = new(innerException: exception);
58+
Logger.LogError(localizerException, localizerException.Message);
59+
throw localizerException;
6260
}
63-
64-
return Array.Empty<string>();
6561
}
6662

6763
public string GetCurrentLanguage() => CurrentDictionary.Language;
@@ -89,11 +85,10 @@ public async Task SetLanguage(string language)
8985
}
9086
catch (Exception exception)
9187
{
92-
ThrowLocalizerException(exception, "Exception setting language. [{PreviousLanguage} -> {NextLanguage}]", previousLanguage, language);
88+
FailedToSetLanguageException localizerException = new(previousLanguage, language, message: string.Empty, innerException: exception);
89+
Logger.LogError(localizerException, localizerException.Message);
90+
throw localizerException;
9391
}
94-
95-
ThrowLocalizerException(innerException: null, "Failed to set language. [{PreviousLanguage} -> {NextLanguage}]", previousLanguage, language);
96-
return;
9792
}
9893

9994
public string GetLocalizedString(string uid)
@@ -111,9 +106,15 @@ public string GetLocalizedString(string uid)
111106
return item.Value;
112107
}
113108
}
109+
catch (LocalizerException)
110+
{
111+
throw;
112+
}
114113
catch (Exception exception)
115114
{
116-
ThrowLocalizerException(exception, "Failed to get localized string. [Uid: {Uid}]", uid);
115+
FailedToGetLocalizedStringException localizerException = new(uid, innerException: exception);
116+
Logger.LogError(localizerException, localizerException.Message);
117+
throw localizerException;
117118
}
118119

119120
return this.options.UseUidWhenLocalizedStringNotFound is true
@@ -135,9 +136,15 @@ public IEnumerable<string> GetLocalizedStrings(string uid)
135136
return items.Select(x => x.Value);
136137
}
137138
}
139+
catch (LocalizerException)
140+
{
141+
throw;
142+
}
138143
catch (Exception exception)
139144
{
140-
ThrowLocalizerException(exception, "Failed to get localized string. [Uid: {Uid}]", uid);
145+
FailedToGetLocalizedStringException localizerException = new(uid, innerException: exception);
146+
Logger.LogError(localizerException, localizerException.Message);
147+
throw localizerException;
141148
}
142149

143150
return this.options.UseUidWhenLocalizedStringNotFound is true
@@ -228,13 +235,6 @@ private static void DependencyObjectsReferences_DependencyObjectRemoved(object?
228235
e.ItemsTotal);
229236
}
230237

231-
private static void ThrowLocalizerException(Exception? innerException, string message, params object?[] args)
232-
{
233-
LocalizerException localizerException = new(message, innerException);
234-
Logger.LogError(localizerException, message, args);
235-
throw localizerException;
236-
}
237-
238238
private static DependencyProperty? GetDependencyProperty(DependencyObject dependencyObject, string dependencyPropertyName)
239239
{
240240
Type type = dependencyObject.GetType();

WinUI3Localizer/LocalizerBuilder.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,9 @@ public async Task<ILocalizer> Build()
8888
{
8989
if (IsLocalizerAlreadyBuilt is true)
9090
{
91-
throw new LocalizerException($"{nameof(Localizer)} is already built.");
91+
LocalizerIsAlreadyBuiltException localizerException = new();
92+
Logger?.LogError(localizerException, localizerException.Message);
93+
throw localizerException;
9294
}
9395

9496
Localizer localizer = new(this.options);

0 commit comments

Comments
 (0)