libdap Updated for version 3.20.11
libdap4 is an implementation of OPeNDAP's DAP protocol.
Error.h
1
2// -*- mode: c++; c-basic-offset:4 -*-
3
4// This file is part of libdap, A C++ implementation of the OPeNDAP Data
5// Access Protocol.
6
7// Copyright (c) 2002,2003 OPeNDAP, Inc.
8// Author: James Gallagher <jgallagher@opendap.org>
9//
10// This library is free software; you can redistribute it and/or
11// modify it under the terms of the GNU Lesser General Public
12// License as published by the Free Software Foundation; either
13// version 2.1 of the License, or (at your option) any later version.
14//
15// This library is distributed in the hope that it will be useful,
16// but WITHOUT ANY WARRANTY; without even the implied warranty of
17// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18// Lesser General Public License for more details.
19//
20// You should have received a copy of the GNU Lesser General Public
21// License along with this library; if not, write to the Free Software
22// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23//
24// You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.
25
26// (c) COPYRIGHT URI/MIT 1999,2000
27// Please read the full copyright statement in the file COPYRIGHT_URI.
28//
29// Authors:
30// jhrg,jimg James Gallagher <jgallagher@gso.uri.edu>
31
32// Interface for the Error class
33//
34// jhrg 4/23/96
35
36#ifndef _error_h
37#define _error_h
38
39#include <iostream>
40#include <string>
41#include <exception>
42
43#include <cstdio> // For FILE *
44#include <utility>
45
46using std::cout;
47using std::string;
48using std::ostream;
49
50namespace libdap
51{
52
58typedef int ErrorCode; //using standard errno+netCDF error codes from server
59
62#define undefined_error 1000
63#define unknown_error 1001
64#define internal_error 1002
65#define no_such_file 1003
66#define no_such_variable 1004
67#define malformed_expr 1005
68#define no_authorization 1006
69#define cannot_read_file 1007
70#define not_implemented 1008
71#define dummy_message 1009
73
93class Error : public std::exception
94{
95protected:
96 ErrorCode _error_code;
97 std::string _error_message;
98 std::string d_file;
99 int d_line{};
100
101public:
103 Error() : exception(), _error_code(undefined_error) {}
104
117 Error(ErrorCode ec, std::string msg, std::string file = "", int line = 0)
118 : exception(), _error_code(ec), _error_message(std::move(msg)), d_file(std::move(file)), d_line(line)
119 {}
120
129 explicit Error(std::string msg, std::string file = "" , int line = 0)
130 : exception(), _error_code(unknown_error), _error_message(std::move(msg)), d_file(std::move(file)), d_line(line)
131 {}
132
133 Error(const Error &copy_from) noexcept
134 : exception(), _error_code(copy_from._error_code), _error_message(copy_from._error_message)
135 {}
136
137 ~Error() override = default;
138
139 Error &operator=(const Error &rhs);
140
141 bool OK() const;
142 bool parse(FILE *fp);
143 void print(FILE *out) const;
144 void print(std::ostream &out) const;
146 std::string get_error_message() const;
147 void set_error_code(ErrorCode ec = undefined_error);
148 void set_error_message(std::string msg = "");
149
150 std::string get_file() const { return d_file; }
151 void set_file(std::string f) { d_file = std::move(f); }
152 int get_line() const { return d_line; }
153 void set_line(int l) { d_line = l; }
154
156 const char* what() const noexcept override {
157 return _error_message.c_str();
158 }
159};
160
161} // namespace libdap
162
163#endif // _error_h
A class for error processing.
Definition: Error.h:94
Error(ErrorCode ec, std::string msg, std::string file="", int line=0)
Definition: Error.h:117
void set_error_message(std::string msg="")
Definition: Error.cc:252
void set_error_code(ErrorCode ec=undefined_error)
Definition: Error.cc:227
void print(FILE *out) const
Definition: Error.cc:165
const char * what() const noexcept override
The pointer is valid only for the lifetime of the Error instance. jhrg 9/22/20.
Definition: Error.h:156
ErrorCode get_error_code() const
Definition: Error.cc:214
std::string get_error_message() const
Definition: Error.cc:243
Error(std::string msg, std::string file="", int line=0)
Definition: Error.h:129
bool parse(FILE *fp)
Parse an Error object.
Definition: Error.cc:123
bool OK() const
Is the Error object valid?
Definition: Error.cc:100
top level DAP object to house generic methods
Definition: AlarmHandler.h:36
int ErrorCode
An enumerated type for common errors.
Definition: Error.h:58