Skip to content

Commit a705883

Browse files
committed
add error handling
1 parent 03bae4d commit a705883

File tree

1 file changed

+34
-22
lines changed

1 file changed

+34
-22
lines changed

windows/RNPrint/RNPrint.cpp

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -38,67 +38,79 @@ winrt::fire_and_forget RNPrint::Print(
3838
std::string filePathStr = options.filePath.value();
3939
auto filePathHstring = winrt::to_hstring(filePathStr);
4040

41-
// Check if the filePath is a URL (http/https)
42-
if (filePathStr.rfind("http://", 0) == 0 || filePathStr.rfind("https://", 0) == 0) {
43-
// Download the file to a temporary location first
44-
m_context.UIDispatcher().Post([options, filePathStr, promise = std::move(promise)]() mutable -> winrt::fire_and_forget {
41+
if (filePathStr.rfind("http://", 0) == 0 || filePathStr.rfind("https://", 0) == 0)
42+
{
43+
auto context = m_context; // Capture context safely
44+
m_context.UIDispatcher().Post([context, filePathStr, jobName = options.jobName, promise]() mutable -> winrt::fire_and_forget {
4545
using namespace winrt::Windows::Storage;
4646
using namespace winrt::Windows::Web::Http;
47-
using namespace winrt::Windows::Foundation;
4847

4948
try {
5049
auto tempFolder = ApplicationData::Current().TemporaryFolder();
51-
auto uri = winrt::Windows::Foundation::Uri(winrt::to_hstring(filePathStr));
50+
winrt::Windows::Foundation::Uri uri{ winrt::to_hstring(filePathStr) };
5251
HttpClient httpClient;
5352

5453
auto buffer = co_await httpClient.GetBufferAsync(uri);
54+
std::wstring fileName = L"printfile.pdf";
55+
5556
auto path = uri.Path();
56-
std::wstring fileName = L"printfile";
5757
if (!path.empty()) {
5858
std::wstring wpath = path.c_str();
5959
size_t pos = wpath.find_last_of(L"/\\");
60-
if (pos != std::wstring::npos && pos + 1 < wpath.size()) {
60+
if (pos != std::wstring::npos && pos + 1 < wpath.length()) {
6161
fileName = wpath.substr(pos + 1);
6262
}
63-
else {
64-
fileName = wpath;
65-
}
6663
}
64+
6765
auto file = co_await tempFolder.CreateFileAsync(fileName, CreationCollisionOption::GenerateUniqueName);
6866
co_await FileIO::WriteBufferAsync(file, buffer);
6967

70-
std::wstring nativePath = file.Path().c_str(); // Get full native path
71-
ShellExecuteW(NULL, L"print", nativePath.c_str(), NULL, NULL, SW_SHOWNORMAL);
72-
promise.Resolve(options.jobName);
68+
std::wstring nativePath = file.Path().c_str();
69+
auto result = ShellExecuteW(nullptr, L"print", nativePath.c_str(), nullptr, nullptr, SW_SHOWNORMAL);
70+
71+
if ((INT_PTR)result <= 32) {
72+
promise.Reject(L"Failed to print downloaded file.");
73+
} else {
74+
promise.Resolve(jobName);
75+
}
7376
}
7477
catch (...) {
7578
promise.Reject(L"Exception occurred while downloading or printing file.");
7679
}
80+
7781
co_return;
7882
});
79-
} else {
80-
// Local file path
81-
m_context.UIDispatcher().Post([options, filePathHstring, promise = std::move(promise)]() mutable -> winrt::fire_and_forget {
83+
}
84+
else
85+
{
86+
auto context = m_context; // Capture safely
87+
m_context.UIDispatcher().Post([context, filePathHstring, jobName = options.jobName, promise]() mutable -> winrt::fire_and_forget {
8288
using namespace winrt::Windows::Storage;
83-
using namespace winrt::Windows::Foundation;
8489

8590
try {
8691
StorageFile file = co_await StorageFile::GetFileFromPathAsync(filePathHstring);
87-
std::wstring nativePath = file.Path().c_str(); // Get full native path
88-
ShellExecuteW(NULL, L"print", nativePath.c_str(), NULL, NULL, SW_SHOWNORMAL);
89-
promise.Resolve(options.jobName);
92+
std::wstring nativePath = file.Path().c_str();
93+
auto result = ShellExecuteW(nullptr, L"print", nativePath.c_str(), nullptr, nullptr, SW_SHOWNORMAL);
94+
95+
if ((INT_PTR)result <= 32) {
96+
promise.Reject(L"Failed to print local file.");
97+
} else {
98+
promise.Resolve(jobName);
99+
}
90100
}
91101
catch (...) {
92102
promise.Reject(L"Failed to open or print local file.");
93103
}
104+
94105
co_return;
95106
});
96107
}
97108
}
98109
catch (...)
99110
{
100-
promise.Reject(L"Unknown error in Print function");
111+
promise.Reject(L"Unknown error in Print function.");
101112
}
113+
102114
co_return;
103115
}
104116
} // namespace winrt::RNPrint

0 commit comments

Comments
 (0)