-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathHResultDetailProvider.cs
More file actions
63 lines (57 loc) · 1.72 KB
/
HResultDetailProvider.cs
File metadata and controls
63 lines (57 loc) · 1.72 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/****
* TakymLib
* Copyright (C) 2020-2022 Yigty.ORG; all rights reserved.
* Copyright (C) 2020-2022 Takym.
*
* distributed under the MIT License.
****/
using System;
using System.Collections.Concurrent;
using System.ComponentModel;
namespace TakymLib.Logging
{
/// <summary>
/// <see cref="TakymLib.Logging.ErrorReportBuilder"/>にH-RESULT情報を提供します。
/// このクラスは継承できません。
/// </summary>
public sealed class HResultDetailProvider : ICustomErrorDetailProvider
{
private readonly ConcurrentDictionary<int, string> _messages;
/// <summary>
/// 型'<see cref="TakymLib.Logging.HResultDetailProvider"/>'の新しいインスタンスを生成します。
/// </summary>
public HResultDetailProvider()
{
_messages = new();
}
/// <summary>
/// 追加情報を可読な翻訳済みの文字列へ変換します。
/// </summary>
/// <param name="exception">変換するデータを保持している例外オブジェクトです。</param>
/// <returns>翻訳済みの文字列です。</returns>
public string GetLocalizedDetail(Exception exception)
{
if (exception is null) {
return string.Empty;
}
string result = $"H-RESULT Message: {this.GetMessage(exception.HResult)}";
if (exception is Win32Exception w32e) {
return result + Environment.NewLine
+ $"H-RESULT Message (Win32): {this.GetMessage(w32e.NativeErrorCode)}";
} else {
return result;
}
}
/// <summary>
/// 内部キャッシュを削除します。
/// </summary>
public void ClearCache()
{
_messages.Clear();
}
private string GetMessage(int hresult)
{
return _messages.GetOrAdd(hresult, hresult => new Win32Exception(hresult).Message);
}
}
}