Руслан Урядинский / libuavcan

Dependents:   UAVCAN UAVCAN_Subscriber

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers exception.hpp Source File

exception.hpp

00001 /*
00002  * Copyright (C) 2014 Pavel Kirienko <pavel.kirienko@gmail.com>
00003  */
00004 
00005 #pragma once
00006 
00007 #include <cerrno>
00008 #include <cstring>
00009 #include <stdexcept>
00010 
00011 namespace uavcan_linux
00012 {
00013 /**
00014  * This is the root exception class for all exceptions that can be thrown from the libuavcan Linux driver.
00015  */
00016 class Exception : public std::runtime_error
00017 {
00018     const int errno_;
00019 
00020     static std::string makeErrorString(const std::string& descr, int use_errno)
00021     {
00022         return descr + " [errno " + std::to_string(use_errno) + " \"" + std::strerror(use_errno) + "\"]";
00023     }
00024 
00025 public:
00026     explicit Exception(const std::string& descr, int use_errno = errno)
00027         : std::runtime_error(makeErrorString(descr, use_errno))
00028         , errno_(use_errno)
00029     { }
00030 
00031     /**
00032      * Returns standard UNIX errno value captured at the moment
00033      * when this exception object was constructed.
00034      */
00035     int getErrno() const { return errno_; }
00036 };
00037 
00038 /**
00039  * This type is thrown when a Libuavcan API method exits with error.
00040  * The error code is stored in the exception object and is avialable via @ref getLibuavcanErrorCode().
00041  */
00042 class LibuavcanErrorException : public Exception
00043 {
00044     const std::int16_t error_;
00045 
00046     static std::string makeErrorString(std::int16_t e)
00047     {
00048         return "Libuavcan error (" + std::to_string(e) + ")";
00049     }
00050 
00051 public:
00052     explicit LibuavcanErrorException(std::int16_t uavcan_error_code) :
00053         Exception(makeErrorString(uavcan_error_code)),
00054         error_(std::abs(uavcan_error_code))
00055     { }
00056 
00057     std::int16_t getLibuavcanErrorCode() const { return error_; }
00058 };
00059 
00060 /**
00061  * This exception is thrown when all available interfaces become down.
00062  */
00063 class AllIfacesDownException : public Exception
00064 {
00065 public:
00066     AllIfacesDownException() : Exception("All ifaces are down", ENETDOWN) { }
00067 };
00068 
00069 }