|
| 1 | +using System.Text.Json; |
| 2 | + |
| 3 | +namespace KeetaNet.Anchor; |
| 4 | + |
| 5 | +/// <summary> |
| 6 | +/// An anchor refusal carrying a typed asset-movement blocker: the operation |
| 7 | +/// cannot proceed until the user resolves <see cref="Blocker"/>. |
| 8 | +/// </summary> |
| 9 | +public sealed class KeetaBlockerException : KeetaException |
| 10 | +{ |
| 11 | + /// <summary>Every recognized blocker transport code starts with this.</summary> |
| 12 | + internal const string CodePrefix = "KEETA_ANCHOR_ASSET_MOVEMENT_"; |
| 13 | + |
| 14 | + /// <summary>The typed blocker the user must resolve.</summary> |
| 15 | + public AssetMovementBlocker Blocker { get; } |
| 16 | + |
| 17 | + private KeetaBlockerException(string code, string message, AssetMovementBlocker blocker) |
| 18 | + : base(code, message) |
| 19 | + { |
| 20 | + Blocker = blocker; |
| 21 | + } |
| 22 | + |
| 23 | + /// <summary> |
| 24 | + /// Rehydrate a blocker failure from the core's last-error parts, or null |
| 25 | + /// when <paramref name="code"/> is not a blocker transport code or |
| 26 | + /// <paramref name="payload"/> does not decode as a recognized blocker. |
| 27 | + /// </summary> |
| 28 | + internal static KeetaBlockerException? TryDecode(string code, string payload) |
| 29 | + { |
| 30 | + if (!code.StartsWith(CodePrefix, StringComparison.Ordinal)) |
| 31 | + { |
| 32 | + return null; |
| 33 | + } |
| 34 | + |
| 35 | + AssetMovementBlocker? blocker; |
| 36 | + try |
| 37 | + { |
| 38 | + blocker = JsonSerializer.Deserialize<AssetMovementBlocker>(payload, KeetaJson.Options); |
| 39 | + } |
| 40 | + catch (JsonException) |
| 41 | + { |
| 42 | + return null; |
| 43 | + } |
| 44 | + |
| 45 | + if (blocker is null || Describe(blocker) is not { } summary) |
| 46 | + { |
| 47 | + return null; |
| 48 | + } |
| 49 | + |
| 50 | + return new KeetaBlockerException(code, summary, blocker); |
| 51 | + } |
| 52 | + |
| 53 | + /// <summary> |
| 54 | + /// A human-readable summary of what the user must resolve, or null for a |
| 55 | + /// shape that does not surface typed. |
| 56 | + /// </summary> |
| 57 | + private static string? Describe(AssetMovementBlocker blocker) => |
| 58 | + blocker switch |
| 59 | + { |
| 60 | + AssetKycShareNeededBlocker => "the provider requires KYC attributes to be shared first", |
| 61 | + AssetAdditionalKycNeededBlocker => "the provider requires additional KYC steps", |
| 62 | + AssetOperationNotSupportedBlocker => "the provider does not support this operation", |
| 63 | + AssetUserActionNeededBlocker => "the provider requires on-ledger user actions", |
| 64 | + _ => null, |
| 65 | + }; |
| 66 | +} |
0 commit comments