Enhance error handling by formatting error messages

This commit is contained in:
ob-yalian
2026-04-01 20:52:46 +08:00
parent ad55f90bcc
commit b82fc0c267
9 changed files with 108 additions and 92 deletions
+12 -2
View File
@@ -37,13 +37,23 @@ inline void LogFatal(const char* file, int line, const std::string& message) {
std::cerr << "Check failed at " << file << ":" << line << ": " << message << std::endl;
std::abort();
}
inline const char* getObErrorMessage(const ob::Error& e) {
return e.getMessage() ? e.getMessage() : "Unknown OB error";
}
inline std::string formatObErrorWithStatus(const ob::Error& e) {
std::ostringstream os;
os << getObErrorMessage(e) << " status:" << static_cast<int>(e.getStatus());
return os.str();
}
} // namespace orbbec_camera
#define TRY_EXECUTE_BLOCK(block) \
try { \
block; \
} catch (const ob::Error& e) { \
std::string error_msg = e.getMessage() ? e.getMessage() : "Unknown OB error"; \
std::string error_msg = orbbec_camera::formatObErrorWithStatus(e); \
if (error_msg.find("Device is deactivated") != std::string::npos || \
error_msg.find("disconnected") != std::string::npos || \
error_msg.find("Send control transfer failed") != std::string::npos) { \
@@ -66,7 +76,7 @@ inline void LogFatal(const char* file, int line, const std::string& message) {
} catch (const ob::Error& e) { \
RCLCPP_ERROR_STREAM(logger_, "Failed to set " << property << " to " << value << " in " \
<< __FUNCTION__ << " at line " << __LINE__ \
<< ": " << e.getMessage()); \
<< ": " << orbbec_camera::formatObErrorWithStatus(e)); \
} catch (const std::exception& e) { \
RCLCPP_ERROR_STREAM(logger_, "Failed to set " << property << " to " << value << " in " \
<< __FUNCTION__ << " at line " << __LINE__ \