Skip to content

Commit

Permalink
Added proper JS stacktrace support for the JS callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
Aragas committed May 30, 2024
1 parent 029a92f commit 823e893
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,6 @@ namespace Bannerlord::LauncherManager
}
catch (const Napi::Error &e)
{
std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> conv;
return info.Env().Null();
}
}
Expand All @@ -189,9 +188,7 @@ namespace Bannerlord::LauncherManager
}
catch (const Napi::Error &e)
{
const auto stack = e.Value().Get("stack").As<String>();
std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> conv;
return Create(return_value_void{Copy(stack.Utf16Value())});
return Create(return_value_void{Copy(GetErrorMessage(e))});
}
}
static return_value_json *getLoadOrder(void *p_owner) noexcept
Expand All @@ -206,8 +203,7 @@ namespace Bannerlord::LauncherManager
}
catch (const Napi::Error &e)
{
std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> conv;
return Create(return_value_json{Copy(conv.from_bytes(e.Message())), nullptr});
return Create(return_value_json{Copy(GetErrorMessage(e)), nullptr});
}
}
static return_value_void *setLoadOrder(void *p_owner, char16_t *p_load_order) noexcept
Expand All @@ -223,8 +219,7 @@ namespace Bannerlord::LauncherManager
}
catch (const Napi::Error &e)
{
std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> conv;
return Create(return_value_void{Copy(conv.from_bytes(e.Message()))});
return Create(return_value_void{Copy(GetErrorMessage(e))});
}
}
static return_value_void *sendNotification(void *p_owner, char16_t *p_id, char16_t *p_type, char16_t *p_message, uint32_t displayMs) noexcept
Expand All @@ -243,8 +238,7 @@ namespace Bannerlord::LauncherManager
}
catch (const Napi::Error &e)
{
std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> conv;
return Create(return_value_void{Copy(conv.from_bytes(e.Message()))});
return Create(return_value_void{Copy(GetErrorMessage(e))});
}
}
static return_value_void *sendDialog(void *p_owner, char16_t *p_type, char16_t *p_title, char16_t *p_message, char16_t *p_filters, void *p_callback_ptr, void(__cdecl *p_callback)(void *, char16_t *)) noexcept
Expand Down Expand Up @@ -281,8 +275,7 @@ namespace Bannerlord::LauncherManager
}
catch (const Napi::Error &e)
{
std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> conv;
return Create(return_value_void{Copy(conv.from_bytes(e.Message()))});
return Create(return_value_void{Copy(GetErrorMessage(e))});
}
}
static return_value_string *getInstallPath(void *p_owner) noexcept
Expand All @@ -297,8 +290,7 @@ namespace Bannerlord::LauncherManager
}
catch (const Napi::Error &e)
{
std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> conv;
return Create(return_value_string{Copy(conv.from_bytes(e.Message())), nullptr});
return Create(return_value_string{Copy(GetErrorMessage(e)), nullptr});
}
}
static return_value_data *readFileContent(void *p_owner, char16_t *p_file_path, int32_t p_offset, int32_t p_length) noexcept
Expand Down Expand Up @@ -329,8 +321,7 @@ namespace Bannerlord::LauncherManager
}
catch (const Napi::Error &e)
{
std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> conv;
return Create(return_value_data{Copy(conv.from_bytes(e.Message())), nullptr, 0});
return Create(return_value_data{Copy(GetErrorMessage(e)), nullptr, 0});
}
}
static return_value_void *writeFileContent(void *p_owner, char16_t *p_file_path, uint8_t *p_data, int32_t length) noexcept
Expand All @@ -352,8 +343,7 @@ namespace Bannerlord::LauncherManager
}
catch (const Napi::Error &e)
{
std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> conv;
return Create(return_value_void{Copy(conv.from_bytes(e.Message()))});
return Create(return_value_void{Copy(GetErrorMessage(e))});
}
}
static return_value_json *readDirectoryFileList(void *p_owner, char16_t *p_directory_path) noexcept
Expand All @@ -374,8 +364,7 @@ namespace Bannerlord::LauncherManager
}
catch (const Napi::Error &e)
{
std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> conv;
return Create(return_value_json{Copy(conv.from_bytes(e.Message())), nullptr});
return Create(return_value_json{Copy(GetErrorMessage(e)), nullptr});
}
}
static return_value_json *readDirectoryList(void *p_owner, char16_t *p_directory_path) noexcept
Expand All @@ -396,8 +385,7 @@ namespace Bannerlord::LauncherManager
}
catch (const Napi::Error &e)
{
std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> conv;
return Create(return_value_json{Copy(conv.from_bytes(e.Message())), nullptr});
return Create(return_value_json{Copy(GetErrorMessage(e)), nullptr});
}
}
static return_value_json *getAllModuleViewModels(void *p_owner) noexcept
Expand All @@ -417,8 +405,7 @@ namespace Bannerlord::LauncherManager
}
catch (const Napi::Error &e)
{
std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> conv;
return Create(return_value_json{Copy(conv.from_bytes(e.Message())), nullptr});
return Create(return_value_json{Copy(GetErrorMessage(e)), nullptr});
}
}
static return_value_json *getModuleViewModels(void *p_owner) noexcept
Expand All @@ -438,8 +425,7 @@ namespace Bannerlord::LauncherManager
}
catch (const Napi::Error &e)
{
std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> conv;
return Create(return_value_json{Copy(conv.from_bytes(e.Message())), nullptr});
return Create(return_value_json{Copy(GetErrorMessage(e)), nullptr});
}
}
static return_value_void *setModuleViewModels(void *p_owner, char16_t *p_module_view_models) noexcept
Expand All @@ -460,8 +446,7 @@ namespace Bannerlord::LauncherManager
}
catch (const Napi::Error &e)
{
std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> conv;
return Create(return_value_void{Copy(conv.from_bytes(e.Message()))});
return Create(return_value_void{Copy(GetErrorMessage(e))});
}
}
static return_value_json *getOptions(void *p_owner) noexcept
Expand All @@ -481,8 +466,7 @@ namespace Bannerlord::LauncherManager
}
catch (const Napi::Error &e)
{
std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> conv;
return Create(return_value_json{Copy(conv.from_bytes(e.Message())), nullptr});
return Create(return_value_json{Copy(GetErrorMessage(e)), nullptr});
}
}
static return_value_json *getState(void *p_owner) noexcept
Expand All @@ -502,8 +486,7 @@ namespace Bannerlord::LauncherManager
}
catch (const Napi::Error &e)
{
std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> conv;
return Create(return_value_json{Copy(conv.from_bytes(e.Message())), nullptr});
return Create(return_value_json{Copy(GetErrorMessage(e)), nullptr});
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ namespace Bannerlord::ModuleManager
}
catch (const Napi::Error &e)
{
std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> conv;
return Create(return_value_bool{Copy(conv.from_bytes(e.Message())), false});
return Create(return_value_bool{Copy(GetErrorMessage(e)), false});
}
}

Expand All @@ -56,8 +55,7 @@ namespace Bannerlord::ModuleManager
}
catch (const Napi::Error &e)
{
std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> conv;
return Create(return_value_bool{Copy(conv.from_bytes(e.Message())), false});
return Create(return_value_bool{Copy(GetErrorMessage(e)), false});
}
}
static return_value_void *setSelected(param_ptr *p_owner, param_string *p_module_id, param_bool value_raw) noexcept
Expand All @@ -75,8 +73,7 @@ namespace Bannerlord::ModuleManager
}
catch (const Napi::Error &e)
{
std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> conv;
return Create(return_value_void{Copy(conv.from_bytes(e.Message()))});
return Create(return_value_void{Copy(GetErrorMessage(e))});
}
}
static return_value_bool *getDisabled(param_ptr *p_owner, param_string *p_module_id) noexcept
Expand All @@ -92,8 +89,7 @@ namespace Bannerlord::ModuleManager
}
catch (const Napi::Error &e)
{
std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> conv;
return Create(return_value_bool{Copy(conv.from_bytes(e.Message())), false});
return Create(return_value_bool{Copy(GetErrorMessage(e)), false});
}
}
static return_value_void *setDisabled(param_ptr *p_owner, param_string *p_module_id, param_bool value_raw) noexcept
Expand All @@ -111,8 +107,7 @@ namespace Bannerlord::ModuleManager
}
catch (const Napi::Error &e)
{
std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> conv;
return Create(return_value_void{Copy(conv.from_bytes(e.Message()))});
return Create(return_value_void{Copy(GetErrorMessage(e))});
}
}

Expand Down
13 changes: 13 additions & 0 deletions src/Bannerlord.LauncherManager.Native.TypeScript/src/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,19 @@ namespace Utils
return dst;
}

std::u16string GetErrorMessage(const Napi::Error e)
{
const auto errorValue = e.Value();
if (errorValue.Has("stack"))
{
const auto stack = e.Value().Get("stack").As<String>();
return stack.Utf16Value();
}

std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> conv;

Check failure on line 126 in src/Bannerlord.LauncherManager.Native.TypeScript/src/utils.hpp

View workflow job for this annotation

GitHub Actions / Publish on NPM and GPR

'wstring_convert': is not a member of 'std'

Check failure on line 126 in src/Bannerlord.LauncherManager.Native.TypeScript/src/utils.hpp

View workflow job for this annotation

GitHub Actions / Publish on NPM and GPR

'wstring_convert': undeclared identifier

Check failure on line 126 in src/Bannerlord.LauncherManager.Native.TypeScript/src/utils.hpp

View workflow job for this annotation

GitHub Actions / Publish on NPM and GPR

'codecvt_utf8_utf16': is not a member of 'std'

Check failure on line 126 in src/Bannerlord.LauncherManager.Native.TypeScript/src/utils.hpp

View workflow job for this annotation

GitHub Actions / Publish on NPM and GPR

'codecvt_utf8_utf16': undeclared identifier

Check failure on line 126 in src/Bannerlord.LauncherManager.Native.TypeScript/src/utils.hpp

View workflow job for this annotation

GitHub Actions / Publish on NPM and GPR

type 'char16_t' unexpected
return conv.from_bytes(e.Message());

Check failure on line 127 in src/Bannerlord.LauncherManager.Native.TypeScript/src/utils.hpp

View workflow job for this annotation

GitHub Actions / Publish on NPM and GPR

'conv': undeclared identifier
}

void ConsoleLog(const Env env, const String message)
{
const auto consoleObject = env.Global().Get("console").As<Object>();
Expand Down

0 comments on commit 823e893

Please sign in to comment.