refactor(core): harden PluginManifest error model
Addresses code quality review of c082ecf:
- Require takes a literal JSON field name, no more fragile PascalCase->camelCase transform
- Parse_MissingRequiredField_Throws asserts exact message, not substring
- Remove unused using System.Text.Json.Serialization
This commit is contained in:
parent
c082ecf36a
commit
99d2702c13
2 changed files with 7 additions and 9 deletions
|
|
@ -1,5 +1,4 @@
|
|||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace AcDream.Core.Plugins;
|
||||
|
||||
|
|
@ -26,10 +25,10 @@ public sealed record PluginManifest(
|
|||
if (dto is null)
|
||||
throw new PluginManifestException("manifest is empty");
|
||||
|
||||
Require(dto.Id, nameof(dto.Id));
|
||||
Require(dto.DisplayName, nameof(dto.DisplayName));
|
||||
Require(dto.Version, nameof(dto.Version));
|
||||
Require(dto.EntryDll, nameof(dto.EntryDll));
|
||||
Require(dto.Id, "id");
|
||||
Require(dto.DisplayName, "displayName");
|
||||
Require(dto.Version, "version");
|
||||
Require(dto.EntryDll, "entryDll");
|
||||
if (dto.ApiVersion <= 0)
|
||||
throw new PluginManifestException("apiVersion must be >= 1");
|
||||
|
||||
|
|
@ -42,11 +41,10 @@ public sealed record PluginManifest(
|
|||
dto.Dependencies ?? Array.Empty<string>());
|
||||
}
|
||||
|
||||
private static void Require(string? value, string fieldName)
|
||||
private static void Require(string? value, string jsonFieldName)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(value))
|
||||
throw new PluginManifestException(
|
||||
$"missing required field: {char.ToLowerInvariant(fieldName[0])}{fieldName[1..]}");
|
||||
throw new PluginManifestException($"missing required field: {jsonFieldName}");
|
||||
}
|
||||
|
||||
private static readonly JsonSerializerOptions JsonOptions = new()
|
||||
|
|
|
|||
|
|
@ -34,7 +34,7 @@ public class PluginManifestTests
|
|||
""";
|
||||
|
||||
var ex = Assert.Throws<PluginManifestException>(() => PluginManifest.Parse(json));
|
||||
Assert.Contains("displayName", ex.Message);
|
||||
Assert.Equal("missing required field: displayName", ex.Message);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue