libdap Updated for version 3.21.1
libdap4 is an implementation of OPeNDAP's DAP protocol.
Error.cc
Go to the documentation of this file.
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 1994-1999
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// Implementation for the Error class.
33
34#include "config.h"
35
36#include <cassert>
37#include <cstdio>
38#include <utility>
39
40#include "Error.h"
41#include "InternalErr.h"
42#include "debug.h"
43#include "parser.h"
44
45using namespace std;
46
47// Glue routines declared in Error.lex
48extern void Error_switch_to_buffer(void *new_buffer);
49extern void Error_delete_buffer(void *buffer);
50extern void *Error_buffer(FILE *fp);
51
52// extern void Errorrestart(FILE *yyin); // defined in Error.tab.c
54
55namespace libdap {
56
57// There are two entries for 'cannot read file' because of an error made
58// when the message was first added to this class.
59static const char *err_messages[] = {
60 "Undefined error", "Unknown error", "Internal error", "No such file", "No such variable",
61 "Malformed expression", "No authorization", "Cannot read file", "Not Implemented", ""};
62
64 assert(OK());
65
66 if (&rhs == this) // are they identical?
67 return *this;
68 else {
71
72 d_file = rhs.d_file;
73 d_line = rhs.d_line;
74
75 assert(this->OK());
76
77 return *this;
78 }
79}
80
87bool Error::OK() const {
88 // The object is empty - users cannot make these, but this class can!
89 bool empty = ((_error_code == undefined_error) && (_error_message.empty()));
90
91 // Just a message - the program part is null.
92 bool message = ((_error_code != undefined_error) && (!_error_message.empty()));
93
94 DBG(cerr << "empty: " << empty << ", message: " << message << endl);
95 return empty || message;
96}
97
106bool Error::parse(FILE *fp) {
107 if (!fp)
108 throw InternalErr(__FILE__, __LINE__, "Null input stream");
109
110 void *buffer = Error_buffer(fp);
112
113 parser_arg arg(this);
114
115 bool status;
116 try {
117 status = Errorparse(&arg) == 0;
118 Error_delete_buffer(buffer);
119 } catch (Error &e) {
120 Error_delete_buffer(buffer);
121 throw InternalErr(__FILE__, __LINE__, e.get_error_message());
122 }
123
124 // STATUS is the result of the parser function; if a recoverable error
125 // was found it will be true but arg.status() will be false.
126 // I'm throwing an InternalErr here since Error objects are generated by
127 // the core; they should always parse! 9/21/2000 jhrg
128 if (!status || !arg.status())
129 throw InternalErr(__FILE__, __LINE__, "Error parsing error object!");
130 else
131 return OK(); // Check object consistency
132}
133
144void Error::print(FILE *out) const {
145 assert(OK());
146
147 fprintf(out, "Error {\n");
148
149 fprintf(out, " code = %d;\n", static_cast<int>(_error_code));
150
151 // If the error message is wrapped in double quotes, print it, else, add
152 // wrapping double quotes.
153 if (*_error_message.begin() == '"' && *(_error_message.end() - 1) == '"')
154 fprintf(out, " message = %s;\n", _error_message.c_str());
155 else
156 fprintf(out, " message = \"%s\";\n", _error_message.c_str());
157
158 fprintf(out, "};\n");
159}
160
171void Error::print(ostream &strm) const {
172 assert(OK());
173
174 strm << "Error {\n";
175
176 strm << " code = " << static_cast<int>(_error_code) << ";\n";
177
178 // If the error message is wrapped in double quotes, print it, else, add
179 // wrapping double quotes.
180 if (*_error_message.begin() == '"' && *(_error_message.end() - 1) == '"')
181 strm << " message = " << _error_message.c_str() << ";\n";
182 else
183 strm << " message = \"" << _error_message.c_str() << "\";\n";
184
185 strm << "};\n";
186}
187
190 assert(OK());
191 return _error_code;
192}
193
201 _error_code = ec;
202 // Added check to make sure that err_messages is not accessed beyond its
203 // bounds. 02/02/04 jhrg
204 if (_error_message.empty() && ec > undefined_error && ec <= cannot_read_file) {
205 _error_message = err_messages[ec - undefined_error];
206 } else {
207 _error_message = err_messages[0];
208 }
209}
210
213 assert(OK());
214
215 return {_error_message};
216}
217
219void Error::set_error_message(string msg) { _error_message = std::move(msg); }
220
221} // namespace libdap
int Errorparse(libdap::parser_arg *arg)
void Error_delete_buffer(void *buffer)
void Error_switch_to_buffer(void *new_buffer)
void * Error_buffer(FILE *fp)
#define undefined_error
Undefined error code, an empty Error object was built.
Definition Error.h:61
#define cannot_read_file
(400)
Definition Error.h:68
ErrorCode _error_code
Definition Error.h:94
Error & operator=(const Error &rhs)
Definition Error.cc:63
void set_error_message(std::string msg="")
Definition Error.cc:219
void set_error_code(ErrorCode ec=undefined_error)
Definition Error.cc:200
void print(FILE *out) const
Definition Error.cc:144
std::string _error_message
Definition Error.h:95
ErrorCode get_error_code() const
Definition Error.cc:189
std::string get_error_message() const
Definition Error.cc:212
bool parse(FILE *fp)
Parse an Error object.
Definition Error.cc:106
std::string d_file
Definition Error.h:96
bool OK() const
Is the Error object valid?
Definition Error.cc:87
int d_line
Definition Error.h:97
A class for software fault reporting.
Definition InternalErr.h:61
#define DBG(x)
Definition debug.h:58
top level DAP object to house generic methods
Definition AISConnect.cc:30
int ErrorCode
An enumerated type for common errors.
Definition Error.h:57
Pass parameters by reference to a parser.
Definition parser.h:67