Throw typed exceptions in automation rather than strings

Originally committed to SVN as r5639.
This commit is contained in:
Thomas Goyne 2011-09-28 19:48:28 +00:00
parent 6d0e44baad
commit c09259c93d
4 changed files with 18 additions and 44 deletions

View File

@ -293,7 +293,7 @@ namespace Automation4 {
FeatureFilter::FeatureFilter(const wxString &_name, const wxString &_description, int _priority)
: Feature(SCRIPTFEATURE_FILTER, _name)
, AssExportFilter(_name, _description, _priority)
, config_dialog(0)
, config_dialog(0)
{
AssExportFilterChain::Register(this);
}
@ -735,22 +735,12 @@ namespace Automation4 {
bool more = dir.GetFirst(&fn, wxEmptyString, wxDIR_FILES);
while (more) {
script_path.SetName(fn);
try {
wxString fullpath = script_path.GetFullPath();
if (ScriptFactory::CanHandleScriptFormat(fullpath)) {
Script *s = ScriptFactory::CreateFromFile(fullpath, true);
Add(s);
if (!s->GetLoadedState()) error_count++;
}
}
catch (const char *e) {
error_count++;
wxLogError("Error loading Automation script: %s\n%s", fn, e);
}
catch (...) {
error_count++;
wxLogError("Error loading Automation script: %s\nUnknown error.", fn);
}
wxString fullpath = script_path.GetFullPath();
if (ScriptFactory::CanHandleScriptFormat(fullpath)) {
Script *s = ScriptFactory::CreateFromFile(fullpath, true);
Add(s);
if (!s->GetLoadedState()) error_count++;
}
more = dir.GetNext(&fn);
}
}
@ -852,7 +842,7 @@ namespace Automation4 {
GetFactories();
if (find(factories->begin(), factories->end(), factory) != factories->end())
throw "Automation 4: Attempt to register the same script factory multiple times. This should never happen.";
throw agi::InternalError("Automation 4: Attempt to register the same script factory multiple times. This should never happen.", 0);
factories->push_back(factory);
}
@ -939,7 +929,7 @@ namespace Automation4 {
/// @param filename
///
UnknownScript::UnknownScript(const wxString &filename)
: Script(filename)
: Script(filename)
{
wxFileName fn(filename);
name = fn.GetName();

View File

@ -77,6 +77,9 @@ DECLARE_EVENT_TYPE(wxEVT_AUTOMATION_SCRIPT_COMPLETED, -1)
/// DOCME
namespace Automation4 {
DEFINE_BASE_EXCEPTION_NOINNER(AutomationError, agi::Exception)
DEFINE_SIMPLE_EXCEPTION_NOINNER(ScriptLoadError, AutomationError, "automation/load/generic")
DEFINE_SIMPLE_EXCEPTION_NOINNER(MacroRunError, AutomationError, "automation/macro/generic")
// Calculate the extents of a text string given a style
bool CalculateTextExtents(AssStyle *style, wxString &text, double &width, double &height, double &descent, double &extlead);

View File

@ -257,7 +257,7 @@ namespace Automation4 {
if (lua_load(L, script_reader.reader_func, &script_reader, GetPrettyFilename().mb_str(wxConvUTF8))) {
wxString err(lua_tostring(L, -1), wxConvUTF8);
err.Prepend("Error loading Lua script \"" + GetPrettyFilename() + "\":\n\n");
throw err;
throw ScriptLoadError(STD_STR(err));
}
_stackcheck.check_stack(1);
// and execute it
@ -267,16 +267,14 @@ namespace Automation4 {
// error occurred, assumed to be on top of Lua stack
wxString err(lua_tostring(L, -1), wxConvUTF8);
err.Prepend("Error initialising Lua script \"" + GetPrettyFilename() + "\":\n\n");
throw err;
throw ScriptLoadError(STD_STR(err));
}
_stackcheck.check_stack(0);
lua_getglobal(L, "version");
if (lua_isnumber(L, -1)) {
if (lua_tointeger(L, -1) == 3) {
lua_pop(L, 1); // just to avoid tripping the stackcheck in debug
// So this is an auto3 script...
// Throw it as an exception, the script factory manager will catch this and use the auto3 script instead of this script object
throw "Attempted to load an Automation 3 script as an Automation 4 Lua script. Automation 3 is no longer supported.";
throw ScriptLoadError("Attempted to load an Automation 3 script as an Automation 4 Lua script. Automation 3 is no longer supported.");
}
}
lua_getglobal(L, "script_name");
@ -302,31 +300,14 @@ namespace Automation4 {
_stackcheck.check_stack(0);
}
catch (const char *e) {
catch (agi::Exception const& e) {
Destroy();
loaded = false;
name = GetPrettyFilename();
description = wxString(e, wxConvUTF8);
}
catch (const wxString& e) {
Destroy();
loaded = false;
name = GetPrettyFilename();
description = e;
}
catch (Script *s) {
// Be sure to properly propagate any scripts throw
throw s;
}
catch (...) {
Destroy();
loaded = false;
name = GetPrettyFilename();
description = "Unknown error initialising Lua script";
description = e.GetChainedMessage();
}
}
/// @brief DOCME
/// @return
///

View File

@ -107,7 +107,7 @@ namespace {
lua_setfield(L, -2, name);
}
DEFINE_SIMPLE_EXCEPTION_NOINNER(BadField, agi::Exception, "automation/macro/bad_field")
DEFINE_SIMPLE_EXCEPTION_NOINNER(BadField, Automation4::MacroRunError, "automation/macro/bad_field")
BadField bad_field(const char *expected_type, const char *name, const char *line_clasee)
{
return BadField(std::string("Invalid ") + expected_type + " '" + name + "' field in '" + line_clasee + "' class subtitle line");