DCCL v4
exception.h
1 // Copyright 2009-2023:
2 // GobySoft, LLC (2013-)
3 // Massachusetts Institute of Technology (2007-2014)
4 // Community contributors (see AUTHORS file)
5 // File authors:
6 // Toby Schneider <toby@gobysoft.org>
7 //
8 //
9 // This file is part of the Dynamic Compact Control Language Library
10 // ("DCCL").
11 //
12 // DCCL is free software: you can redistribute it and/or modify
13 // it under the terms of the GNU Lesser General Public License as published by
14 // the Free Software Foundation, either version 2.1 of the License, or
15 // (at your option) any later version.
16 //
17 // DCCL is distributed in the hope that it will be useful,
18 // but WITHOUT ANY WARRANTY; without even the implied warranty of
19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 // GNU Lesser General Public License for more details.
21 //
22 // You should have received a copy of the GNU Lesser General Public License
23 // along with DCCL. If not, see <http://www.gnu.org/licenses/>.
24 #ifndef Exception20100812H
25 #define Exception20100812H
26 
27 #include <google/protobuf/descriptor.h>
28 #include <stdexcept>
29 
30 namespace dccl
31 {
32 inline std::string exception_string(const std::string& in, const google::protobuf::Descriptor* desc,
33  const google::protobuf::FieldDescriptor* field)
34 {
35  std::string out;
36  if (desc)
37  out += std::string("Message: ") + desc->full_name() + ": ";
38  if (field)
39  out += std::string("Field: ") + field->full_name() + ": ";
40 
41  out += in;
42  return out;
43 }
44 
46 class Exception : public std::runtime_error
47 {
48  public:
49  Exception(const std::string& s, const google::protobuf::Descriptor* desc = nullptr)
50  : std::runtime_error(exception_string(s, desc, nullptr)), desc_(desc)
51  {
52  }
53 
54  const google::protobuf::Descriptor* desc() const { return desc_; }
55 
56  private:
57  const google::protobuf::Descriptor* desc_;
58 };
59 
62 {
63  public:
64  NullValueException() : Exception(exception_string("NULL value", nullptr, nullptr)) {}
65 };
66 
67 class OutOfRangeException : public std::out_of_range
68 {
69  public:
70  OutOfRangeException(const std::string& s, const google::protobuf::FieldDescriptor* field,
71  const google::protobuf::Descriptor* desc = nullptr)
72  : std::out_of_range(exception_string(s, desc, field)), field_(field), desc_(desc)
73  {
74  }
75 
76  const google::protobuf::FieldDescriptor* field() const { return field_; }
77  const google::protobuf::Descriptor* desc() const { return desc_; }
78 
79  private:
80  const google::protobuf::FieldDescriptor* field_;
81  const google::protobuf::Descriptor* desc_;
82 };
83 } // namespace dccl
84 
85 #endif
dccl
Dynamic Compact Control Language namespace.
Definition: any.h:46
dccl::Exception
Exception class for DCCL.
Definition: exception.h:46
dccl::NullValueException
Exception used to signal null (non-existent) value within field codecs during decode.
Definition: exception.h:61
dccl::OutOfRangeException
Definition: exception.h:67