Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sqlite_exception ... access to error code? #80

Closed
mtissington opened this issue Feb 5, 2017 · 10 comments
Closed

sqlite_exception ... access to error code? #80

mtissington opened this issue Feb 5, 2017 · 10 comments

Comments

@mtissington
Copy link

mtissington commented Feb 5, 2017

When a sqlite_exception is thrown how do I access the error code?

can you make error_code available?

Thanks.

@zauguin
Copy link
Collaborator

zauguin commented Feb 5, 2017

The error code can't be a accessed directly, you can differentiate the errors by the type and the message.
Why do you need the codes?

@mtissington
Copy link
Author

Hmm, you have made row id available, surly its straight forward to make the error_code available?

When I catch-all exceptions using sqlite_exception, using error_code I could have a simple switch statement.

How is type available in the catch-all sqlite_exception?

@mtissington
Copy link
Author

Unless I'm missing something the sqlite_exception is a struct that contains a string. It would be of great help
to have the struct also contain the error code ... :)

@zauguin
Copy link
Collaborator

zauguin commented Feb 6, 2017

Just in case you still need this: I created a PoC (zauguin/errorcodes) where the exception object contains a get_code() method to access the code.
But I do not think that this is generally useful. Why do you want to catch sqlite_exception and use a switch instead of catching the individual error types?

@mtissington
Copy link
Author

Thanks ... I used your earlier suggestion and used typeid(object) instead. For all exceptions I need to just log the error and no_row I need to do a little extra processing. It's been a long day!

@mtissington
Copy link
Author

Having thought about this I think using get_code() is cleaner. Using typeid(e) seems to me to be a cludge to get the information.

I hope you merge this into master :)

@mtissington mtissington reopened this Feb 7, 2017
@zauguin
Copy link
Collaborator

zauguin commented Feb 7, 2017

@aminroosta What do you think about this?

@polesapart
Copy link

polesapart commented Feb 7, 2017

An IMHO cleaner (but API-breaking) change would be to change the sqlite_exception constructor to something like:

sqlite_exception(char ecode): runtime_error(sqlite3_errstr(ecode)), code(ecode) {}

Which would allow the specific exceptions to be initialized by (i.e.):

if(error_code == SQLITE_ERROR) throw exceptions::error(error_code);

instead of:

if(error_code == SQLITE_ERROR) throw exceptions::error(sqlite3_errstr(error_code), error_code);

But perhaps that's too disruptive for just the sake of looking better.

@zauguin
Copy link
Collaborator

zauguin commented Feb 7, 2017

Actually the old changing was API-breaking already, because you had to pass the errorcode.
So I pushed a change which adds this as a second constructor.

@aminroosta
Copy link
Collaborator

Looks good to me, Thanks @zauguin!
Can you write a little documentation in the README file :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants