Skip to content

Commit 8f50167

Browse files
authored
Merge pull request #39 from REVrobotics/addCanBridgeDeviceNotFound-field-to-appropriate-errors
2 parents 401c5c9 + 85bbeea commit 8f50167

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed

src/canWrapper.cc

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@
2121
#include "canWrapper.h"
2222
#include "DfuSeFile.h"
2323

24-
#define DEVICE_NOT_FOUND_ERROR "Device not found. Make sure to run getDevices()"
25-
2624
#define REV_COMMON_HEARTBEAT_ID 0x00502C0
2725
#define SPARK_HEARTBEAT_ID 0x2052C80
2826
#define HEARTBEAT_PERIOD_MS 20
@@ -50,6 +48,12 @@ std::map<std::string, std::array<uint8_t, REV_COMMON_HEARTBEAT_LENGTH>> revCommo
5048
std::map<std::string, std::array<uint8_t, SPARK_HEARTBEAT_LENGTH>> sparkHeartbeatMap;
5149
auto latestHeartbeatAck = std::chrono::time_point<std::chrono::steady_clock>();
5250

51+
void throwDeviceNotFoundError(Napi::Env env) {
52+
Napi::Error error = Napi::Error::New(env, "CAN bridge device not found. Make sure to run getDevices()");
53+
error.Set("canBridgeDeviceNotFound", Napi::Boolean::New(env, true));
54+
error.ThrowAsJavaScriptException();
55+
}
56+
5357
// Only call when holding canDevicesMtx
5458
void removeExtraDevicesFromDeviceMap(std::vector<std::string> descriptors) {
5559
for (auto itr = canDeviceMap.begin(); itr != canDeviceMap.end(); ++itr) {
@@ -226,7 +230,7 @@ Napi::Object receiveMessage(const Napi::CallbackInfo& info) {
226230
auto deviceIterator = canDeviceMap.find(descriptor);
227231
if (deviceIterator == canDeviceMap.end()) {
228232
if (devicesRegisteredToHal.find(descriptor) != devicesRegisteredToHal.end()) return receiveHalMessage(info);
229-
Napi::Error::New(env, DEVICE_NOT_FOUND_ERROR).ThrowAsJavaScriptException();
233+
throwDeviceNotFoundError(env);
230234
return Napi::Object::New(env);
231235
}
232236

@@ -325,7 +329,7 @@ Napi::Number openStreamSession(const Napi::CallbackInfo& info) {
325329
std::scoped_lock lock{canDevicesMtx};
326330
auto deviceIterator = canDeviceMap.find(descriptor);
327331
if (deviceIterator == canDeviceMap.end()) {
328-
Napi::Error::New(env, DEVICE_NOT_FOUND_ERROR).ThrowAsJavaScriptException();
332+
throwDeviceNotFoundError(env);
329333
return Napi::Number::New(env, 0);
330334
}
331335

@@ -366,7 +370,7 @@ Napi::Array readStreamSession(const Napi::CallbackInfo& info) {
366370
std::scoped_lock lock{canDevicesMtx};
367371
auto deviceIterator = canDeviceMap.find(descriptor);
368372
if (deviceIterator == canDeviceMap.end()) {
369-
Napi::Error::New(env, DEVICE_NOT_FOUND_ERROR).ThrowAsJavaScriptException();
373+
throwDeviceNotFoundError(env);
370374
return Napi::Array::New(env);
371375
}
372376

@@ -415,7 +419,7 @@ Napi::Number closeStreamSession(const Napi::CallbackInfo& info) {
415419
std::scoped_lock lock{canDevicesMtx};
416420
auto deviceIterator = canDeviceMap.find(descriptor);
417421
if (deviceIterator == canDeviceMap.end()) {
418-
Napi::Error::New(env, DEVICE_NOT_FOUND_ERROR).ThrowAsJavaScriptException();
422+
throwDeviceNotFoundError(env);
419423
return Napi::Number::New(env, 0);
420424
}
421425

@@ -438,7 +442,7 @@ Napi::Object getCANDetailStatus(const Napi::CallbackInfo& info) {
438442
std::scoped_lock lock{canDevicesMtx};
439443
auto deviceIterator = canDeviceMap.find(descriptor);
440444
if (deviceIterator == canDeviceMap.end()) {
441-
Napi::Error::New(env, DEVICE_NOT_FOUND_ERROR).ThrowAsJavaScriptException();
445+
throwDeviceNotFoundError(env);
442446
return Napi::Object::New(env);
443447
}
444448

@@ -510,7 +514,7 @@ Napi::Number sendCANMessage(const Napi::CallbackInfo& info) {
510514
}
511515
int status = _sendCANMessage(descriptor, messageId, messageData, dataParam.Length(), repeatPeriodMs);
512516
if (status < 0) {
513-
Napi::Error::New(env, DEVICE_NOT_FOUND_ERROR).ThrowAsJavaScriptException();
517+
throwDeviceNotFoundError(env);
514518
}
515519
return Napi::Number::New(env, status);
516520
}
@@ -538,7 +542,7 @@ Napi::Number sendRtrMessage(const Napi::CallbackInfo& info) {
538542
}
539543
int status = _sendCANMessage(descriptor, messageId, messageData, dataParam.Length(), repeatPeriodMs);
540544
if (status < 0) {
541-
Napi::Error::New(env, DEVICE_NOT_FOUND_ERROR).ThrowAsJavaScriptException();
545+
throwDeviceNotFoundError(env);
542546
}
543547
return Napi::Number::New(env, status);
544548
}
@@ -736,7 +740,7 @@ Napi::Object getLatestMessageOfEveryReceivedArbId(const Napi::CallbackInfo& info
736740
auto deviceIterator = canDeviceMap.find(descriptor);
737741
if (deviceIterator == canDeviceMap.end()) {
738742
if (devicesRegisteredToHal.find(descriptor) != devicesRegisteredToHal.end()) return receiveHalMessage(info);
739-
Napi::Error::New(env, DEVICE_NOT_FOUND_ERROR).ThrowAsJavaScriptException();
743+
throwDeviceNotFoundError(env);
740744
return Napi::Object::New(env);
741745
}
742746
device = deviceIterator->second;

0 commit comments

Comments
 (0)