This repository was archived by the owner on Jun 21, 2023. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathErrorMessageTranslator.cs
More file actions
95 lines (82 loc) · 3.11 KB
/
Copy pathErrorMessageTranslator.cs
File metadata and controls
95 lines (82 loc) · 3.11 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using GitHub.Extensions;
using ReactiveUI;
using ReactiveUI.Legacy;
#pragma warning disable CS0618 // Type or member is obsolete
#pragma warning disable CA1034 // Nested types should not be visible
namespace GitHub.Services
{
public class ErrorMessageTranslator
{
readonly IDictionary<ErrorType, ErrorMap> userErrorMappings;
public ErrorMessageTranslator(IDictionary<ErrorType, ErrorMap> userErrors)
{
Guard.ArgumentNotNull(userErrors, nameof(userErrors));
userErrorMappings = userErrors;
}
public UserError GetUserError(ErrorType errorType, Exception exception, params object[] context)
{
var translation = GetUserErrorTranslation(errorType, exception, context);
return new UserError(translation.ErrorMessage, translation.CauseOrResolution, translation.RecoveryCommands, null, exception)
{
UserErrorIcon = StockUserErrorIcon.Error
};
}
public UserErrorTranslation GetUserErrorTranslation(
ErrorType errorType,
Exception exception,
params object[] context)
{
ErrorMessage errorMessage = null;
ErrorMap errorMap;
if (userErrorMappings.TryGetValue(ErrorType.Global, out errorMap) && errorMap != null)
{
errorMessage = errorMap.GetErrorInfo(exception);
}
if (errorMessage == null)
{
if (!userErrorMappings.TryGetValue(errorType, out errorMap) || errorMap == null)
throw new InvalidOperationException("This should never happen!");
}
errorMessage = errorMap.GetErrorInfo(exception) ?? new ErrorMessage("error", "Unknown error occurred");
string details = errorMessage.Message;
string heading = errorMessage.Heading;
if (context != null && context.Any())
{
heading = String.Format(CultureInfo.InvariantCulture, heading, context);
details = String.Format(CultureInfo.InvariantCulture, details, context);
}
return new UserErrorTranslation(heading, details, errorMap.RecoveryCommands);
}
public class UserErrorTranslation
{
public UserErrorTranslation(
string errorMessage,
string causeOrResolution,
IEnumerable<IRecoveryCommand> recoveryCommands)
{
ErrorMessage = errorMessage;
CauseOrResolution = causeOrResolution;
RecoveryCommands = recoveryCommands;
}
public string ErrorMessage
{
get;
private set;
}
public string CauseOrResolution
{
get;
private set;
}
public IEnumerable<IRecoveryCommand> RecoveryCommands
{
get;
private set;
}
}
}
}