From 01253f1cdf373a37d207ff24c9540e5a60d4ec72 Mon Sep 17 00:00:00 2001 From: Paul Beckingham Date: Wed, 6 Jul 2011 22:52:59 -0400 Subject: [PATCH] Error Handling - Errors that occur during initialization no longer prevent the display of debug info. --- src/Context.cpp | 143 +++++++++++++++++++++++++++++------------------- src/Context.h | 2 +- src/main.cpp | 5 +- 3 files changed, 90 insertions(+), 60 deletions(-) diff --git a/src/Context.cpp b/src/Context.cpp index 2dcd332d7..2fa4bc3cd 100644 --- a/src/Context.cpp +++ b/src/Context.cpp @@ -65,85 +65,114 @@ Context::~Context () } //////////////////////////////////////////////////////////////////////////////// -void Context::initialize (int argc, const char** argv) +int Context::initialize (int argc, const char** argv) { Timer t ("Context::initialize"); + int rc = 0; - // char** argv --> std::vector Context::args. - // TODO Handle "cal" case here. - args.capture (argc, argv); + try + { + // char** argv --> std::vector Context::args. + // TODO Handle "cal" case here. + args.capture (argc, argv); - // echo one two -- three | task zero --> task zero one two - // 'three' is left in the input buffer. - args.append_stdin (); + // echo one two -- three | task zero --> task zero one two + // 'three' is left in the input buffer. + args.append_stdin (); - // Assume default .taskrc and .task locations. - assumeLocations (); + // Assume default .taskrc and .task locations. + assumeLocations (); - // Process 'rc:' command line override, and remove the argument from the - // Context::args. - args.rc_override (home_dir, rc_file); + // Process 'rc:' command line override, and remove the argument from the + // Context::args. + args.rc_override (home_dir, rc_file); - // Dump any existing values and load rc file. - config.clear (); - config.load (rc_file); + // Dump any existing values and load rc file. + config.clear (); + config.load (rc_file); - // The data location, Context::data_dir, is determined from the assumed - // location (~/.task), or set by data.location in the config file, or - // overridden by rc.data.location on the command line. - std::string location; - args.get_data_location (location); - data_dir = Directory (location); - extension_dir = data_dir.data + "/extensions"; + // The data location, Context::data_dir, is determined from the assumed + // location (~/.task), or set by data.location in the config file, or + // overridden by rc.data.location on the command line. + std::string location; + args.get_data_location (location); + data_dir = Directory (location); + extension_dir = data_dir.data + "/extensions"; - // Create missing config file and data directory, if necessary. - createDefaultConfig (); + // Create missing config file and data directory, if necessary. + createDefaultConfig (); - // Handle Aliases. - loadAliases (); - args.resolve_aliases (); + // Handle Aliases. + loadAliases (); + args.resolve_aliases (); - // Apply rc overrides to Context::config, capturing raw args for later use. - args.apply_overrides (); + // Apply rc overrides to Context::config, capturing raw args for later use. + args.apply_overrides (); - // Initialize the color rules, if necessary. - if (color ()) - initializeColorRules (); + // Initialize the color rules, if necessary. + if (color ()) + initializeColorRules (); - // Instantiate built-in command objects. - Command::factory (commands); + // Instantiate built-in command objects. + Command::factory (commands); - // Instantiate built-in column objects. - Column::factory (columns); + // Instantiate built-in column objects. + Column::factory (columns); - // Categorize all arguments one more time. - args.categorize (); + // Categorize all arguments one more time. + args.categorize (); - // Handle default command and assumed 'info' command. - args.inject_defaults (); + // Handle default command and assumed 'info' command. + args.inject_defaults (); - // TODO Instantiate extension command objects. - // TODO Instantiate default command object. + // TODO Instantiate extension command objects. + // TODO Instantiate default command object. - // TODO Instantiate extension column objects. + // TODO Instantiate extension column objects. - // TODO Instantiate extension UDA objects. - // TODO Instantiate extension format objects. + // TODO Instantiate extension UDA objects. + // TODO Instantiate extension format objects. - // If there is a locale variant (en-US.), then strip it. - std::string locale = config.get ("locale"); - std::string::size_type period = locale.find ('.'); - if (period != std::string::npos) - locale = locale.substr (0, period); + // If there is a locale variant (en-US.), then strip it. + std::string locale = config.get ("locale"); + std::string::size_type period = locale.find ('.'); + if (period != std::string::npos) + locale = locale.substr (0, period); - // Initialize the database. - tdb.clear (); - tdb.location (data_dir); + // Initialize the database. + tdb.clear (); + tdb.location (data_dir); - // Hook system init, plus post-start event occurring at the first possible - // moment after hook initialization. - hooks.initialize (); - hooks.trigger ("on-launch"); + // Hook system init, plus post-start event occurring at the first possible + // moment after hook initialization. + hooks.initialize (); + hooks.trigger ("on-launch"); + } + + catch (const std::string& error) + { + footnote (error); + rc = 2; + } + + catch (...) + { + footnote (STRING_UNKNOWN_ERROR); + rc = 3; + } + + // Dump all debug messages, controlled by rc.debug. + if (rc && config.getBoolean ("debug")) + { + std::vector ::iterator d; + for (d = debugMessages.begin (); d != debugMessages.end (); ++d) + if (color ()) + std::cout << colorizeDebug (*d) << "\n"; + else + std::cout << *d << "\n"; + } + + return rc; } //////////////////////////////////////////////////////////////////////////////// diff --git a/src/Context.h b/src/Context.h index def6ba196..112794d7f 100644 --- a/src/Context.h +++ b/src/Context.h @@ -50,7 +50,7 @@ public: Context (const Context&); Context& operator= (const Context&); - void initialize (int, const char**); // all startup + int initialize (int, const char**); // all startup int run (); int dispatch (std::string&); // command handler dispatch void shadow (); // shadow file update diff --git a/src/main.cpp b/src/main.cpp index 9ac233af2..a0fd43a9b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -61,8 +61,9 @@ int main (int argc, const char** argv) try { - context.initialize (argc, argv); - status = context.run (); + status = context.initialize (argc, argv); + if (status == 0) + status = context.run (); } catch (std::string& error)