MotionLib  1.0.0
SamBuCa motion library
Exception.cpp
1 /*
2  * Copyright (C) 2021 CERN
3  *
4  * This software is provided 'as-is', without any express or implied
5  * warranty. In no event will the authors be held liable for any damages
6  * arising from the use of this software.
7  *
8  * Permission is granted to anyone to use this software for any purpose,
9  * including commercial applications, and to alter it and redistribute it
10  * freely, subject to the following restrictions:
11  *
12  * 1. The origin of this software must not be misrepresented; you must not
13  * claim that you wrote the original software. If you use this software
14  * in a product, an acknowledgment in the product documentation would be
15  * appreciated but is not required.
16  * 2. Altered source versions must be plainly marked as such, and must not be
17  * misrepresented as being the original software.
18  * 3. This notice may not be removed or altered from any source distribution.
19  *
20  * Created on: 2021-11-22
21  * Author: Michal Mysior <mmysior> <michal.mysior@cern.ch>
22  *
23  */
24 
25 #include "Exception.hpp"
26 
27 #include <cerrno>
28 #include <cstring>
29 #include <system_error>
30 
31 namespace smc {
32 
33 Exception::Exception(std::error_code ev) : m_errorCode{ev} {}
34 
35 Exception::Exception(std::error_code ev, const std::string &what) :
36  m_errorCode{ev},
37  m_what(what)
38 {}
39 
40 const char *Exception::what() const noexcept
41 {
42  if (m_what.empty())
43  m_what = m_errorCode.message();
44  return m_what.c_str();
45 }
46 
47 struct ErrorCategory : std::error_category
48 {
49  const char *name() const noexcept override;
50  std::string message(int ev) const override;
51 };
52 
53 const char *ErrorCategory::name() const noexcept
54 {
55  return "MotionControllerError";
56 }
57 
58 std::string ErrorCategory::message(int ev) const
59 {
60  if (ev == 0)
61  return "Ok";
62  else if (ev < 1000)
63  return ::strerror(ev);
64 
65  switch (static_cast<ErrorCode>(ev))
66  {
67  case ErrorCode::IncompatibleUnits: return "Cannot convert units";
68  case ErrorCode::InternalError: return "Internal error";
69  default: return "Unknown error";
70  }
71 }
72 
73 const ErrorCategory errorCategory{};
74 
75 std::error_code make_error_code(ErrorCode e)
76 {
77  return {static_cast<int>(e), errorCategory};
78 }
79 
80 Exception make_errno_exception(std::error_code ev)
81 {
82  return Exception(ev, ::strerror(errno));
83 }
84 
85 } /* namespace smc */
ErrorCode
Error related to MotionController.
Definition: Exception.hpp:44
main motion-lib namespace
Definition: Client.cpp:30