Skip to content

Commit 27b8cd2

Browse files
committed
Merge branch '89-catch_exc_in_C_API'
Catch communication exceptions in C API to avoid crash. Fixes #89
2 parents f72b672 + 1766a8e commit 27b8cd2

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

NK_C_API.cc

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ uint8_t * get_with_array_result(T func){
6464
catch (LibraryException & libraryException){
6565
NK_last_command_status = libraryException.exception_id();
6666
}
67+
catch (const DeviceCommunicationException &deviceException){
68+
NK_last_command_status = 256-deviceException.getType();
69+
}
6770
return nullptr;
6871
}
6972

@@ -79,6 +82,9 @@ const char* get_with_string_result(T func){
7982
catch (LibraryException & libraryException){
8083
NK_last_command_status = libraryException.exception_id();
8184
}
85+
catch (const DeviceCommunicationException &deviceException){
86+
NK_last_command_status = 256-deviceException.getType();
87+
}
8288
return "";
8389
}
8490

@@ -94,6 +100,9 @@ auto get_with_result(T func){
94100
catch (LibraryException & libraryException){
95101
NK_last_command_status = libraryException.exception_id();
96102
}
103+
catch (const DeviceCommunicationException &deviceException){
104+
NK_last_command_status = 256-deviceException.getType();
105+
}
97106
return static_cast<decltype(func())>(0);
98107
}
99108

@@ -140,6 +149,11 @@ extern "C" {
140149
NK_last_command_status = commandFailedException.last_command_status;
141150
return commandFailedException.last_command_status;
142151
}
152+
catch (const DeviceCommunicationException &deviceException){
153+
NK_last_command_status = 256-deviceException.getType();
154+
cerr << deviceException.what() << endl;
155+
return 0;
156+
}
143157
catch (std::runtime_error &e) {
144158
cerr << e.what() << endl;
145159
return 0;

0 commit comments

Comments
 (0)