libdap Updated for version 3.20.11
libdap4 is an implementation of OPeNDAP's DAP protocol.
Error.cc
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
35#include "config.h"
36
37#include <cstdio>
38#include <cassert>
39#include <utility>
40
41#include "Error.h"
42#include "parser.h"
43#include "InternalErr.h"
44#include "debug.h"
45
46using namespace std;
47
48// Glue routines declared in Error.lex
49extern void Error_switch_to_buffer(void *new_buffer);
50extern void Error_delete_buffer(void * buffer);
51extern void *Error_buffer(FILE *fp);
52
53//extern void Errorrestart(FILE *yyin); // defined in Error.tab.c
54extern int Errorparse(libdap::parser_arg *arg);
55
56namespace libdap {
57
58// There are two entries for 'cannot read file' because of an error made
59// when the message was first added to this class.
60static const char *err_messages[] = {
61 "Undefined error",
62 "Unknown error",
63 "Internal error",
64 "No such file",
65 "No such variable",
66 "Malformed expression",
67 "No authorization",
68 "Cannot read file",
69 "Not Implemented",
70 ""
71};
72
73Error &
74Error::operator=(const Error &rhs)
75{
76 assert(OK());
77
78 if (&rhs == this) // are they identical?
79 return *this;
80 else {
81 _error_code = rhs._error_code;
82 _error_message = rhs._error_message;
83
84 d_file = rhs.d_file;
85 d_line = rhs.d_line;
86
87 assert(this->OK());
88
89 return *this;
90 }
91}
92
99bool
101{
102 // The object is empty - users cannot make these, but this class can!
103 bool empty = ((_error_code == undefined_error)
104 && (_error_message.empty()));
105
106 // Just a message - the program part is null.
107 bool message = ((_error_code != undefined_error)
108 && (!_error_message.empty()));
109
110 DBG(cerr << "empty: " << empty << ", message: " << message << endl);
111 return empty || message;
112}
113
122bool
124{
125 if (!fp)
126 throw InternalErr(__FILE__, __LINE__, "Null input stream");
127
128 void *buffer = Error_buffer(fp);
129 Error_switch_to_buffer(buffer);
130
131 parser_arg arg(this);
132
133 bool status;
134 try {
135 status = Errorparse(&arg) == 0;
136 Error_delete_buffer(buffer);
137 }
138 catch (Error &e) {
139 Error_delete_buffer(buffer);
140 throw InternalErr(__FILE__, __LINE__, e.get_error_message());
141 }
142
143 // STATUS is the result of the parser function; if a recoverable error
144 // was found it will be true but arg.status() will be false.
145 // I'm throwing an InternalErr here since Error objects are generated by
146 // the core; they should always parse! 9/21/2000 jhrg
147 if (!status || !arg.status())
148 throw InternalErr(__FILE__, __LINE__, "Error parsing error object!");
149 else
150 return OK(); // Check object consistency
151}
152
153
164void
165Error::print(FILE *out) const
166{
167 assert(OK());
168
169 fprintf(out, "Error {\n") ;
170
171 fprintf(out, " code = %d;\n", static_cast<int>(_error_code)) ;
172
173 // If the error message is wrapped in double quotes, print it, else, add
174 // wrapping double quotes.
175 if (*_error_message.begin() == '"' && *(_error_message.end() - 1) == '"')
176 fprintf(out, " message = %s;\n", _error_message.c_str()) ;
177 else
178 fprintf(out, " message = \"%s\";\n", _error_message.c_str()) ;
179
180 fprintf(out, "};\n") ;
181}
182
193void
194Error::print(ostream &strm) const
195{
196 assert(OK());
197
198 strm << "Error {\n" ;
199
200 strm << " code = " << static_cast<int>(_error_code) << ";\n" ;
201
202 // If the error message is wrapped in double quotes, print it, else, add
203 // wrapping double quotes.
204 if (*_error_message.begin() == '"' && *(_error_message.end() - 1) == '"')
205 strm << " message = " << _error_message.c_str() << ";\n" ;
206 else
207 strm << " message = \"" << _error_message.c_str() << "\";\n" ;
208
209 strm << "};\n" ;
210}
211
215{
216 assert(OK());
217 return _error_code;
218}
219
226void
228{
229 _error_code = ec;
230 // Added check to make sure that err_messages is not accessed beyond its
231 // bounds. 02/02/04 jhrg
232 if (_error_message.empty()
233 && ec > undefined_error && ec <= cannot_read_file) {
234 _error_message = err_messages[ec - undefined_error];
235 }
236 else {
237 _error_message = err_messages[0];
238 }
239}
240
242string
244{
245 assert(OK());
246
247 return {_error_message};
248}
249
251void
253{
254 _error_message = std::move(msg);
255}
256
257} // namespace libdap
A class for error processing.
Definition: Error.h:94
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
ErrorCode get_error_code() const
Definition: Error.cc:214
std::string get_error_message() const
Definition: Error.cc:243
bool parse(FILE *fp)
Parse an Error object.
Definition: Error.cc:123
bool OK() const
Is the Error object valid?
Definition: Error.cc:100
A class for software fault reporting.
Definition: InternalErr.h:65
top level DAP object to house generic methods
Definition: AlarmHandler.h:36
int ErrorCode
An enumerated type for common errors.
Definition: Error.h:58
Pass parameters by reference to a parser.
Definition: parser.h:69