From ad5e90ee99d6e6c200349e9f3801ce395ec0cbbf Mon Sep 17 00:00:00 2001 From: Emery Berger Date: Mon, 10 Apr 2023 14:29:10 -0400 Subject: [PATCH] Handle edge cases. --- sqlwrite.cpp | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/sqlwrite.cpp b/sqlwrite.cpp index 4a0cf80..bd8042c 100644 --- a/sqlwrite.cpp +++ b/sqlwrite.cpp @@ -15,6 +15,7 @@ #include #include +#include #include "openai.hpp" #include "sqlite3ext.h" @@ -94,9 +95,13 @@ static void real_ask_command(sqlite3_context *ctx, int argc, sqlite3_value **arg query += "\n\nExisting indexes:\n"; printed_index_header = true; } - const char *tbl_name = reinterpret_cast(sqlite3_column_text(stmt, 2)); - const char *sql = reinterpret_cast(sqlite3_column_text(stmt, 3)); - query += fmt::format("Index for {}: {}\n", tbl_name, sql); + try { + const char *tbl_name = reinterpret_cast(sqlite3_column_text(stmt, 2)); + const char *sql = reinterpret_cast(sqlite3_column_text(stmt, 3)); + query += fmt::format("Index for {}: {}\n", tbl_name, sql); + } catch (fmt::v9::format_error& fe) { + // Ignore indices where the query response is null, which could get us here. + } } nlohmann::json prompt; @@ -160,12 +165,15 @@ static void real_ask_command(sqlite3_context *ctx, int argc, sqlite3_value **arg } } break; - } catch (std::runtime_error re) { + } catch (std::runtime_error& re) { std::cout << fmt::format("Runtime error: {}\n", re.what()); - } catch (nlohmann::json_abi_v3_11_2::detail::parse_error pe) { + } catch (nlohmann::json_abi_v3_11_2::detail::parse_error& pe) { // Retry if there were JSON parse errors. - } catch (nlohmann::json_abi_v3_11_2::detail::type_error te) { + } catch (nlohmann::json_abi_v3_11_2::detail::type_error& te) { // Retry if there were JSON parse errors. + } catch (fmt::v9::format_error& fe) { + std::cerr << "error: " << fe.what() << std::endl; + // Retry if there is a format error. } } sqlite3_finalize(stmt);