From 8354478e724fff3691621d4dc2ded153acf05713 Mon Sep 17 00:00:00 2001 From: Luna Date: Wed, 7 Dec 2022 15:30:22 -0300 Subject: [PATCH] properly print config errors when loading config file --- config.lua | 9 +++++---- main.lua | 1 + util.lua | 11 +++++++---- 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/config.lua b/config.lua index 58b1750..846d6b4 100644 --- a/config.lua +++ b/config.lua @@ -114,10 +114,11 @@ local function loadConfigFile() local config_file_function = assert(loadstring(config_file_data)) local config_object = config_file_function() local schema_errors = validateConfigFile(config_object) - if schema_errors then - for name, errors in pairs(schema_errors) do - log('CONFIG ERROR' .. writeSchemaErrors(errors, log)) - end + + local total_count = table.pprint(schema_errors, {call=function() end}) + if total_count > 0 then + log('CONFIG ERROR') + table.pprint(schema_errors, {call=log}) end return config_object end diff --git a/main.lua b/main.lua index 2be3b01..60fbb8e 100644 --- a/main.lua +++ b/main.lua @@ -8,6 +8,7 @@ local ctx = require('ctx') local config = require('config') +require('util') ctx:loadFromConfig(config.loadConfigFile()) diff --git a/util.lua b/util.lua index a510a1c..4db2229 100644 --- a/util.lua +++ b/util.lua @@ -4,21 +4,24 @@ function table.len(t) return count end -function table.pprint(t, ident, total_count) +function table.pprint(t, options, ident, total_count) local ident = ident or 0 local total_count = total_count or 0 + + local options = options or {} + local print_function = options.call or print if type(t) == 'table' then local count = 0 for k, v in pairs(t) do - print(string.rep('\t', ident) .. k) + print_function(string.rep('\t', ident) .. k) count = count + 1 - total_count = pprint(v, ident + 1, total_count) + total_count = table.pprint(v, options, ident + 1, total_count) end if count == 0 then --print('') end else - print(string.rep('\t', ident) .. tostring(t)) + print_function(string.rep('\t', ident) .. tostring(t)) total_count = total_count + 1 end return total_count