exception.hpp 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /*
  2. * Copyright (C) 2014 Pavel Kirienko <pavel.kirienko@gmail.com>
  3. */
  4. #pragma once
  5. #include <cerrno>
  6. #include <cstring>
  7. #include <stdexcept>
  8. namespace uavcan_linux
  9. {
  10. /**
  11. * This is the root exception class for all exceptions that can be thrown from the libuavcan Linux driver.
  12. */
  13. class Exception : public std::runtime_error
  14. {
  15. const int errno_;
  16. static std::string makeErrorString(const std::string& descr, int use_errno)
  17. {
  18. return descr + " [errno " + std::to_string(use_errno) + " \"" + std::strerror(use_errno) + "\"]";
  19. }
  20. public:
  21. explicit Exception(const std::string& descr, int use_errno = errno)
  22. : std::runtime_error(makeErrorString(descr, use_errno))
  23. , errno_(use_errno)
  24. { }
  25. /**
  26. * Returns standard UNIX errno value captured at the moment
  27. * when this exception object was constructed.
  28. */
  29. int getErrno() const { return errno_; }
  30. };
  31. /**
  32. * This type is thrown when a Libuavcan API method exits with error.
  33. * The error code is stored in the exception object and is avialable via @ref getLibuavcanErrorCode().
  34. */
  35. class LibuavcanErrorException : public Exception
  36. {
  37. const std::int16_t error_;
  38. static std::string makeErrorString(std::int16_t e)
  39. {
  40. return "Libuavcan error (" + std::to_string(e) + ")";
  41. }
  42. public:
  43. explicit LibuavcanErrorException(std::int16_t uavcan_error_code) :
  44. Exception(makeErrorString(uavcan_error_code)),
  45. error_(std::abs(uavcan_error_code))
  46. { }
  47. std::int16_t getLibuavcanErrorCode() const { return error_; }
  48. };
  49. /**
  50. * This exception is thrown when all available interfaces become down.
  51. */
  52. class AllIfacesDownException : public Exception
  53. {
  54. public:
  55. AllIfacesDownException() : Exception("All ifaces are down", ENETDOWN) { }
  56. };
  57. }