Program Listing for File exceptions.hpp

Return to documentation for file (include/rclcpp/exceptions/exceptions.hpp)

// Copyright 2016 Open Source Robotics Foundation, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef RCLCPP__EXCEPTIONS__EXCEPTIONS_HPP_
#define RCLCPP__EXCEPTIONS__EXCEPTIONS_HPP_

#include <stdexcept>
#include <string>
#include <vector>

#include "rcl/error_handling.h"
#include "rcl/types.h"
#include "rclcpp/visibility_control.hpp"

#include "rcpputils/join.hpp"

namespace rclcpp
{
namespace exceptions
{

class InvalidNodeError : public std::runtime_error
{
public:
  InvalidNodeError()
  : std::runtime_error("node is invalid") {}
};

class NameValidationError : public std::invalid_argument
{
public:
  NameValidationError(
    const char * name_type_,
    const char * name_,
    const char * error_msg_,
    size_t invalid_index_)
  : std::invalid_argument(format_error(name_type_, name_, error_msg_, invalid_index_)),
    name_type(name_type_), name(name_), error_msg(error_msg_), invalid_index(invalid_index_)
  {}

  static std::string
  format_error(
    const char * name_type,
    const char * name,
    const char * error_msg,
    size_t invalid_index);

  const std::string name_type;
  const std::string name;
  const std::string error_msg;
  const size_t invalid_index;
};

class InvalidNodeNameError : public NameValidationError
{
public:
  InvalidNodeNameError(const char * node_name, const char * error_msg, size_t invalid_index)
  : NameValidationError("node name", node_name, error_msg, invalid_index)
  {}
};

class InvalidNamespaceError : public NameValidationError
{
public:
  InvalidNamespaceError(const char * namespace_, const char * error_msg, size_t invalid_index)
  : NameValidationError("namespace", namespace_, error_msg, invalid_index)
  {}
};

class InvalidTopicNameError : public NameValidationError
{
public:
  InvalidTopicNameError(const char * namespace_, const char * error_msg, size_t invalid_index)
  : NameValidationError("topic name", namespace_, error_msg, invalid_index)
  {}
};

class InvalidServiceNameError : public NameValidationError
{
public:
  InvalidServiceNameError(const char * namespace_, const char * error_msg, size_t invalid_index)
  : NameValidationError("service name", namespace_, error_msg, invalid_index)
  {}
};

class InvalidServiceTypeError : public std::runtime_error
{
public:
  InvalidServiceTypeError()
  : std::runtime_error("Service type is invalid.") {}
  explicit InvalidServiceTypeError(const std::string & msg)
  : std::runtime_error(msg) {}
};

class UnimplementedError : public std::runtime_error
{
public:
  UnimplementedError()
  : std::runtime_error("This code is unimplemented.") {}
  explicit UnimplementedError(const std::string & msg)
  : std::runtime_error(msg) {}
};

typedef void (* reset_error_function_t)();


/* *INDENT-OFF* */  // Uncrustify cannot yet understand [[noreturn]] properly
RCLCPP_PUBLIC
void
throw_from_rcl_error [[noreturn]] (
  rcl_ret_t ret,
  const std::string & prefix = "",
  const rcl_error_state_t * error_state = nullptr,
  reset_error_function_t reset_error = rcl_reset_error);
/* *INDENT-ON* */

class RCLErrorBase
{
public:
  RCLCPP_PUBLIC
  RCLErrorBase(rcl_ret_t ret, const rcl_error_state_t * error_state);
  virtual ~RCLErrorBase() {}

  rcl_ret_t ret;
  std::string message;
  std::string file;
  size_t line;
  std::string formatted_message;
};

class RCLError : public RCLErrorBase, public std::runtime_error
{
public:
  RCLCPP_PUBLIC
  RCLError(rcl_ret_t ret, const rcl_error_state_t * error_state, const std::string & prefix);
  RCLCPP_PUBLIC
  RCLError(const RCLErrorBase & base_exc, const std::string & prefix);
};

class RCLBadAlloc : public RCLErrorBase, public std::bad_alloc
{
public:
  RCLCPP_PUBLIC
  RCLBadAlloc(rcl_ret_t ret, const rcl_error_state_t * error_state);
  RCLCPP_PUBLIC
  explicit RCLBadAlloc(const RCLErrorBase & base_exc);
};

class RCLInvalidArgument : public RCLErrorBase, public std::invalid_argument
{
public:
  RCLCPP_PUBLIC
  RCLInvalidArgument(
    rcl_ret_t ret,
    const rcl_error_state_t * error_state,
    const std::string & prefix);
  RCLCPP_PUBLIC
  RCLInvalidArgument(const RCLErrorBase & base_exc, const std::string & prefix);
};

class RCLInvalidROSArgsError : public RCLErrorBase, public std::runtime_error
{
public:
  RCLCPP_PUBLIC
  RCLInvalidROSArgsError(
    rcl_ret_t ret, const rcl_error_state_t * error_state, const std::string & prefix);
  RCLCPP_PUBLIC
  RCLInvalidROSArgsError(const RCLErrorBase & base_exc, const std::string & prefix);
};

class UnknownROSArgsError : public std::runtime_error
{
public:
  explicit UnknownROSArgsError(std::vector<std::string> && unknown_ros_args_in)
  : std::runtime_error(
      "found unknown ROS arguments: '" + rcpputils::join(unknown_ros_args_in, "', '") + "'"),
    unknown_ros_args(unknown_ros_args_in)
  {
  }

  const std::vector<std::string> unknown_ros_args;
};

class UnknownTypeError : public std::runtime_error
{
public:
  explicit UnknownTypeError(const std::string & type)
  : std::runtime_error("Unknown type: " + type) {}
};

class InvalidEventError : public std::runtime_error
{
public:
  InvalidEventError()
  : std::runtime_error("event is invalid") {}
};

class EventNotRegisteredError : public std::runtime_error
{
public:
  EventNotRegisteredError()
  : std::runtime_error("event already registered") {}
};

class MissingGroupNodeException : public std::runtime_error
{
public:
  explicit MissingGroupNodeException(const std::string & obj_type)
  : std::runtime_error("cannot create: " + obj_type + " , callback group not in node") {}
};

class InvalidParametersException : public std::runtime_error
{
public:
  // Inherit constructors from runtime_error.
  using std::runtime_error::runtime_error;
};

class InvalidParameterValueException : public std::runtime_error
{
  // Inherit constructors from runtime_error.
  using std::runtime_error::runtime_error;
};


class InvalidParameterTypeException : public std::runtime_error
{
public:

  RCLCPP_PUBLIC
  InvalidParameterTypeException(const std::string & name, const std::string message)
  : std::runtime_error("parameter '" + name + "' has invalid type: " + message)
  {}
};


class UninitializedStaticallyTypedParameterException : public std::runtime_error
{
public:

  RCLCPP_PUBLIC
  explicit UninitializedStaticallyTypedParameterException(const std::string & name)
  : std::runtime_error("Statically typed parameter '" + name + "' must be initialized.")
  {}
};

class ParameterAlreadyDeclaredException : public std::runtime_error
{
  // Inherit constructors from runtime_error.
  using std::runtime_error::runtime_error;
};

class ParameterNotDeclaredException : public std::runtime_error
{
  // Inherit constructors from runtime_error.
  using std::runtime_error::runtime_error;
};

class ParameterImmutableException : public std::runtime_error
{
  // Inherit constructors from runtime_error.
  using std::runtime_error::runtime_error;
};

class ParameterModifiedInCallbackException : public std::runtime_error
{
  // Inherit constructors from runtime_error.
  using std::runtime_error::runtime_error;
};

class ParameterUninitializedException : public std::runtime_error
{
public:

  explicit ParameterUninitializedException(const std::string & name)
  : std::runtime_error("parameter '" + name + "' is not initialized")
  {}
};

class InvalidQosOverridesException : public std::runtime_error
{
  // Inherit constructors from runtime_error.
  using std::runtime_error::runtime_error;
};

class QoSCheckCompatibleException : public std::runtime_error
{
  // Inherit constructors from runtime_error.
  using std::runtime_error::runtime_error;
};

}  // namespace exceptions
}  // namespace rclcpp

#endif  // RCLCPP__EXCEPTIONS__EXCEPTIONS_HPP_