datastax/python-driver — pull request #25 by Kami (2013-09-11 20:40:39+00:00)
Title: Throw a better exception which user binds PreparedStatement with an argument of an invalid type

## Problem

If a user tries to bind `PreparedStatement` with a value of an invalid type, no special checking is done which means struck.pack exception is propagated to the user.

```bash
Traceback (most recent call last):
File "/w/python-driver/tests/unit/test_parameter_binding.py", line 65, in test_invalid_argument_type
bound_statement.bind(values)
File "/w/python-driver/cassandra/query.py", line 209, in bind
self.values.append(col_type.serialize(value))
error: cannot convert argument to integer
```

This exception is unfriendly and makes it harder and more time consuming to debug those issues.

## Proposed approach / solution

I've attached a patch (with corresponding test cases) which makes debugging easier by throwing a more friendly exception. Exception which is thrown is called `InvalidParameterTypeError` and contains the following attributes:

* `col_name`
* `expected_type`
* `actual_type`

With this new Exception type, the error now looks like this:

```bash
Traceback (most recent call last):
File "/w/python-driver/tests/unit/test_parameter_binding.py", line 65, in test_invalid_argument_type
bound_statement.bind(values)
File "/w/python-driver/cassandra/query.py", line 220, in bind
raise err
InvalidParameterTypeError: Received an argument of invalid type for column "foo1". Expected: , Got:
```

Friendlier error message plus additional arguments make debugging easier.

This pull request also contains `__repr__` change which also make debugging easier. If you think this repr change should be part of a separate pull request, let me know and I'll create a separate pull request for it.