From 188373fd0a0031c159935444afd601ee4218de1b Mon Sep 17 00:00:00 2001 From: Vincent Rischmann Date: Thu, 31 Dec 2020 22:04:08 +0100 Subject: add proper error types * Add SQLiteError and various SQLiteExtendedXYZError * Replace old errors with the SQLite ones where appropriate Fixes #8 --- sqlite.zig | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) (limited to 'sqlite.zig') diff --git a/sqlite.zig b/sqlite.zig index 6abedc4..4aae0e6 100644 --- a/sqlite.zig +++ b/sqlite.zig @@ -9,6 +9,7 @@ const c = @cImport({ }); usingnamespace @import("query.zig"); +usingnamespace @import("error.zig"); const logger = std.log.scoped(.sqlite); @@ -42,8 +43,6 @@ pub const InitOptions = struct { threading_mode: ThreadingMode = .Serialized, }; -pub const Error = error{}; - /// DetailedError contains a SQLite error code and error message. pub const DetailedError = struct { code: usize, @@ -128,7 +127,7 @@ pub const Db = struct { var db: ?*c.sqlite3 = undefined; const result = c.sqlite3_open_v2(path, &db, flags, null); if (result != c.SQLITE_OK or db == null) { - return error.CannotOpenDatabase; + return errorFromResultCode(result); } self.db = db.?; @@ -141,7 +140,7 @@ pub const Db = struct { var db: ?*c.sqlite3 = undefined; const result = c.sqlite3_open_v2(":memory:", &db, flags, null); if (result != c.SQLITE_OK or db == null) { - return error.CannotOpenDatabase; + return errorFromResultCode(result); } self.db = db.?; @@ -292,7 +291,7 @@ pub fn Iterator(comptime Type: type) type { return null; } if (result != c.SQLITE_ROW) { - return error.SQLiteStepError; + return errorFromResultCode(result); } const columns = c.sqlite3_column_count(self.stmt); @@ -332,7 +331,7 @@ pub fn Iterator(comptime Type: type) type { return null; } if (result != c.SQLITE_ROW) { - return error.SQLiteStepError; + return errorFromResultCode(result); } const columns = c.sqlite3_column_count(self.stmt); @@ -649,7 +648,7 @@ pub fn Statement(comptime opts: StatementOptions, comptime query: ParsedQuery) t null, ); if (result != c.SQLITE_OK) { - return error.CannotPrepareStatement; + return errorFromResultCode(result); } break :blk tmp.?; }; @@ -754,7 +753,7 @@ pub fn Statement(comptime opts: StatementOptions, comptime query: ParsedQuery) t const result = c.sqlite3_step(self.stmt); switch (result) { c.SQLITE_DONE => {}, - c.SQLITE_BUSY => return error.SQLiteBusy, + c.SQLITE_BUSY => return errorFromResultCode(result), else => std.debug.panic("invalid result {}", .{result}), } } -- cgit v1.2.3 From a39130b6b8e4900c10697a345fd890468503f6bd Mon Sep 17 00:00:00 2001 From: Vincent Rischmann Date: Thu, 31 Dec 2020 22:08:55 +0100 Subject: fix tests with the new errors --- sqlite.zig | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'sqlite.zig') diff --git a/sqlite.zig b/sqlite.zig index 4aae0e6..ec46ed9 100644 --- a/sqlite.zig +++ b/sqlite.zig @@ -1402,7 +1402,7 @@ test "sqlite: failing open" { .open_flags = .{}, .mode = .{ .File = "/tmp/not_existing.db" }, }); - testing.expectError(error.CannotOpenDatabase, res); + testing.expectError(error.SQLiteCantOpen, res); } test "sqlite: failing prepare statement" { @@ -1410,7 +1410,7 @@ test "sqlite: failing prepare statement" { try db.init(initOptions()); const result = db.prepare("SELECT id FROM foobar"); - testing.expectError(error.CannotPrepareStatement, result); + testing.expectError(error.SQLiteError, result); const detailed_err = db.getDetailedError(); testing.expectEqual(@as(usize, 1), detailed_err.code); -- cgit v1.2.3