Skip to content

Commit

Permalink
Handle json file read errors
Browse files Browse the repository at this point in the history
  • Loading branch information
ArKaNeMaN committed Nov 2, 2023
1 parent dba12d1 commit 26ea5b5
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions scripting/player_prefs.sma
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public plugin_init() {
register_plugin("Player preferences", "1.1.1", "ufame");

CreateForwards();
ReadDbCreadentials();
ReadDbCredentials();

g_bDebugMode = bool: (plugin_flags() & AMX_FLAG_DEBUG);
}
Expand Down Expand Up @@ -382,8 +382,23 @@ public ThreadQuery_Handler(iFailState, Handle: hQuery, szError[], iError, szData
}
}

ReadDbCreadentials() {
new JSON: credsConfig = json_parse(CONFIG_FILE, true);
ReadDbCredentials() {
if (!file_exists(CONFIG_FILE)) {
abort(AMX_ERR_GENERAL, "[PP] DB credentials file not found (%s).", CONFIG_FILE);
return;
}

new JSON: credsConfig = json_parse(CONFIG_FILE, true, true);

if (credsConfig == Invalid_JSON) {
abort(AMX_ERR_GENERAL, "[PP] JSON synax error in DB credentials file (%s).", CONFIG_FILE);
return;
}

if (!json_is_object(credsConfig)) {
abort(AMX_ERR_GENERAL, "[PP] DB credentials file must contain a JSON-object (%s).", CONFIG_FILE);
return;
}

json_object_get_string(credsConfig, "host", g_szSqlHost, charsmax(g_szSqlHost));
json_object_get_string(credsConfig, "user", g_szSqlUser, charsmax(g_szSqlUser));
Expand Down

0 comments on commit 26ea5b5

Please sign in to comment.