Skip to content

Commit

Permalink
Instead of throw the exception if the culture code is not valid, shou…
Browse files Browse the repository at this point in the history
…ld reset it and log it instead.
  • Loading branch information
andy840119 committed Oct 1, 2023
1 parent 7ba2fa9 commit 95b1d46
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,11 @@ public void TestParsingCultureInfo(int value, int expectedLcid)
var actual = bindable.Value;
Assert.AreEqual(expected, actual);
}

[TestCase("中文(简体)")]
public void TestParsingNotSupportedCultureInfo(string value)
{
var bindable = new BindableCultureInfo();
Assert.DoesNotThrow(() => bindable.Parse(value));
}
}
42 changes: 27 additions & 15 deletions osu.Game.Rulesets.Karaoke/Bindables/BindableCultureInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Globalization;
using osu.Framework.Bindables;
using osu.Framework.Logging;
using osu.Game.Rulesets.Karaoke.Utils;

namespace osu.Game.Rulesets.Karaoke.Bindables;
Expand All @@ -23,23 +24,34 @@ public override void Parse(object? input)
return;
}

switch (input)
try
{
case string str:
Value = CultureInfoUtils.CreateLoadCultureInfoByCode(str);
break;

case int lcid:
Value = CultureInfoUtils.CreateLoadCultureInfoById(lcid);
break;

case CultureInfo cultureInfo:
Value = cultureInfo;
break;
switch (input)
{
case string str:
Value = CultureInfoUtils.CreateLoadCultureInfoByCode(str);
break;

case int lcid:
Value = CultureInfoUtils.CreateLoadCultureInfoById(lcid);
break;

case CultureInfo cultureInfo:
Value = cultureInfo;
break;

default:
base.Parse(input);
break;
}
}
catch (Exception ex)
{
Value = null;

default:
base.Parse(input);
break;
// It might have issue that the culture info is not available in the system.
// Log it instead of throw exception.
Logger.Error(ex, $"Failed to parse {input} into {typeof(CultureInfo)}");
}
}

Expand Down

0 comments on commit 95b1d46

Please sign in to comment.