Skip to content

Commit 3d24c4a

Browse files
committed
addressed review comments
1 parent 1a7cc02 commit 3d24c4a

File tree

2 files changed

+14
-30
lines changed

2 files changed

+14
-30
lines changed

src/Microsoft.TemplateEngine.Orchestrator.RunnableProjects/Macros/BaseMacroConfig.cs

Lines changed: 6 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -90,21 +90,13 @@ protected static bool ConvertJTokenToBool(string token, IGeneratedSymbolConfig c
9090
try
9191
{
9292
var jToken = JToken.Parse(token);
93-
if (jToken.Type is not JTokenType.Boolean and not JTokenType.String)
94-
{
95-
throw new TemplateAuthoringException(string.Format(LocalizableStrings.MacroConfig_Exception_ValueShouldBeBoolean, config.VariableName, parameterName), config.VariableName);
96-
}
97-
if (bool.TryParse(jToken.ToString(), out bool result))
93+
if (jToken.TryParseBool(out bool result))
9894
{
9995
return result;
10096
}
10197
throw new TemplateAuthoringException(string.Format(LocalizableStrings.MacroConfig_Exception_ValueShouldBeBoolean, config.VariableName, parameterName), config.VariableName);
10298
}
103-
catch (TemplateAuthoringException)
104-
{
105-
throw;
106-
}
107-
catch (Exception ex)
99+
catch (Exception ex) when (ex is not TemplateAuthoringException)
108100
{
109101
throw new TemplateAuthoringException(string.Format(LocalizableStrings.MacroConfig_Exception_InvalidJSON, config.VariableName, parameterName), config.VariableName, ex);
110102
}
@@ -115,21 +107,13 @@ protected static int ConvertJTokenToInt(string token, IGeneratedSymbolConfig con
115107
try
116108
{
117109
var jToken = JToken.Parse(token);
118-
if (jToken.Type is not JTokenType.Integer and not JTokenType.String)
119-
{
120-
throw new TemplateAuthoringException(string.Format(LocalizableStrings.MacroConfig_Exception_ValueShouldBeInteger, config.VariableName, parameterName), config.VariableName);
121-
}
122-
if (int.TryParse(jToken.ToString(), out int result))
110+
if (jToken.TryParseInt(out int result))
123111
{
124112
return result;
125113
}
126114
throw new TemplateAuthoringException(string.Format(LocalizableStrings.MacroConfig_Exception_ValueShouldBeInteger, config.VariableName, parameterName), config.VariableName);
127115
}
128-
catch (TemplateAuthoringException)
129-
{
130-
throw;
131-
}
132-
catch (Exception ex)
116+
catch (Exception ex) when (ex is not TemplateAuthoringException)
133117
{
134118
throw new TemplateAuthoringException(string.Format(LocalizableStrings.MacroConfig_Exception_InvalidJSON, config.VariableName, parameterName), config.VariableName, ex);
135119
}
@@ -146,11 +130,7 @@ protected static string ConvertJTokenToString(string token, IGeneratedSymbolConf
146130
}
147131
return jToken.ToString();
148132
}
149-
catch (TemplateAuthoringException)
150-
{
151-
throw;
152-
}
153-
catch (Exception ex)
133+
catch (Exception ex) when (ex is not TemplateAuthoringException)
154134
{
155135
throw new TemplateAuthoringException(string.Format(LocalizableStrings.MacroConfig_Exception_InvalidJSON, config.VariableName, parameterName), config.VariableName, ex);
156136
}
@@ -167,11 +147,7 @@ protected static JArray ConvertJTokenToJArray(string token, IGeneratedSymbolConf
167147
}
168148
return (JArray)jToken;
169149
}
170-
catch (TemplateAuthoringException)
171-
{
172-
throw;
173-
}
174-
catch (Exception ex)
150+
catch (Exception ex) when (ex is not TemplateAuthoringException)
175151
{
176152
throw new TemplateAuthoringException(string.Format(LocalizableStrings.MacroConfig_Exception_InvalidJSON, config.VariableName, parameterName), config.VariableName, ex);
177153
}

src/Shared/JExtensions.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,14 @@ internal static bool ToBool(this JToken? token, string? key = null, bool default
8787
return result;
8888
}
8989

90+
internal static bool TryParseInt(this JToken token, out int result)
91+
{
92+
result = default;
93+
return (token.Type == JTokenType.Integer || token.Type == JTokenType.String)
94+
&&
95+
int.TryParse(token.ToString(), out result);
96+
}
97+
9098
internal static int ToInt32(this JToken? token, string? key = null, int defaultValue = 0)
9199
{
92100
int value;

0 commit comments

Comments
 (0)