~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/error.cc

  • Committer: Andrew Hutchings
  • Date: 2011-02-01 10:23:22 UTC
  • mto: (2136.1.1 build)
  • mto: This revision was merged to the branch mainline in revision 2137.
  • Revision ID: andrew@linuxjedi.co.uk-20110201102322-oxztcyrjzg3c7yta
Fix counters cleanup

Show diffs side-by-side

added added

removed removed

Lines of Context:
22
22
 * Errors a drizzled can give you
23
23
 */
24
24
 
25
 
#include <config.h>
26
 
#include <drizzled/internal/my_sys.h>
27
 
#include <drizzled/definitions.h>
28
 
#include <drizzled/error.h>
29
 
#include <drizzled/gettext.h>
30
 
#include <drizzled/identifier.h>
31
 
#include <drizzled/util/find_ptr.h>
 
25
#include "config.h"
 
26
#include "drizzled/internal/my_sys.h"
 
27
#include "drizzled/definitions.h"
 
28
#include "drizzled/error.h"
 
29
#include "drizzled/gettext.h"
 
30
 
 
31
#include "drizzled/identifier.h"
32
32
 
33
33
#include <boost/unordered_map.hpp>
34
 
 
35
 
namespace drizzled {
36
 
namespace {
 
34
#include <exception>
 
35
 
 
36
namespace drizzled
 
37
{
 
38
namespace
 
39
{
 
40
 
 
41
class ErrorStringNotFound: public std::exception
 
42
{
 
43
public:
 
44
  ErrorStringNotFound()
 
45
  {}
 
46
};
37
47
 
38
48
ErrorMap& get_error_map()
39
49
{
48
58
  return get_error_map().mapping_;
49
59
}
50
60
 
51
 
void add_error_message(error_t error_code,
 
61
void add_error_message(drizzled::error_t error_code,
52
62
                       const std::string &error_name,
53
63
                       const std::string &message)
54
64
{
55
65
  get_error_map().add(error_code, error_name, message);
56
66
}
57
67
 
58
 
const char* error_message(error_t code)
 
68
const char * error_message(drizzled::error_t code)
59
69
{
60
 
        const std::string* ptr = get_error_map().find(code);
61
 
        return ptr ? ptr->c_str() : get_error_map().find(ER_UNKNOWN_ERROR)->c_str();
 
70
  try
 
71
  {
 
72
    return get_error_map().find(code).c_str();
 
73
  }
 
74
  catch (ErrorStringNotFound const& e)
 
75
  {
 
76
    return get_error_map().find(ER_UNKNOWN_ERROR).c_str();
 
77
  }
62
78
}
63
79
 
64
80
error_handler_func error_handler_hook= NULL;
65
81
 
66
 
namespace error {
67
 
 
68
 
void access(const drizzled::identifier::User& user)
69
 
{
70
 
  my_error(ER_ACCESS_DENIED_ERROR, MYF(0), user.getSQLPath().c_str(), ER(user.hasPassword() ? ER_YES : ER_NO));
71
 
72
 
 
73
 
void access(const drizzled::identifier::User& user, const drizzled::identifier::Schema& schema)
74
 
{
75
 
  my_error(ER_DBACCESS_DENIED_ERROR, MYF(0), user.getSQLPath().c_str(), schema.getSQLPath().c_str());
76
 
77
 
 
78
 
void access(const drizzled::identifier::User& user, const drizzled::identifier::Table& table)
79
 
{
80
 
  my_error(ER_TABLEACCESS_DENIED_ERROR, MYF(0), user.getSQLPath().c_str(), table.getSQLPath().c_str());
81
 
82
 
 
83
 
static error::level_t _verbosity= error::ERROR;
84
 
static std::string _verbosity_strint;
85
 
 
86
 
const std::string &verbose_string()
87
 
{
88
 
  switch (_verbosity)
89
 
  {
90
 
  case error::INSPECT:
91
 
    {
92
 
      static std::string _arg= "INSPECT";
93
 
      return _arg;
94
 
    }
95
 
  case error::INFO:
96
 
    {
97
 
      static std::string _arg= "INFO";
98
 
      return _arg;
99
 
    }
100
 
  case error::WARN:
101
 
    {
102
 
      static std::string _arg= "WARN";
103
 
      return _arg;
104
 
    }
105
 
  case error::ERROR:
106
 
    {
107
 
      static std::string _arg= "ERROR";
108
 
      return _arg;
109
 
    }
110
 
  }
111
 
 
112
 
  abort();
113
 
}
114
 
 
115
 
error::level_t &verbosity()
116
 
{
117
 
  return _verbosity;
118
 
}
119
 
 
120
 
void check_verbosity(const std::string &arg)
121
 
{
122
 
  if (not arg.compare("INSPECT"))
123
 
  {
124
 
    _verbosity= error::INSPECT;
125
 
  }
126
 
  else if (not arg.compare("INFO"))
127
 
  {
128
 
    _verbosity= error::INFO;
129
 
  }
130
 
  else if (not arg.compare("WARN"))
131
 
  {
132
 
    _verbosity= error::WARN;
133
 
  }
134
 
  else if (not arg.compare("ERROR"))
135
 
  {
136
 
    _verbosity= error::ERROR;
137
 
  }
138
 
}
139
 
 
140
 
} // namespace error
141
 
 
142
82
/*
143
83
  WARNING!
144
84
  my_error family functions have to be used according following rules:
163
103
  my_error(nr, MyFlags, ref.c_str());
164
104
165
105
 
166
 
void my_error(error_t nr, const drizzled::Identifier& id, myf MyFlags)
 
106
void my_error(error_t nr, drizzled::Identifier::const_reference ref, myf MyFlags)
167
107
{
168
 
  my_error(nr, MyFlags, id.getSQLPath().c_str());
 
108
  std::string temp;
 
109
  ref.getSQLPath(temp);
 
110
  my_error(nr, MyFlags, temp.c_str());
169
111
170
112
 
171
113
void my_error(error_t nr)
175
117
 
176
118
void my_error(error_t nr, myf MyFlags, ...)
177
119
{
 
120
  std::string format;
178
121
  va_list args;
179
122
  char ebuff[ERRMSGSIZE + 20];
180
123
 
181
 
  if (const std::string* format= get_error_map().find(nr))
182
 
        {       
 
124
  try
 
125
  {
 
126
    format= get_error_map().find(nr);
183
127
    va_start(args,MyFlags);
184
 
    (void) vsnprintf (ebuff, sizeof(ebuff), _(format->c_str()), args);
 
128
    (void) vsnprintf (ebuff, sizeof(ebuff), _(format.c_str()), args);
185
129
    va_end(args);
186
130
  }
187
 
        else
 
131
  catch (ErrorStringNotFound const& e)
 
132
  {
188
133
    (void) snprintf (ebuff, sizeof(ebuff), _("Unknown error %d"), nr);
 
134
  }
189
135
  (*error_handler_hook)(nr, ebuff, MyFlags);
190
136
}
191
137
 
221
167
      MyFlags   Flags
222
168
*/
223
169
 
224
 
void my_message(drizzled::error_t error, const char *str, myf MyFlags)
 
170
void my_message(drizzled::error_t error, const char *str, register myf MyFlags)
225
171
{
226
172
  (*error_handler_hook)(error, str, MyFlags);
227
173
}
233
179
                   const std::string &error_name,
234
180
                   const std::string &message)
235
181
{
236
 
  if (not mapping_.count(error_num))
 
182
  if (mapping_.find(error_num) == mapping_.end())
237
183
  {
238
184
    // Log the error.
239
185
    mapping_[error_num]= ErrorMap::value_type(error_name, message);
244
190
  }
245
191
}
246
192
 
247
 
const std::string* ErrorMap::find(drizzled::error_t error_num) const
 
193
const std::string &ErrorMap::find(drizzled::error_t error_num) const
248
194
{
249
 
  const ErrorMessageMap::mapped_type* pos= find_ptr(mapping_, error_num);
250
 
        return pos ? &pos->second : NULL;
 
195
  ErrorMessageMap::const_iterator pos= mapping_.find(error_num);
 
196
  if (pos == mapping_.end())
 
197
  {
 
198
    throw ErrorStringNotFound();
 
199
  }
 
200
  return pos->second.second;
251
201
}
252
202
 
253
203
#define ADD_ERROR_MESSAGE(code, msg) add(code, STRINGIFY_ARG(code), msg)
296
246
  ADD_ERROR_MESSAGE(ER_OUT_OF_RESOURCES, N_("Out of memory; check if drizzled or some other process uses all available memory; if not, you may have to use 'ulimit' to allow drizzled to use more memory or you can add more swap space"));
297
247
  ADD_ERROR_MESSAGE(ER_BAD_HOST_ERROR, N_("Can't get hostname for your address"));
298
248
  ADD_ERROR_MESSAGE(ER_HANDSHAKE_ERROR, N_("Bad handshake"));
299
 
 
300
 
  // Access error messages
301
 
  ADD_ERROR_MESSAGE(ER_DBACCESS_DENIED_ERROR, N_("Access denied for user '%s' to schema '%s'"));
302
 
  ADD_ERROR_MESSAGE(ER_TABLEACCESS_DENIED_ERROR, N_("Access denied for user '%s' to table '%s'"));
303
 
  ADD_ERROR_MESSAGE(ER_ACCESS_DENIED_ERROR, N_("Access denied for user '%s' (using password: %s)"));
304
 
 
 
249
  ADD_ERROR_MESSAGE(ER_DBACCESS_DENIED_ERROR, N_("Access denied for user '%-.48s'@'%-.64s' to schema '%-.192s'"));
 
250
  ADD_ERROR_MESSAGE(ER_ACCESS_DENIED_ERROR, N_("Access denied for user '%-.48s'@'%-.64s' (using password: %s)"));
305
251
  ADD_ERROR_MESSAGE(ER_NO_DB_ERROR, N_("No schema selected"));
306
252
  ADD_ERROR_MESSAGE(ER_UNKNOWN_COM_ERROR, N_("Unknown command"));
307
253
  ADD_ERROR_MESSAGE(ER_BAD_NULL_ERROR, N_("Column '%-.192s' cannot be null"));
384
330
  ADD_ERROR_MESSAGE(ER_WRONG_VALUE_COUNT_ON_ROW, N_("Column count doesn't match value count at row %ld"));
385
331
  ADD_ERROR_MESSAGE(ER_CANT_REOPEN_TABLE, N_("Can't reopen table: '%-.192s'"));
386
332
  ADD_ERROR_MESSAGE(ER_MIX_OF_GROUP_FUNC_AND_FIELDS, N_("Mixing of GROUP columns (MIN(),MAX(),COUNT(),...) with no GROUP columns is illegal if there is no GROUP BY clause"));
 
333
  ADD_ERROR_MESSAGE(ER_NO_SUCH_TABLE, N_("Table '%-.192s.%-.192s' doesn't exist"));
387
334
  ADD_ERROR_MESSAGE(ER_SYNTAX_ERROR, N_("You have an error in your SQL syntax; check the manual that corresponds to your Drizzle server version for the right syntax to use"));
388
335
  ADD_ERROR_MESSAGE(ER_NET_PACKET_TOO_LARGE, N_("Got a packet bigger than 'max_allowed_packet' bytes"));
389
336
  ADD_ERROR_MESSAGE(ER_NET_PACKETS_OUT_OF_ORDER, N_("Got packets out of order"));
557
504
  ADD_ERROR_MESSAGE(ER_USE_SQL_BIG_RESULT, N_("Temporary table too large, rerun with SQL_BIG_RESULT."));
558
505
  ADD_ERROR_MESSAGE(ER_UNKNOWN_ENGINE_OPTION, N_("Unknown table engine option key/pair %s = %s."));
559
506
  ADD_ERROR_MESSAGE(ER_UNKNOWN_SCHEMA_OPTION, N_("Unknown schema engine option key/pair %s = %s."));
560
 
  ADD_ERROR_MESSAGE(ER_CARTESIAN_JOIN_ATTEMPTED, N_("Implicit cartesian join attempted."));
 
507
 
561
508
  ADD_ERROR_MESSAGE(ER_ADMIN_ACCESS, N_("Admin access not allowed from this username/IP address."));
562
509
 
563
510
  // User lock/barrier error messages
609
556
  ADD_ERROR_MESSAGE(ER_ASSERT_NULL, N_("Assertion '%s' failed, the result was NULL."));
610
557
 
611
558
  // Some old error values use the same strings as some new error values.
612
 
  ADD_ERROR_MESSAGE(EE_FILENOTFOUND, *find(ER_FILE_NOT_FOUND));
613
 
  ADD_ERROR_MESSAGE(EE_CANTCREATEFILE, *find(ER_CANT_CREATE_FILE));
614
 
  ADD_ERROR_MESSAGE(EE_READ, *find(ER_ERROR_ON_READ));
615
 
  ADD_ERROR_MESSAGE(EE_WRITE, *find(ER_ERROR_ON_WRITE));
616
 
  ADD_ERROR_MESSAGE(EE_BADCLOSE, *find(ER_ERROR_ON_CLOSE));
617
 
  ADD_ERROR_MESSAGE(EE_OUTOFMEMORY, *find(ER_OUTOFMEMORY));
618
 
  ADD_ERROR_MESSAGE(EE_DELETE, *find(ER_CANT_DELETE_FILE));
619
 
  ADD_ERROR_MESSAGE(EE_LINK, *find(ER_ERROR_ON_RENAME));
620
 
  ADD_ERROR_MESSAGE(EE_EOFERR, *find(ER_UNEXPECTED_EOF));
621
 
  ADD_ERROR_MESSAGE(EE_CANTLOCK, *find(ER_CANT_LOCK));
622
 
  ADD_ERROR_MESSAGE(EE_DIR, *find(ER_CANT_READ_DIR));
623
 
  ADD_ERROR_MESSAGE(EE_STAT, *find(ER_CANT_GET_STAT));
624
 
  ADD_ERROR_MESSAGE(EE_DISK_FULL, *find(ER_DISK_FULL));
 
559
  ADD_ERROR_MESSAGE(EE_FILENOTFOUND, find(ER_FILE_NOT_FOUND));
 
560
  ADD_ERROR_MESSAGE(EE_CANTCREATEFILE, find(ER_CANT_CREATE_FILE));
 
561
  ADD_ERROR_MESSAGE(EE_READ, find(ER_ERROR_ON_READ));
 
562
  ADD_ERROR_MESSAGE(EE_WRITE, find(ER_ERROR_ON_WRITE));
 
563
  ADD_ERROR_MESSAGE(EE_BADCLOSE, find(ER_ERROR_ON_CLOSE));
 
564
  ADD_ERROR_MESSAGE(EE_OUTOFMEMORY, find(ER_OUTOFMEMORY));
 
565
  ADD_ERROR_MESSAGE(EE_DELETE, find(ER_CANT_DELETE_FILE));
 
566
  ADD_ERROR_MESSAGE(EE_LINK, find(ER_ERROR_ON_RENAME));
 
567
  ADD_ERROR_MESSAGE(EE_EOFERR, find(ER_UNEXPECTED_EOF));
 
568
  ADD_ERROR_MESSAGE(EE_CANTLOCK, find(ER_CANT_LOCK));
 
569
  ADD_ERROR_MESSAGE(EE_DIR, find(ER_CANT_READ_DIR));
 
570
  ADD_ERROR_MESSAGE(EE_STAT, find(ER_CANT_GET_STAT));
 
571
  ADD_ERROR_MESSAGE(EE_DISK_FULL, find(ER_DISK_FULL));
625
572
  
626
573
  // Catalog related errors
627
574
  ADD_ERROR_MESSAGE(ER_CATALOG_CANNOT_CREATE, N_("Cannot create catalog '%s'."));
633
580
  ADD_ERROR_MESSAGE(ER_CATALOG_NO_LOCK, N_("Could not gain lock on '%s'."));
634
581
  ADD_ERROR_MESSAGE(ER_CORRUPT_CATALOG_DEFINITION, N_("Corrupt or invalid catalog definition for '%s' : '%s'."));
635
582
  ADD_ERROR_MESSAGE(ER_WRONG_NAME_FOR_CATALOG, N_("Invalid catalog name."));
636
 
  ADD_ERROR_MESSAGE(ER_USE_DATA_DICTIONARY, N_("Engine status is now stored in the data_dictionary tables, please use these instead."));
637
 
  ADD_ERROR_MESSAGE(ER_TRANSACTION_ALREADY_STARTED, N_("There is already a transaction in progress"));
638
 
  ADD_ERROR_MESSAGE(ER_NO_LOCK_HELD, N_("No lock is held by this connection."));
 
583
 
 
584
 
639
585
}
640
586
 
641
587
} /* namespace drizzled */