Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correctly handle the deserialization error #3335

Open
wants to merge 2 commits into
base: v4
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 22 additions & 8 deletions shadowsocks-csharp/Model/Configuration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ public Configuration()
NLogConfig nLogConfig;

private static readonly string CONFIG_FILE = "gui-config.json";
private static readonly string CONFIG_FILE_BAK = "gui-config_bak.json";
#if DEBUG
private static readonly NLogConfig.LogLevel verboseLogLevel = NLogConfig.LogLevel.Trace;
#else
Expand Down Expand Up @@ -161,21 +162,34 @@ public static void CheckServer(Server server)
public static Configuration Load()
{
Configuration config;
List<Exception> errors = new List<Exception>();
if (File.Exists(CONFIG_FILE))
{
try

string configContent = File.ReadAllText(CONFIG_FILE);
config = JsonConvert.DeserializeObject<Configuration>(configContent, new JsonSerializerSettings()
{
ObjectCreationHandling = ObjectCreationHandling.Replace,
Error = delegate (object sender, Newtonsoft.Json.Serialization.ErrorEventArgs args)
{
errors.Add(args.ErrorContext.Error);
},
});
if (errors.Count == 0)
{
string configContent = File.ReadAllText(CONFIG_FILE);
config = JsonConvert.DeserializeObject<Configuration>(configContent, new JsonSerializerSettings()
try
{
ObjectCreationHandling = ObjectCreationHandling.Replace
});
File.WriteAllText(CONFIG_FILE_BAK, configContent);
}
catch (Exception) { };
return config;
}
catch (Exception e)
else
{
if (!(e is FileNotFoundException))
logger.LogUsefulException(e);
foreach(Exception error in errors)
{
logger.LogUsefulException(error);
};
}
}
config = new Configuration();
Expand Down
38 changes: 0 additions & 38 deletions shadowsocks-csharp/packages.config

This file was deleted.

124 changes: 109 additions & 15 deletions shadowsocks-csharp/shadowsocks-csharp.csproj
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="..\packages\ReactiveUI.Fody.12.1.5\build\ReactiveUI.Fody.props" Condition="Exists('..\packages\ReactiveUI.Fody.12.1.5\build\ReactiveUI.Fody.props')" />
<Import Project="..\packages\Costura.Fody.4.1.0\build\Costura.Fody.props" Condition="Exists('..\packages\Costura.Fody.4.1.0\build\Costura.Fody.props')" />
<Import Project="..\packages\Caseless.Fody.1.9.0\build\Caseless.Fody.props" Condition="Exists('..\packages\Caseless.Fody.1.9.0\build\Caseless.Fody.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
Expand Down Expand Up @@ -368,7 +365,6 @@
<ItemGroup>
<None Include="Data\sysproxy.exe.gz" />
<None Include="Data\sysproxy64.exe.gz" />
<None Include="packages.config" />
<None Include="Properties\Settings.settings">
<Generator>SettingsSingleFileGenerator</Generator>
<LastGenOutput>Settings.Designer.cs</LastGenOutput>
Expand Down Expand Up @@ -435,18 +431,116 @@
<Generator>MSBuild:Compile</Generator>
</Page>
</ItemGroup>
<ItemGroup />
<ItemGroup>
<PackageReference Include="AvalonEdit">
<Version>6.0.1</Version>
</PackageReference>
<PackageReference Include="Caseless.Fody">
<Version>1.9.0</Version>
</PackageReference>
<PackageReference Include="CommandLineParser">
<Version>2.8.0</Version>
</PackageReference>
<PackageReference Include="Costura.Fody">
<Version>4.1.0</Version>
</PackageReference>
<PackageReference Include="DynamicData">
<Version>7.1.1</Version>
</PackageReference>
<PackageReference Include="Fody">
<Version>6.3.0</Version>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="GlobalHotKey">
<Version>1.1.0</Version>
</PackageReference>
<PackageReference Include="Google.Protobuf">
<Version>3.14.0</Version>
</PackageReference>
<PackageReference Include="MdXaml">
<Version>1.6.0</Version>
</PackageReference>
<PackageReference Include="Newtonsoft.Json">
<Version>12.0.3</Version>
</PackageReference>
<PackageReference Include="NLog">
<Version>4.7.6</Version>
</PackageReference>
<PackageReference Include="Pharmacist.Common">
<Version>2.0.5</Version>
</PackageReference>
<PackageReference Include="ReactiveUI">
<Version>12.1.5</Version>
</PackageReference>
<PackageReference Include="ReactiveUI.Events.WPF">
<Version>12.1.5</Version>
</PackageReference>
<PackageReference Include="ReactiveUI.Fody">
<Version>12.1.5</Version>
</PackageReference>
<PackageReference Include="ReactiveUI.Validation">
<Version>1.8.6</Version>
</PackageReference>
<PackageReference Include="ReactiveUI.WPF">
<Version>12.1.5</Version>
</PackageReference>
<PackageReference Include="Splat">
<Version>9.8.1</Version>
</PackageReference>
<PackageReference Include="System.Buffers">
<Version>4.5.1</Version>
</PackageReference>
<PackageReference Include="System.IO">
<Version>4.3.0</Version>
</PackageReference>
<PackageReference Include="System.Memory">
<Version>4.5.4</Version>
</PackageReference>
<PackageReference Include="System.Net.Http">
<Version>4.3.4</Version>
</PackageReference>
<PackageReference Include="System.Numerics.Vectors">
<Version>4.5.0</Version>
</PackageReference>
<PackageReference Include="System.Reactive">
<Version>5.0.0</Version>
</PackageReference>
<PackageReference Include="System.Runtime">
<Version>4.3.1</Version>
</PackageReference>
<PackageReference Include="System.Runtime.CompilerServices.Unsafe">
<Version>5.0.0</Version>
</PackageReference>
<PackageReference Include="System.Security.Cryptography.Algorithms">
<Version>4.3.1</Version>
</PackageReference>
<PackageReference Include="System.Security.Cryptography.Encoding">
<Version>4.3.0</Version>
</PackageReference>
<PackageReference Include="System.Security.Cryptography.Primitives">
<Version>4.3.0</Version>
</PackageReference>
<PackageReference Include="System.Security.Cryptography.X509Certificates">
<Version>4.3.2</Version>
</PackageReference>
<PackageReference Include="System.Threading.Tasks.Extensions">
<Version>4.5.4</Version>
</PackageReference>
<PackageReference Include="System.ValueTuple">
<Version>4.5.0</Version>
</PackageReference>
<PackageReference Include="WPFLocalizeExtension">
<Version>3.8.0</Version>
</PackageReference>
<PackageReference Include="XAMLMarkupExtensions">
<Version>2.0.0</Version>
</PackageReference>
<PackageReference Include="ZXing.Net">
<Version>0.16.6</Version>
</PackageReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
<ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('..\packages\Caseless.Fody.1.9.0\build\Caseless.Fody.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Caseless.Fody.1.9.0\build\Caseless.Fody.props'))" />
<Error Condition="!Exists('..\packages\Costura.Fody.4.1.0\build\Costura.Fody.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Costura.Fody.4.1.0\build\Costura.Fody.props'))" />
<Error Condition="!Exists('..\packages\Fody.6.3.0\build\Fody.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Fody.6.3.0\build\Fody.targets'))" />
<Error Condition="!Exists('..\packages\ReactiveUI.Fody.12.1.5\build\ReactiveUI.Fody.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\ReactiveUI.Fody.12.1.5\build\ReactiveUI.Fody.props'))" />
</Target>
<Import Project="..\packages\Fody.6.3.0\build\Fody.targets" Condition="Exists('..\packages\Fody.6.3.0\build\Fody.targets')" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
Expand Down
4 changes: 4 additions & 0 deletions test/app.config
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
<assemblyIdentity name="ICSharpCode.AvalonEdit" publicKeyToken="9cc39be672370310" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-6.0.1.278" newVersion="6.0.1.278" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Reactive" publicKeyToken="94bc3704cddfc263" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-5.0.0.0" newVersion="5.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8" /></startup></configuration>