raise meaningful error messages

This commit is contained in:
David Rose 2004-05-19 20:57:52 +00:00
parent 14765e6fc4
commit ebc3f08805
2 changed files with 29 additions and 14 deletions

View File

@ -321,9 +321,11 @@ void DCClass::
pack_required_field(Datagram &dg, PyObject *distobj, DCField *field) const { pack_required_field(Datagram &dg, PyObject *distobj, DCField *field) const {
DCAtomicField *atom = field->as_atomic_field(); DCAtomicField *atom = field->as_atomic_field();
if (atom == (DCAtomicField *)NULL) { if (atom == (DCAtomicField *)NULL) {
cerr << "Cannot pack non-atomic field " << field->get_name() ostringstream strm;
<< " for generate\n"; strm << "Cannot pack non-atomic field " << field->get_name()
nassertv(false); << " for generate";
nassert_raise(strm.str());
return;
} }
// We need to get the initial value of this field. There isn't a // We need to get the initial value of this field. There isn't a
@ -335,15 +337,18 @@ pack_required_field(Datagram &dg, PyObject *distobj, DCField *field) const {
if (atom->get_num_elements() == 0) { if (atom->get_num_elements() == 0) {
// It sure doesn't make sense to have a required field with no // It sure doesn't make sense to have a required field with no
// parameters. What data, exactly, is required? // parameters. What data, exactly, is required?
cerr << "Required field " << set_name << " has no parameters!\n"; ostringstream strm;
nassertv(false); strm << "Required field " << set_name << " has no parameters!";
nassert_raise(strm.str());
return;
} }
if (set_name.substr(0, 3) != "set") { if (set_name.substr(0, 3) != "set") {
// This is required to suit our set/get mangling convention. // This is required to suit our set/get mangling convention.
cerr << "Required field " << set_name ostringstream strm;
<< " does not begin with 'set'\n"; strm << "Required field " << set_name << " does not begin with 'set'";
nassertv(false); nassert_raise(strm.str());
return;
} }
string get_name = set_name; string get_name = set_name;
get_name[0] = 'g'; get_name[0] = 'g';
@ -351,9 +356,11 @@ pack_required_field(Datagram &dg, PyObject *distobj, DCField *field) const {
// Now we have to look up the getter on the distributed object // Now we have to look up the getter on the distributed object
// and call it. // and call it.
if (!PyObject_HasAttrString(distobj, (char *)get_name.c_str())) { if (!PyObject_HasAttrString(distobj, (char *)get_name.c_str())) {
cerr << "Required field " << set_name ostringstream strm;
<< " doesn't have matching field named " << get_name << "\n"; strm << "Required field " << set_name
nassertv(false); << " doesn't have matching field named " << get_name;
nassert_raise(strm.str());
return;
} }
PyObject *func = PyObject *func =
PyObject_GetAttrString(distobj, (char *)get_name.c_str()); PyObject_GetAttrString(distobj, (char *)get_name.c_str());
@ -471,9 +478,11 @@ ai_format_generate(PyObject *distobj, int do_id,
DCField *field = get_field_by_name(field_name); DCField *field = get_field_by_name(field_name);
if (field == (DCField *)NULL) { if (field == (DCField *)NULL) {
cerr << "No field named " << field_name << " in class " << get_name() ostringstream strm;
strm << "No field named " << field_name << " in class " << get_name()
<< "\n"; << "\n";
nassertr(false, Datagram()); nassert_raise(strm.str());
return Datagram();
} }
pack_required_field(dg, distobj, field); pack_required_field(dg, distobj, field);

View File

@ -19,6 +19,7 @@
#include "dcField.h" #include "dcField.h"
#include "hashGenerator.h" #include "hashGenerator.h"
#include "dcmsgtypes.h" #include "dcmsgtypes.h"
#include "notify.h"
//////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////
// Function: DCField::get_number // Function: DCField::get_number
@ -79,7 +80,12 @@ pack_args(Datagram &datagram, PyObject *tuple) const {
nassertv(PySequence_Check(tuple)); nassertv(PySequence_Check(tuple));
int index = 0; int index = 0;
bool enough_args = do_pack_args(datagram, tuple, index); bool enough_args = do_pack_args(datagram, tuple, index);
nassertv(enough_args && index == PySequence_Size(tuple)); if (!enough_args || index != PySequence_Size(tuple)) {
ostringstream strm;
strm << "Wrong number of arguments to field " << get_name();
nassert_raise(strm.str());
return;
}
} }
#endif // HAVE_PYTHON #endif // HAVE_PYTHON