From 926e4bbc7ccadf316b6a9af59828bda431937d6b Mon Sep 17 00:00:00 2001 From: gnattu Date: Sun, 31 Mar 2024 01:39:09 +0800 Subject: [PATCH 1/4] fix: catch collectible access non-collectible exception Catch the `NotSupportedException` caused by plugins directly using the server's XML serializer and throw a new one with informative messages, notifying the plugin developer that directly using this serializer is not a supported use case. Signed-off-by: gnattu --- .../Serialization/MyXmlSerializer.cs | 78 +++++++++++++++---- 1 file changed, 65 insertions(+), 13 deletions(-) diff --git a/Emby.Server.Implementations/Serialization/MyXmlSerializer.cs b/Emby.Server.Implementations/Serialization/MyXmlSerializer.cs index 1bac2600ca..9c3ab3d0a0 100644 --- a/Emby.Server.Implementations/Serialization/MyXmlSerializer.cs +++ b/Emby.Server.Implementations/Serialization/MyXmlSerializer.cs @@ -24,6 +24,18 @@ namespace Emby.Server.Implementations.Serialization static (_, t) => new XmlSerializer(t), type); + private static void ThrowCollectibleException(Exception e) + { + var isCollectibleException = string.Equals(e.Message, "A non-collectible assembly may not reference a collectible assembly.", StringComparison.Ordinal); + if (isCollectibleException) + { + throw new NotSupportedException($"It seems you are using the server's XML serializer in your plugin.\n" + + $"This serializer is only used by the server to handle the plugin's config file " + + $"and is not intended for the plugin to use directly.\n" + + $"Please create your own XML serializer instance to handle plugin specific files."); + } + } + /// /// Serializes to writer. /// @@ -43,10 +55,18 @@ namespace Emby.Server.Implementations.Serialization /// System.Object. public object? DeserializeFromStream(Type type, Stream stream) { - using (var reader = XmlReader.Create(stream)) + try + { + using (var reader = XmlReader.Create(stream)) + { + var netSerializer = GetSerializer(type); + return netSerializer.Deserialize(reader); + } + } + catch (NotSupportedException e) { - var netSerializer = GetSerializer(type); - return netSerializer.Deserialize(reader); + ThrowCollectibleException(e); + throw; } } @@ -57,11 +77,19 @@ namespace Emby.Server.Implementations.Serialization /// The stream. public void SerializeToStream(object obj, Stream stream) { - using (var writer = new StreamWriter(stream, null, IODefaults.StreamWriterBufferSize, true)) - using (var textWriter = new XmlTextWriter(writer)) + try { - textWriter.Formatting = Formatting.Indented; - SerializeToWriter(obj, textWriter); + using (var writer = new StreamWriter(stream, null, IODefaults.StreamWriterBufferSize, true)) + using (var textWriter = new XmlTextWriter(writer)) + { + textWriter.Formatting = Formatting.Indented; + SerializeToWriter(obj, textWriter); + } + } + catch (NotSupportedException e) + { + ThrowCollectibleException(e); + throw; } } @@ -72,9 +100,17 @@ namespace Emby.Server.Implementations.Serialization /// The file. public void SerializeToFile(object obj, string file) { - using (var stream = new FileStream(file, FileMode.Create, FileAccess.Write)) + try + { + using (var stream = new FileStream(file, FileMode.Create, FileAccess.Write)) + { + SerializeToStream(obj, stream); + } + } + catch (NotSupportedException e) { - SerializeToStream(obj, stream); + ThrowCollectibleException(e); + throw; } } @@ -86,9 +122,17 @@ namespace Emby.Server.Implementations.Serialization /// System.Object. public object? DeserializeFromFile(Type type, string file) { - using (var stream = File.OpenRead(file)) + try { - return DeserializeFromStream(type, stream); + using (var stream = File.OpenRead(file)) + { + return DeserializeFromStream(type, stream); + } + } + catch (NotSupportedException e) + { + ThrowCollectibleException(e); + throw; } } @@ -100,9 +144,17 @@ namespace Emby.Server.Implementations.Serialization /// System.Object. public object? DeserializeFromBytes(Type type, byte[] buffer) { - using (var stream = new MemoryStream(buffer, 0, buffer.Length, false, true)) + try + { + using (var stream = new MemoryStream(buffer, 0, buffer.Length, false, true)) + { + return DeserializeFromStream(type, stream); + } + } + catch (NotSupportedException e) { - return DeserializeFromStream(type, stream); + ThrowCollectibleException(e); + throw; } } } From 504c8faf89e35422c140df8ca983d4bec6a7f851 Mon Sep 17 00:00:00 2001 From: gnattu Date: Sun, 31 Mar 2024 22:11:07 +0800 Subject: [PATCH 2/4] fix: add inner exception Co-authored-by: Mark Monteiro --- Emby.Server.Implementations/Serialization/MyXmlSerializer.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Emby.Server.Implementations/Serialization/MyXmlSerializer.cs b/Emby.Server.Implementations/Serialization/MyXmlSerializer.cs index 9c3ab3d0a0..408ab199fd 100644 --- a/Emby.Server.Implementations/Serialization/MyXmlSerializer.cs +++ b/Emby.Server.Implementations/Serialization/MyXmlSerializer.cs @@ -32,7 +32,8 @@ namespace Emby.Server.Implementations.Serialization throw new NotSupportedException($"It seems you are using the server's XML serializer in your plugin.\n" + $"This serializer is only used by the server to handle the plugin's config file " + $"and is not intended for the plugin to use directly.\n" + - $"Please create your own XML serializer instance to handle plugin specific files."); + $"Please create your own XML serializer instance to handle plugin specific files.", + e); } } From 6c7fc9414b528c06b59d4b32b3b93ca5ffcbfb0c Mon Sep 17 00:00:00 2001 From: gnattu Date: Sun, 31 Mar 2024 22:11:23 +0800 Subject: [PATCH 3/4] fix: specify exception type Co-authored-by: Mark Monteiro --- Emby.Server.Implementations/Serialization/MyXmlSerializer.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Emby.Server.Implementations/Serialization/MyXmlSerializer.cs b/Emby.Server.Implementations/Serialization/MyXmlSerializer.cs index 408ab199fd..dae7543100 100644 --- a/Emby.Server.Implementations/Serialization/MyXmlSerializer.cs +++ b/Emby.Server.Implementations/Serialization/MyXmlSerializer.cs @@ -24,7 +24,7 @@ namespace Emby.Server.Implementations.Serialization static (_, t) => new XmlSerializer(t), type); - private static void ThrowCollectibleException(Exception e) + private static void ThrowCollectibleException(NotSupportedException e) { var isCollectibleException = string.Equals(e.Message, "A non-collectible assembly may not reference a collectible assembly.", StringComparison.Ordinal); if (isCollectibleException) From c3eddf7f338ab05f2c83bf67a1fe0600b5685e79 Mon Sep 17 00:00:00 2001 From: gnattu Date: Sun, 31 Mar 2024 22:22:48 +0800 Subject: [PATCH 4/4] chore: fix style Signed-off-by: gnattu --- .../Serialization/MyXmlSerializer.cs | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/Emby.Server.Implementations/Serialization/MyXmlSerializer.cs b/Emby.Server.Implementations/Serialization/MyXmlSerializer.cs index dae7543100..3e30f57cdf 100644 --- a/Emby.Server.Implementations/Serialization/MyXmlSerializer.cs +++ b/Emby.Server.Implementations/Serialization/MyXmlSerializer.cs @@ -29,11 +29,12 @@ namespace Emby.Server.Implementations.Serialization var isCollectibleException = string.Equals(e.Message, "A non-collectible assembly may not reference a collectible assembly.", StringComparison.Ordinal); if (isCollectibleException) { - throw new NotSupportedException($"It seems you are using the server's XML serializer in your plugin.\n" + - $"This serializer is only used by the server to handle the plugin's config file " + - $"and is not intended for the plugin to use directly.\n" + - $"Please create your own XML serializer instance to handle plugin specific files.", - e); + throw new NotSupportedException( + $"It seems you are using the server's XML serializer in your plugin.\n" + + $"This serializer is only used by the server to handle the plugin's config file " + + $"and is not intended for the plugin to use directly.\n" + + $"Please create your own XML serializer instance to handle plugin specific files.", + e); } }