Skip to content

Commit 4a6a6c6

Browse files
committed
Was made refactoring
1 parent 40dd0f8 commit 4a6a6c6

File tree

14 files changed

+226
-166
lines changed

14 files changed

+226
-166
lines changed

src/JavaScriptEngineSwitcher.ChakraCore/ChakraCoreJsEngine.cs

Lines changed: 52 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -174,23 +174,11 @@ public ChakraCoreJsEngine(ChakraCoreSettings settings)
174174
}
175175
catch (DllNotFoundException e)
176176
{
177-
throw WrapTypeLoadException(e);
178-
}
179-
#if NETSTANDARD1_3
180-
catch (TypeLoadException e)
181-
#else
182-
catch (EntryPointNotFoundException e)
183-
#endif
184-
{
185-
throw WrapTypeLoadException(e);
186-
}
187-
catch (OriginalException e)
188-
{
189-
throw WrapJsException(e);
177+
throw WrapDllNotFoundException(e);
190178
}
191179
catch (Exception e)
192180
{
193-
throw CoreErrorHelpers.WrapUnknownEngineLoadException(e, EngineName, EngineVersion);
181+
throw CoreErrorHelpers.WrapEngineLoadException(e, EngineName, EngineVersion, true);
194182
}
195183
finally
196184
{
@@ -236,10 +224,7 @@ private static void Initialize()
236224
}
237225
catch (InvalidOperationException e)
238226
{
239-
string message = string.Format(CoreStrings.Engine_JsEngineNotLoaded, EngineName) + " " +
240-
e.Message;
241-
242-
throw new WrapperEngineLoadException(message, EngineName, EngineVersion, e);
227+
throw CoreErrorHelpers.WrapEngineLoadException(e, EngineName, EngineVersion);
243228
}
244229
}
245230

@@ -1051,12 +1036,12 @@ private static WrapperException WrapJsException(OriginalException originalExcept
10511036
callStackWithSourceFragment = CoreErrorHelpers.StringifyErrorLocationItems(callStackItems);
10521037
}
10531038

1054-
message = CoreErrorHelpers.GenerateErrorMessage(type, description,
1039+
message = CoreErrorHelpers.GenerateScriptErrorMessage(type, description,
10551040
callStackWithSourceFragment);
10561041
}
10571042
else
10581043
{
1059-
message = CoreErrorHelpers.GenerateErrorMessage(type, description, documentName,
1044+
message = CoreErrorHelpers.GenerateScriptErrorMessage(type, description, documentName,
10601045
lineNumber, columnNumber, sourceFragment);
10611046
}
10621047
}
@@ -1129,89 +1114,83 @@ private static WrapperException WrapJsException(OriginalException originalExcept
11291114
return wrapperException;
11301115
}
11311116

1132-
private static WrapperEngineLoadException WrapTypeLoadException(
1133-
TypeLoadException originalTypeLoadException)
1117+
private static WrapperEngineLoadException WrapDllNotFoundException(
1118+
DllNotFoundException originalDllNotFoundException)
11341119
{
1135-
string originalMessage = originalTypeLoadException.Message;
1136-
string jsEngineNotLoadedPart = string.Format(CoreStrings.Engine_JsEngineNotLoaded, EngineName);
1120+
string originalMessage = originalDllNotFoundException.Message;
1121+
string description;
11371122
string message;
11381123
bool isMonoRuntime = Utils.IsMonoRuntime();
11391124

1140-
if (originalTypeLoadException is DllNotFoundException
1141-
&& ((isMonoRuntime && originalMessage == DllName.Universal)
1142-
|| originalMessage.ContainsQuotedValue(DllName.Universal)))
1125+
if ((isMonoRuntime && originalMessage == DllName.Universal)
1126+
|| originalMessage.ContainsQuotedValue(DllName.Universal))
11431127
{
11441128
const string buildInstructionsUrl =
11451129
"https://github.com/Microsoft/ChakraCore/wiki/Building-ChakraCore#{0}";
1146-
const string monoInstallationInstructionsUrl =
1130+
const string manualInstallationInstructionsUrl =
11471131
"https://github.com/Taritsyn/JavaScriptEngineSwitcher/wiki/JS-Engine-Switcher:-ChakraCore#{0}";
11481132
Architecture osArchitecture = RuntimeInformation.OSArchitecture;
11491133

1150-
StringBuilder messageBuilder = StringBuilderPool.GetBuilder();
1151-
messageBuilder.Append(jsEngineNotLoadedPart);
1152-
messageBuilder.Append(" ");
1153-
1134+
StringBuilder descriptionBuilder = StringBuilderPool.GetBuilder();
11541135
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
11551136
{
1156-
messageBuilder.AppendFormat(CoreStrings.Engine_AssemblyNotFound, DllName.ForWindows);
1157-
messageBuilder.Append(" ");
1158-
1137+
descriptionBuilder.AppendFormat(CoreStrings.Engine_AssemblyNotFound, DllName.ForWindows);
1138+
descriptionBuilder.Append(" ");
11591139
if (osArchitecture == Architecture.X64 || osArchitecture == Architecture.X86)
11601140
{
1161-
messageBuilder.AppendFormat(CoreStrings.Engine_NuGetPackageInstallationRequired,
1141+
descriptionBuilder.AppendFormat(CoreStrings.Engine_NuGetPackageInstallationRequired,
11621142
Utils.Is64BitProcess() ?
11631143
"JavaScriptEngineSwitcher.ChakraCore.Native.win-x64"
11641144
:
11651145
"JavaScriptEngineSwitcher.ChakraCore.Native.win-x86"
11661146
);
1167-
messageBuilder.Append(" ");
1168-
messageBuilder.Append(Strings.Engine_VcRedist2015InstallationRequired);
1147+
descriptionBuilder.Append(" ");
1148+
descriptionBuilder.Append(Strings.Engine_VcRedist2015InstallationRequired);
11691149
}
11701150
else if (osArchitecture == Architecture.Arm)
11711151
{
1172-
messageBuilder.AppendFormat(CoreStrings.Engine_NuGetPackageInstallationRequired,
1152+
descriptionBuilder.AppendFormat(CoreStrings.Engine_NuGetPackageInstallationRequired,
11731153
"JavaScriptEngineSwitcher.ChakraCore.Native.win8-arm");
11741154
}
11751155
else
11761156
{
1177-
messageBuilder.AppendFormat(CoreStrings.Engine_NoNuGetPackageForProcessorArchitecture,
1157+
descriptionBuilder.AppendFormat(CoreStrings.Engine_NoNuGetPackageForProcessorArchitecture,
11781158
"JavaScriptEngineSwitcher.ChakraCore.Native.win*",
11791159
osArchitecture.ToString().ToLowerInvariant()
11801160
);
1181-
messageBuilder.Append(" ");
1182-
messageBuilder.AppendFormat(Strings.Engine_BuildNativeAssemblyForCurrentProcessorArchitecture,
1161+
descriptionBuilder.Append(" ");
1162+
descriptionBuilder.AppendFormat(Strings.Engine_BuildNativeAssemblyForCurrentProcessorArchitecture,
11831163
DllName.ForWindows,
11841164
string.Format(buildInstructionsUrl, "windows")
11851165
);
11861166
}
11871167
}
11881168
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
11891169
{
1190-
messageBuilder.AppendFormat(CoreStrings.Engine_AssemblyNotFound, DllName.ForLinux);
1191-
messageBuilder.Append(" ");
1192-
1170+
descriptionBuilder.AppendFormat(CoreStrings.Engine_AssemblyNotFound, DllName.ForLinux);
1171+
descriptionBuilder.Append(" ");
11931172
if (isMonoRuntime)
11941173
{
1195-
messageBuilder.AppendFormat(Strings.Engine_ManualInstallationUnderMonoRequired,
1174+
descriptionBuilder.AppendFormat(Strings.Engine_ManualInstallationUnderMonoRequired,
11961175
"JavaScriptEngineSwitcher.ChakraCore.Native.linux-*",
1197-
string.Format(monoInstallationInstructionsUrl, "linux")
1176+
string.Format(manualInstallationInstructionsUrl, "linux")
11981177
);
11991178
}
12001179
else
12011180
{
12021181
if (osArchitecture == Architecture.X64)
12031182
{
1204-
messageBuilder.AppendFormat(CoreStrings.Engine_NuGetPackageInstallationRequired,
1183+
descriptionBuilder.AppendFormat(CoreStrings.Engine_NuGetPackageInstallationRequired,
12051184
"JavaScriptEngineSwitcher.ChakraCore.Native.linux-x64");
12061185
}
12071186
else
12081187
{
1209-
messageBuilder.AppendFormat(CoreStrings.Engine_NoNuGetPackageForProcessorArchitecture,
1188+
descriptionBuilder.AppendFormat(CoreStrings.Engine_NoNuGetPackageForProcessorArchitecture,
12101189
"JavaScriptEngineSwitcher.ChakraCore.Native.linux-*",
12111190
osArchitecture.ToString().ToLowerInvariant()
12121191
);
1213-
messageBuilder.Append(" ");
1214-
messageBuilder.AppendFormat(Strings.Engine_BuildNativeAssemblyForCurrentProcessorArchitecture,
1192+
descriptionBuilder.Append(" ");
1193+
descriptionBuilder.AppendFormat(Strings.Engine_BuildNativeAssemblyForCurrentProcessorArchitecture,
12151194
DllName.ForLinux,
12161195
string.Format(buildInstructionsUrl, "linux")
12171196
);
@@ -1220,31 +1199,30 @@ private static WrapperEngineLoadException WrapTypeLoadException(
12201199
}
12211200
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
12221201
{
1223-
messageBuilder.AppendFormat(CoreStrings.Engine_AssemblyNotFound, DllName.ForOsx);
1224-
messageBuilder.Append(" ");
1225-
1202+
descriptionBuilder.AppendFormat(CoreStrings.Engine_AssemblyNotFound, DllName.ForOsx);
1203+
descriptionBuilder.Append(" ");
12261204
if (isMonoRuntime)
12271205
{
1228-
messageBuilder.AppendFormat(Strings.Engine_ManualInstallationUnderMonoRequired,
1206+
descriptionBuilder.AppendFormat(Strings.Engine_ManualInstallationUnderMonoRequired,
12291207
"JavaScriptEngineSwitcher.ChakraCore.Native.osx-*",
1230-
string.Format(monoInstallationInstructionsUrl, "os-x")
1208+
string.Format(manualInstallationInstructionsUrl, "os-x")
12311209
);
12321210
}
12331211
else
12341212
{
12351213
if (osArchitecture == Architecture.X64)
12361214
{
1237-
messageBuilder.AppendFormat(CoreStrings.Engine_NuGetPackageInstallationRequired,
1215+
descriptionBuilder.AppendFormat(CoreStrings.Engine_NuGetPackageInstallationRequired,
12381216
"JavaScriptEngineSwitcher.ChakraCore.Native.osx-x64");
12391217
}
12401218
else
12411219
{
1242-
messageBuilder.AppendFormat(CoreStrings.Engine_NoNuGetPackageForProcessorArchitecture,
1220+
descriptionBuilder.AppendFormat(CoreStrings.Engine_NoNuGetPackageForProcessorArchitecture,
12431221
"JavaScriptEngineSwitcher.ChakraCore.Native.osx-*",
12441222
osArchitecture.ToString().ToLowerInvariant()
12451223
);
1246-
messageBuilder.Append(" ");
1247-
messageBuilder.AppendFormat(Strings.Engine_BuildNativeAssemblyForCurrentProcessorArchitecture,
1224+
descriptionBuilder.Append(" ");
1225+
descriptionBuilder.AppendFormat(Strings.Engine_BuildNativeAssemblyForCurrentProcessorArchitecture,
12481226
DllName.ForOsx,
12491227
string.Format(buildInstructionsUrl, "os-x")
12501228
);
@@ -1253,19 +1231,27 @@ private static WrapperEngineLoadException WrapTypeLoadException(
12531231
}
12541232
else
12551233
{
1256-
messageBuilder.Append(CoreStrings.Engine_OperatingSystemNotSupported);
1234+
descriptionBuilder.Append(CoreStrings.Engine_OperatingSystemNotSupported);
12571235
}
12581236

1259-
message = messageBuilder.ToString();
1260-
StringBuilderPool.ReleaseBuilder(messageBuilder);
1237+
description = descriptionBuilder.ToString();
1238+
StringBuilderPool.ReleaseBuilder(descriptionBuilder);
1239+
1240+
message = CoreErrorHelpers.GenerateEngineLoadErrorMessage(description, EngineName);
12611241
}
12621242
else
12631243
{
1264-
message = jsEngineNotLoadedPart + " " +
1265-
string.Format(CoreStrings.Common_SeeOriginalErrorMessage, originalMessage);
1244+
description = originalMessage;
1245+
message = CoreErrorHelpers.GenerateEngineLoadErrorMessage(description, EngineName, true);
12661246
}
12671247

1268-
return new WrapperEngineLoadException(message, EngineName, EngineVersion, originalTypeLoadException);
1248+
var wrapperEngineLoadException = new WrapperEngineLoadException(message, EngineName, EngineVersion,
1249+
originalDllNotFoundException)
1250+
{
1251+
Description = description
1252+
};
1253+
1254+
return wrapperEngineLoadException;
12691255
}
12701256

12711257
#endregion

src/JavaScriptEngineSwitcher.ChakraCore/Resources/Strings.Designer.cs

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/JavaScriptEngineSwitcher.ChakraCore/Resources/Strings.resx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@
175175
<value>Could not find suitable method or not enough arguments to invoke of '{0}' method of the host object.</value>
176176
</data>
177177
<data name="Engine_ManualInstallationUnderMonoRequired" xml:space="preserve">
178-
<value>{0} packages do not support installation under Mono, but you can to install the native assemblies manually ({1}).</value>
178+
<value>{0} packages do not support installation under Mono, but you can to install the native assembly manually ({1}).</value>
179179
</data>
180180
<data name="Engine_BuildNativeAssemblyForCurrentProcessorArchitecture" xml:space="preserve">
181181
<value>You can build the '{0}' assembly for the current processor architecture by using following instructions from official repository - {1}.</value>

src/JavaScriptEngineSwitcher.ChakraCore/Resources/Strings.ru-ru.resx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@
175175
<value>Не получается найти подходящий метод или не хватает аргументов для вызова метода "{0}" объекта хоста!</value>
176176
</data>
177177
<data name="Engine_ManualInstallationUnderMonoRequired" xml:space="preserve">
178-
<value>{0} пакеты не поддерживают установку под Mono, но можно установить нативные сборки вручную ({1}).</value>
178+
<value>{0} пакеты не поддерживают установку под Mono, но можно установить нативную сборку вручную ({1}).</value>
179179
</data>
180180
<data name="Engine_BuildNativeAssemblyForCurrentProcessorArchitecture" xml:space="preserve">
181181
<value>Вы можете собрать "{0}" для текущей архитектуры процессора, используя следующие инструкции из официального репозитория - {1}.</value>

0 commit comments

Comments
 (0)