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