-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathVaultExceptionParser.cs
More file actions
213 lines (178 loc) · 8.36 KB
/
VaultExceptionParser.cs
File metadata and controls
213 lines (178 loc) · 8.36 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
namespace VaultExceptionParser2023
{
using Autodesk.Connectivity.WebServices;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Resources;
using System.Text;
using System.Text.RegularExpressions;
/// <summary>
/// The Vault Exception Parser helps to parse Vault Errors and Restrictions from generic numbers to the actual message found in the
/// Server Errors and Restrictions tables listed in the API documentation.
/// </summary>
public static class VaultExceptionParser
{
/// <summary>
/// Gets the lists from the internal resources and loads them into dictionaries accessible to the application.
/// </summary>
static VaultExceptionParser()
{
LoadInternalCodeLists();
}
#region Properties
/// <summary>
/// List of code lists separated by year version.
/// </summary>
public static List<(int yearVersion, string listName)> CodeLists = new List<(int yearVersion, string listName)>();
/// <summary>
/// The Vault Year Version of Error and Restriction codes to return.
/// </summary>
public static int VaultYear { get; private set; } = -1;
/// <summary>
/// Error codes from Server Error Codes table in Vault API documentation for the current version.
/// </summary>
public static Dictionary<int, ICode> VaultErrorCodes = new Dictionary<int, ICode>();
/// <summary>
/// Error codes from Restriction Codes table in Vault API documentation for the current version.
/// </summary>
public static Dictionary<int, ICode> VaultRestrictionCodes = new Dictionary<int, ICode>();
#endregion
/// <summary>
/// Gets the list from the assembly resource stream.
/// </summary>
private static void LoadInternalCodeLists()
{
/* Only load is we haven't already. */
if(CodeLists.Count > 0) { return; }
/* Get the list of coed files from the resource. */
ResourceSet resources = Properties.Resources.ResourceManager.GetResourceSet(CultureInfo.CurrentCulture, true, true);
foreach (DictionaryEntry resource in resources)
{
/* Grab first 4 chars from resource name to get the year. Ex: 2023ErrorCodes.txt*/
bool paresed = int.TryParse(resource.Key.ToString().Substring(0, 4), out int year);
if (!paresed) { continue; }
VaultYear = year;
CodeLists.Add((year, (string)resource.Key));
}
/* Load the codes into their respective dictionaries. */
CodeLists.ForEach(l => LoadCodes(l.listName));
}
/// <summary>
/// Loads all of the applications codes specified in the Application Codes resource file into a dictionary for easy access.<para/>
/// </summary>
private static void LoadCodes(string resourceName)
{
/* Extracts code from string. */
Regex codeRegex = new Regex("^\\d*[ +|\\t]");
/* Extracts error name. */
Regex erroName = new Regex("(?!\\d*[ +|\\t])(\\w*){1}");
string code, currCodeLine, desc, name, prevCodeLine = string.Empty;
VaultCode eCode;
/* Get the resource */
string resource = Properties.Resources.ResourceManager.GetString(resourceName);
using (StringReader reader = new StringReader(resource))
{
while ((currCodeLine = reader.ReadLine()) != null)
{
string eLine = currCodeLine.Trim();
if (eLine.StartsWith("#")) { continue; }
if (codeRegex.IsMatch(eLine))
{
if (prevCodeLine != string.Empty)
{
/* Since the current currCodeLine starts with a codeRegex we can publish the previous one. */
/* Get the Code */
code = codeRegex.Match(prevCodeLine).Value;
/* Get the Error Name */
name = erroName.Match(prevCodeLine).Value;
if (name.IsNullOrEmpty()) { name = "Unused"; }
/* Get the Error Description*/
desc = prevCodeLine.Replace(code, "").Replace(name, "").Trim();
eCode = new VaultCode(int.Parse(code), name, desc);
if (resourceName.Contains("RestrictionCodes"))
{
VaultRestrictionCodes.Add(int.Parse(code), eCode);
}
else
{
VaultErrorCodes.Add(int.Parse(code), eCode);
}
}
prevCodeLine = currCodeLine.Trim();
}
else
{
/* Since the currCodeLine didn't begin with a codeRegex due to a currCodeLine break we should append it to the prevCodeLine. */
prevCodeLine += $"\n{eLine}";
}
}
/* Publish that last error code. */
/* Get the Code */
code = codeRegex.Match(prevCodeLine).Value;
/* Get the Error Name */
name = erroName.Match(prevCodeLine).Value;
if (name.IsNullOrEmpty()) { name = "Unused"; }
/* Get the Error Description*/
desc = prevCodeLine.Replace(code, "").Replace(name, "");
eCode = new VaultCode(int.Parse(code), name, desc);
if (resourceName.Contains("RestrictionCodes"))
{
VaultRestrictionCodes.Add(int.Parse(code), eCode);
}
else
{
VaultErrorCodes.Add(int.Parse(code), eCode);
}
}
}
/// <summary>
/// Try to extract Vault Specific Error or Restriction codes from a general Exception.
/// If Vault specific information cannot be returned then the original Exception is returned.
/// </summary>
/// <param name="ex"></param>
/// <param name="PareseError"></param>
/// <returns></returns>
public static Exception ParseVaultException(this Exception ex)
{
try
{
if (ex.GetType() == typeof(VaultServiceErrorException))
{
/* Try to cast the Exception to the correct type. */
VaultServiceErrorException ve = (VaultServiceErrorException)ex;
/* See if we have restrictions or error. */
if (!ve.Restrictions.IsNullOrEmpty())
{
StringBuilder resMsgs = new StringBuilder("Restrictions have occurred:");
foreach (VaultServiceErrorException.Restriction res in ve.Restrictions)
{
if (VaultRestrictionCodes.ContainsKey(res.Code))
{
resMsgs.AppendLine(VaultRestrictionCodes[res.Code].ToString());
}
else
{
resMsgs.AppendLine($"Restriction code ({res.Code}) description not found.");
}
}
/* Return the concatenated restrictions message. */
return new Exception(resMsgs.ToString(), ex);
}
/* Return the general Vault Exception message. */
string errorMsg = VaultErrorCodes[ve.ErrorCode].ToString();
return new Exception(errorMsg.ToString(), ex);
}
/* Something didn't work so just return the original exception. */
return ex;
}
catch
{
/* Not a Vault specific exception so just return the original exception. */
return ex;
}
}
}
}