Skip to content

Commit 5e4e9dc

Browse files
committed
Improved handling of runtime errors and when OPENAI_API_KEY is not set.
1 parent 26378a7 commit 5e4e9dc

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

sqlwrite.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ static void real_ask_command(sqlite3_context *ctx, int argc, sqlite3_value **arg
167167
break;
168168
} catch (std::runtime_error& re) {
169169
std::cout << fmt::format("Runtime error: {}\n", re.what());
170+
break;
170171
} catch (nlohmann::json_abi_v3_11_2::detail::parse_error& pe) {
171172
// Retry if there were JSON parse errors.
172173
} catch (nlohmann::json_abi_v3_11_2::detail::type_error& te) {
@@ -213,7 +214,16 @@ extern "C" int sqlite3_sqlwrite_init(sqlite3 *db, char **pzErrMsg, const sqlite3
213214
*pzErrMsg = sqlite3_mprintf("Failed to create sqlwrite function: %s", sqlite3_errmsg(db));
214215
return rc;
215216
}
216-
printf("SQLwrite extension successfully initialized. You can now use natural language queries like \"select ask('show me all artists.');\".\nPlease report any issues to https://github.com/plasma-umass/sqlwrite/issues/new\n");
217+
const char* key = std::getenv("OPENAI_API_KEY");
218+
if (!key) {
219+
printf("To use SQLwrite, you must have an API key saved as the environment variable OPENAI_API_KEY.\n");
220+
printf("For example, run `export OPENAI_API_KEY=sk-...`.\n");
221+
printf("If you do not have a key, you can get one here: https://openai.com/api/.\n");
222+
*pzErrMsg = sqlite3_mprintf("OPENAI_API_KEY environment variable not set.\n");
223+
return SQLITE_ERROR;
224+
}
225+
226+
printf("SQLwrite extension successfully initialized.\nYou can now use natural language queries like \"select ask('show me all artists.');\".\nPlease report any issues to https://github.com/plasma-umass/sqlwrite/issues/new\n");
217227

218228
return SQLITE_OK;
219229
}

0 commit comments

Comments
 (0)