@@ -574,14 +574,26 @@ std::vector<std::filesystem::path> WindowsPlatform::GetNewAPILayerJSONPaths() {
574574 return ret;
575575}
576576
577- std::expected<LoaderData, std::string> WindowsPlatform::GetLoaderData () {
577+ template <class T >
578+ static std::unexpected<T> UnexpectedHRESULT (HRESULT value) {
579+ return std::unexpected {
580+ T {{value, std::system_category ()}},
581+ };
582+ }
583+
584+ template <class T >
585+ static std::unexpected<T> UnexpectedGetLastError () {
586+ return UnexpectedHRESULT<T>(HRESULT_FROM_WIN32 (GetLastError ()));
587+ }
588+
589+ std::expected<LoaderData, LoaderData::Error> WindowsPlatform::GetLoaderData () {
578590 if (!mLoaderData ) {
579591 mLoaderData = GetLoaderDataWithoutCache ();
580592 }
581593 return mLoaderData .value ();
582594}
583595
584- std::expected<LoaderData, std::string >
596+ std::expected<LoaderData, LoaderData::Error >
585597WindowsPlatform::GetLoaderDataWithoutCache () {
586598 SECURITY_ATTRIBUTES saAttr {
587599 .nLength = sizeof (SECURITY_ATTRIBUTES),
@@ -591,15 +603,16 @@ WindowsPlatform::GetLoaderDataWithoutCache() {
591603 wil::unique_handle stdoutRead;
592604 wil::unique_handle stdoutWrite;
593605 if (!CreatePipe (stdoutRead.put (), stdoutWrite.put (), &saAttr, 0 )) {
594- return std::unexpected ( " Failed to create pipe " );
606+ return UnexpectedGetLastError<LoaderData::PipeCreationError>( );
595607 }
596608 if (!SetHandleInformation (stdoutRead.get (), HANDLE_FLAG_INHERIT, 0 )) {
597- return std::unexpected ( " Failed to set pipe properties " );
609+ return UnexpectedGetLastError<LoaderData::PipeAttributeError>( );
598610 }
599611
600- wchar_t modulePath[MAX_PATH];
601- if (!GetModuleFileNameW (nullptr , modulePath, MAX_PATH)) {
602- return std::unexpected (" Failed to get executable path" );
612+ constexpr auto MaxPathExtended = 32768 ;
613+ wchar_t modulePath[MaxPathExtended];
614+ if (!GetModuleFileNameW (nullptr , modulePath, MaxPathExtended)) {
615+ return UnexpectedGetLastError<LoaderData::CanNotFindExecutableError>();
603616 }
604617
605618 STARTUPINFOW si {
@@ -623,7 +636,7 @@ WindowsPlatform::GetLoaderDataWithoutCache() {
623636 nullptr ,
624637 &si,
625638 &pi)) {
626- return std::unexpected ( " Failed to create process " );
639+ return UnexpectedGetLastError<LoaderData::CanNotSpawnError>( );
627640 }
628641
629642 wil::unique_handle process {pi.hProcess };
@@ -643,14 +656,13 @@ WindowsPlatform::GetLoaderDataWithoutCache() {
643656
644657 DWORD exitCode;
645658 if (!GetExitCodeProcess (process.get (), &exitCode) || exitCode != 0 ) {
646- return std::unexpected (
647- std::format (" Subprocess failed with exit code {}" , exitCode));
659+ return std::unexpected {LoaderData::BadExitCodeError {exitCode}};
648660 }
649661
650662 try {
651663 return static_cast <LoaderData>(nlohmann::json::parse (output));
652664 } catch (const nlohmann::json::exception& e) {
653- return std::unexpected ( e.what ()) ;
665+ return std::unexpected {LoaderData::InvalidJSONError { e.what ()}} ;
654666 }
655667}
656668
0 commit comments