Skip to content

Commit

Permalink
centralizing exception handling, removing dead code
Browse files Browse the repository at this point in the history
  • Loading branch information
tenderlove committed Jan 25, 2010
1 parent c8e34e8 commit da74b8e
Show file tree
Hide file tree
Showing 9 changed files with 119 additions and 83 deletions.
23 changes: 5 additions & 18 deletions ext/sqlite3/database.c
Expand Up @@ -44,16 +44,7 @@ static VALUE initialize(int argc, VALUE *argv, VALUE self)
status = sqlite3_open(StringValuePtr(file), &ctx->db);
}

switch(status) {
case SQLITE_OK:
break;
case SQLITE_CANTOPEN:
rb_raise(rb_path2class("SQLite3::CantOpenException"),
"%s", sqlite3_errmsg(ctx->db), status);
break;
default:
rb_raise(rb_eRuntimeError, "%s", sqlite3_errmsg(ctx->db), status);
}
CHECK(ctx->db, status)

rb_iv_set(self, "@tracefunc", Qnil);
rb_iv_set(self, "@authorizer", Qnil);
Expand All @@ -75,8 +66,7 @@ static VALUE sqlite3_rb_close(VALUE self)
sqlite3RubyPtr ctx;
Data_Get_Struct(self, sqlite3Ruby, ctx);

if(SQLITE_OK != sqlite3_close(ctx->db))
rb_raise(rb_eRuntimeError, "%s", sqlite3_errmsg(ctx->db));
CHECK(ctx->db, sqlite3_close(ctx->db));

ctx->db = NULL;

Expand Down Expand Up @@ -253,8 +243,7 @@ static VALUE define_function(VALUE self, VALUE name)
NULL
);

if(SQLITE_OK != status)
rb_raise(rb_eRuntimeError, "%s", sqlite3_errmsg(ctx->db));
CHECK(ctx->db, status);

return self;
}
Expand Down Expand Up @@ -314,8 +303,7 @@ static VALUE define_aggregator(VALUE self, VALUE name, VALUE aggregator)
rb_sqlite3_final
);

if(SQLITE_OK != status)
rb_raise(rb_eRuntimeError, "%s", sqlite3_errmsg(ctx->db));
CHECK(ctx->db, status);

return self;
}
Expand Down Expand Up @@ -435,8 +423,7 @@ static VALUE set_authorizer(VALUE self, VALUE authorizer)
ctx->db, NIL_P(authorizer) ? NULL : rb_sqlite3_auth, (void *)self
);

if(SQLITE_OK != status)
rb_raise(rb_eRuntimeError, "%s", sqlite3_errmsg(ctx->db));
CHECK(ctx->db, status);

rb_iv_set(self, "@authorizer", authorizer);

Expand Down
94 changes: 94 additions & 0 deletions ext/sqlite3/exception.c
@@ -0,0 +1,94 @@
#include <sqlite3_ruby.h>

void rb_sqlite3_raise(sqlite3 * db, int status)
{
VALUE klass = Qnil;

switch(status) {
case SQLITE_OK:
return;
break;
case SQLITE_ERROR:
klass = rb_path2class("SQLite3::SQLException");
break;
case SQLITE_INTERNAL:
klass = rb_path2class("SQLite3::InternalException");
break;
case SQLITE_PERM:
klass = rb_path2class("SQLite3::PermissionException");
break;
case SQLITE_ABORT:
klass = rb_path2class("SQLite3::AbortException");
break;
case SQLITE_BUSY:
klass = rb_path2class("SQLite3::BusyException");
break;
case SQLITE_LOCKED:
klass = rb_path2class("SQLite3::LockedException");
break;
case SQLITE_NOMEM:
klass = rb_path2class("SQLite3::MemoryException");
break;
case SQLITE_READONLY:
klass = rb_path2class("SQLite3::ReadOnlyException");
break;
case SQLITE_INTERRUPT:
klass = rb_path2class("SQLite3::InterruptException");
break;
case SQLITE_IOERR:
klass = rb_path2class("SQLite3::IOException");
break;
case SQLITE_CORRUPT:
klass = rb_path2class("SQLite3::CorruptException");
break;
case SQLITE_NOTFOUND:
klass = rb_path2class("SQLite3::NotFoundException");
break;
case SQLITE_FULL:
klass = rb_path2class("SQLite3::FullException");
break;
case SQLITE_CANTOPEN:
klass = rb_path2class("SQLite3::CantOpenException");
break;
case SQLITE_PROTOCOL:
klass = rb_path2class("SQLite3::ProtocolException");
break;
case SQLITE_EMPTY:
klass = rb_path2class("SQLite3::EmptyException");
break;
case SQLITE_SCHEMA:
klass = rb_path2class("SQLite3::SchemaChangedException");
break;
case SQLITE_TOOBIG:
klass = rb_path2class("SQLite3::TooBigException");
break;
case SQLITE_CONSTRAINT:
klass = rb_path2class("SQLite3::ConstraintException");
break;
case SQLITE_MISMATCH:
klass = rb_path2class("SQLite3::MismatchException");
break;
case SQLITE_MISUSE:
klass = rb_path2class("SQLite3::MisuseException");
break;
case SQLITE_NOLFS:
klass = rb_path2class("SQLite3::UnsupportedException");
break;
case SQLITE_AUTH:
klass = rb_path2class("SQLite3::AuthorizationException");
break;
case SQLITE_FORMAT:
klass = rb_path2class("SQLite3::FormatException");
break;
case SQLITE_RANGE:
klass = rb_path2class("SQLite3::RangeException");
break;
case SQLITE_NOTADB:
klass = rb_path2class("SQLite3::NotADatabaseException");
break;
default:
klass = rb_eRuntimeError;
}

rb_raise(klass, "%s", sqlite3_errmsg(db));
}
8 changes: 8 additions & 0 deletions ext/sqlite3/exception.h
@@ -0,0 +1,8 @@
#ifndef SQLITE3_EXCEPTION_RUBY
#define SQLITE3_EXCEPTION_RUBY

#define CHECK(_db, _status) rb_sqlite3_raise(_db, _status);

void rb_sqlite3_raise(sqlite3 * db, int status);

#endif
1 change: 1 addition & 0 deletions ext/sqlite3/sqlite3_ruby.h
Expand Up @@ -8,5 +8,6 @@ extern VALUE mSqlite3;

#include <database.h>
#include <statement.h>
#include <exception.h>

#endif
29 changes: 8 additions & 21 deletions ext/sqlite3/statement.c
Expand Up @@ -49,17 +49,7 @@ static VALUE initialize(VALUE self, VALUE db, VALUE sql)
&tail
);

switch(status) {
case SQLITE_OK:
break;
case SQLITE_AUTH:
rb_raise(
rb_path2class("SQLite3::AuthorizationException"), "%s",
sqlite3_errmsg(db_ctx->db));
break;
default:
rb_raise(rb_path2class("SQLite3::SQLException"), "%s", sqlite3_errmsg(db_ctx->db));
}
CHECK(db_ctx->db, status);

rb_iv_set(self, "@connection", db);
rb_iv_set(self, "@remainder", rb_str_new2(tail));
Expand All @@ -82,8 +72,8 @@ static VALUE sqlite3_rb_close(VALUE self)

REQUIRE_OPEN_STMT(ctx);

if(SQLITE_OK != sqlite3_finalize(ctx->st))
rb_raise(rb_path2class("SQLite3::SQLException"), "uh oh!"); // FIXME this should come from the DB
sqlite3 * db = sqlite3_db_handle(ctx->st);
CHECK(db, sqlite3_finalize(ctx->st));

ctx->st = NULL;

Expand Down Expand Up @@ -146,7 +136,7 @@ static VALUE step(VALUE self)
rb_ary_push(list, Qnil);
break;
default:
rb_raise(rb_eRuntimeError, "bad type"); // FIXME
rb_raise(rb_eRuntimeError, "bad type");
}
}
}
Expand All @@ -156,7 +146,7 @@ static VALUE step(VALUE self)
return Qnil;
break;
default:
rb_raise(rb_eRuntimeError, "oh no! %d", value); // FIXME
CHECK(sqlite3_db_handle(ctx->st), value);
}

return list;
Expand Down Expand Up @@ -221,10 +211,8 @@ static VALUE bind_param(VALUE self, VALUE key, VALUE value)
break;
}

if(SQLITE_OK != status) {
sqlite3 * db = sqlite3_db_handle(ctx->st);
rb_raise(rb_eRuntimeError, "%s", sqlite3_errmsg(db));
}
CHECK(sqlite3_db_handle(ctx->st), status);

return self;
}

Expand All @@ -240,8 +228,7 @@ static VALUE reset_bang(VALUE self)
REQUIRE_OPEN_STMT(ctx);

int status = sqlite3_reset(ctx->st);
if(SQLITE_OK != status)
rb_raise(rb_eRuntimeError, "bind params"); // FIXME this should come from the DB
CHECK(sqlite3_db_handle(ctx->st), status);

ctx->done_p = 0;

Expand Down
24 changes: 0 additions & 24 deletions lib/sqlite3/errors.rb
@@ -1,7 +1,6 @@
require 'sqlite3/constants'

module SQLite3

class Exception < ::StandardError
@code = 0

Expand Down Expand Up @@ -42,27 +41,4 @@ class AuthorizationException < Exception; end
class FormatException < Exception; end
class RangeException < Exception; end
class NotADatabaseException < Exception; end

EXCEPTIONS = [
nil,
SQLException, InternalException, PermissionException,
AbortException, BusyException, LockedException, MemoryException,
ReadOnlyException, InterruptException, IOException, CorruptException,
NotFoundException, FullException, CantOpenException, ProtocolException,
EmptyException, SchemaChangedException, TooBigException,
ConstraintException, MismatchException, MisuseException,
UnsupportedException, AuthorizationException, FormatException,
RangeException, NotADatabaseException
].each_with_index { |e,i| e.instance_variable_set( :@code, i ) if e }

module Error
def check( result, db=nil, msg=nil )
unless result == Constants::ErrorCode::OK
msg = ( msg ? msg + ": " : "" ) + db.errmsg if db
raise(( EXCEPTIONS[result] || SQLite3::Exception ), msg)
end
end
module_function :check
end

end
4 changes: 2 additions & 2 deletions test/test_database.rb
Expand Up @@ -201,7 +201,7 @@ def test_authorizer_fail
@db.authorizer = Class.new {
def call action, a, b, c, d; false end
}.new
assert_raises(SQLite3::SQLException) do
assert_raises(SQLite3::AuthorizationException) do
@db.prepare("select 'fooooo'")
end
end
Expand All @@ -210,7 +210,7 @@ def test_remove_auth
@db.authorizer = Class.new {
def call action, a, b, c, d; false end
}.new
assert_raises(SQLite3::SQLException) do
assert_raises(SQLite3::AuthorizationException) do
@db.prepare("select 'fooooo'")
end

Expand Down
17 changes: 0 additions & 17 deletions test/test_errors.rb

This file was deleted.

2 changes: 1 addition & 1 deletion test/test_integration.rb
Expand Up @@ -461,7 +461,7 @@ def test_interrupt
func.result = x
end

assert_raise( SQLite3::SQLException ) do
assert_raise( SQLite3::InterruptException ) do
@db.execute "select abort(a) from foo"
end
end
Expand Down

0 comments on commit da74b8e

Please sign in to comment.