-
Notifications
You must be signed in to change notification settings - Fork 138
/
ExtractableException.cs
34 lines (33 loc) · 1.24 KB
/
ExtractableException.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
using System;
using System.Diagnostics.Contracts;
using Bencodex.Types;
namespace Libplanet.Action
{
/// <summary>
/// Extension methods for <see cref="IExtractableException"/> and other <see cref="Exception"/>s
/// that do not implement <see cref="IExtractableException"/>.
/// </summary>
public static class ExtractableException
{
/// <summary>
/// Extracts metadata from any exception in a general way.
/// </summary>
/// <param name="exception">An exception to extract its metadata from.</param>
/// <returns>Extracted metadata. If there is not its metadata at all, or it does not
/// support metadata extraction, it can return <c>null</c> instead.</returns>
[Pure]
public static IValue? ExtractMetadata(this Exception exception) =>
exception switch
{
IExtractableException extractable =>
extractable.Metadata,
ArgumentException argExc =>
Dictionary.Empty.Add(
"parameterName",
argExc.ParamName is { } name ? (Text)name : (IValue)Null.Value
),
_ =>
null,
};
}
}