Commit a4bf2f9f authored by Julien Michel's avatar Julien Michel
Browse files

ENH: Do not catch exception when testenv option is used

No related merge requests found
Showing with 85 additions and 68 deletions
+85 -68
...@@ -172,6 +172,12 @@ private: ...@@ -172,6 +172,12 @@ private:
CommandLineLauncher(const CommandLineLauncher &) = delete; CommandLineLauncher(const CommandLineLauncher &) = delete;
void operator =(const CommandLineLauncher&) = delete; void operator =(const CommandLineLauncher&) = delete;
/**
* Actually launch the process and write outputs, without catching exceptions.
*/
bool ExecuteAndWriteOutputNoCatch();
std::string m_Path; std::string m_Path;
Application::Pointer m_Application; Application::Pointer m_Application;
......
...@@ -137,68 +137,79 @@ bool CommandLineLauncher::Execute() ...@@ -137,68 +137,79 @@ bool CommandLineLauncher::Execute()
return false; return false;
} }
bool CommandLineLauncher::ExecuteAndWriteOutput() bool CommandLineLauncher::ExecuteAndWriteOutputNoCatch()
{ {
try if (this->BeforeExecute() == false)
{ {
if (this->BeforeExecute() == false)
{
return false; return false;
} }
if( m_Application->ExecuteAndWriteOutput() == 0 ) if( m_Application->ExecuteAndWriteOutput() == 0 )
{ {
this->DisplayOutputParameters(); this->DisplayOutputParameters();
} }
else else
{ {
return false; return false;
}
return true;
}
bool CommandLineLauncher::ExecuteAndWriteOutput()
{
// If testenv is used, do not catch exceptions
if (m_Parser->IsAttributExists("-testenv", m_VExpression) == true)
{
ExecuteAndWriteOutputNoCatch();
} }
} else
catch(otb::ApplicationException& err) {
{ try
{
ExecuteAndWriteOutputNoCatch();
}
catch(otb::ApplicationException& err)
{
// These are thrown with otbAppLogFATAL, a macro which logs a user // These are thrown with otbAppLogFATAL, a macro which logs a user
// friendly error message before throwing. So log exception details only // friendly error message before throwing. So log exception details only
// in debug. // in debug.
m_Application->GetLogger()->Debug("Caught otb::ApplicationException during application execution:\n"); m_Application->GetLogger()->Debug("Caught otb::ApplicationException during application execution:\n");
m_Application->GetLogger()->Debug(string(err.what()) + "\n"); m_Application->GetLogger()->Debug(string(err.what()) + "\n");
return false; return false;
} }
catch(otb::ImageFileReaderException& err) catch(otb::ImageFileReaderException& err)
{ {
m_Application->GetLogger()->Debug("Caught otb::ImageFileReaderException during application execution:\n"); m_Application->GetLogger()->Debug("Caught otb::ImageFileReaderException during application execution:\n");
m_Application->GetLogger()->Debug(string(err.what()) + "\n"); m_Application->GetLogger()->Debug(string(err.what()) + "\n");
m_Application->GetLogger()->Fatal(string("Cannot open image ") + err.m_Filename + string(". ") + err.GetDescription() + string("\n")); m_Application->GetLogger()->Fatal(string("Cannot open image ") + err.m_Filename + string(". ") + err.GetDescription() + string("\n"));
return false; return false;
} }
catch(itk::ExceptionObject& err) catch(itk::ExceptionObject& err)
{ {
m_Application->GetLogger()->Debug("Caught itk::ExceptionObject during application execution:\n"); m_Application->GetLogger()->Debug("Caught itk::ExceptionObject during application execution:\n");
m_Application->GetLogger()->Debug(string(err.what()) + "\n"); m_Application->GetLogger()->Debug(string(err.what()) + "\n");
m_Application->GetLogger()->Fatal(string(err.GetDescription()) + "\n"); m_Application->GetLogger()->Fatal(string(err.GetDescription()) + "\n");
return false; return false;
} }
catch(std::exception& err) catch(std::exception& err)
{ {
m_Application->GetLogger()->Fatal(std::string("Caught std::exception during application execution: ") + err.what() + "\n"); m_Application->GetLogger()->Fatal(std::string("Caught std::exception during application execution: ") + err.what() + "\n");
return false; return false;
} }
catch(...) catch(...)
{ {
m_Application->GetLogger()->Fatal("Caught unknown exception during application execution.\n"); m_Application->GetLogger()->Fatal("Caught unknown exception during application execution.\n");
return false; return false;
} }
}
return true;
} }
bool CommandLineLauncher::BeforeExecute() bool CommandLineLauncher::BeforeExecute()
{ {
if (m_Application.IsNull()) if (m_Application.IsNull())
{ {
std::cerr << "ERROR: No loaded application." << std::endl; std::cerr << "ERROR: No loaded application." << std::endl;
return false; return false;
} }
// Check if there's keys in the expression if the application takes // Check if there's keys in the expression if the application takes
// at least 1 mandatory parameter // at least 1 mandatory parameter
...@@ -263,15 +274,15 @@ bool CommandLineLauncher::BeforeExecute() ...@@ -263,15 +274,15 @@ bool CommandLineLauncher::BeforeExecute()
ParamResultType result = this->LoadParameters(); ParamResultType result = this->LoadParameters();
if (result == MISSINGMANDATORYPARAMETER) if (result == MISSINGMANDATORYPARAMETER)
{ {
std::cerr << std::endl; std::cerr << std::endl;
this->DisplayHelp(); this->DisplayHelp();
return false; return false;
} }
else if (result != OKPARAM) else if (result != OKPARAM)
{ {
return false; return false;
} }
return true; return true;
} }
...@@ -365,22 +376,22 @@ CommandLineLauncher::ParamResultType CommandLineLauncher::LoadParameters() ...@@ -365,22 +376,22 @@ CommandLineLauncher::ParamResultType CommandLineLauncher::LoadParameters()
// Check for the progress report parameter // Check for the progress report parameter
if (m_Parser->IsAttributExists("-progress", m_VExpression) == true) if (m_Parser->IsAttributExists("-progress", m_VExpression) == true)
{ {
std::vector<std::string> val = m_Parser->GetAttribut("-progress", m_VExpression); std::vector<std::string> val = m_Parser->GetAttribut("-progress", m_VExpression);
if (val.size() == 1 && (val[0] == "1" || val[0] == "true")) if (val.size() == 1 && (val[0] == "1" || val[0] == "true"))
{ {
m_ReportProgress = true; m_ReportProgress = true;
} }
else if (val.size() == 1 && (val[0] == "0" || val[0] == "false")) else if (val.size() == 1 && (val[0] == "0" || val[0] == "false"))
{ {
m_ReportProgress = false; m_ReportProgress = false;
} }
else else
{ {
std::cerr << "ERROR: Invalid value for parameter -progress. It must be 0, 1, false or true." << std::endl; std::cerr << "ERROR: Invalid value for parameter -progress. It must be 0, 1, false or true." << std::endl;
return WRONGPARAMETERVALUE; return WRONGPARAMETERVALUE;
}
} }
}
const std::vector<std::string> appKeyList = m_Application->GetParametersKeys(true); const std::vector<std::string> appKeyList = m_Application->GetParametersKeys(true);
// Loop over each parameter key declared in the application // Loop over each parameter key declared in the application
...@@ -421,22 +432,22 @@ CommandLineLauncher::ParamResultType CommandLineLauncher::LoadParameters() ...@@ -421,22 +432,22 @@ CommandLineLauncher::ParamResultType CommandLineLauncher::LoadParameters()
m_Application->SetParameterStringList(paramKey, values); m_Application->SetParameterStringList(paramKey, values);
} }
else if (type == ParameterType_Choice || else if (type == ParameterType_Choice ||
type == ParameterType_Float || type == ParameterType_Float ||
type == ParameterType_Int || type == ParameterType_Int ||
type == ParameterType_Radius || type == ParameterType_Radius ||
type == ParameterType_Directory || type == ParameterType_Directory ||
type == ParameterType_String || type == ParameterType_String ||
type == ParameterType_InputFilename || type == ParameterType_InputFilename ||
type == ParameterType_OutputFilename || type == ParameterType_OutputFilename ||
type == ParameterType_ComplexInputImage || type == ParameterType_ComplexInputImage ||
type == ParameterType_InputImage || type == ParameterType_InputImage ||
type == ParameterType_OutputImage || type == ParameterType_OutputImage ||
type == ParameterType_ComplexOutputImage || type == ParameterType_ComplexOutputImage ||
type == ParameterType_InputVectorData || type == ParameterType_InputVectorData ||
type == ParameterType_OutputVectorData || type == ParameterType_OutputVectorData ||
type == ParameterType_RAM || type == ParameterType_RAM ||
type == ParameterType_OutputProcessXML || type == ParameterType_OutputProcessXML ||
type == ParameterType_Bool) // || type == ParameterType_InputProcessXML) type == ParameterType_Bool) // || type == ParameterType_InputProcessXML)
{ {
// Single value parameter // Single value parameter
m_Application->SetParameterString(paramKey, values[0]); m_Application->SetParameterString(paramKey, values[0]);
...@@ -459,7 +470,7 @@ CommandLineLauncher::ParamResultType CommandLineLauncher::LoadParameters() ...@@ -459,7 +470,7 @@ CommandLineLauncher::ParamResultType CommandLineLauncher::LoadParameters()
{ {
std::cerr << "ERROR: Too many values for parameter -" << std::cerr << "ERROR: Too many values for parameter -" <<
paramKey << " (expected 2 or 1, got " << values.size() << ")." paramKey << " (expected 2 or 1, got " << values.size() << ")."
<< std::endl; << std::endl;
return INVALIDNUMBEROFVALUE; return INVALIDNUMBEROFVALUE;
} }
} }
...@@ -756,11 +767,11 @@ std::string CommandLineLauncher::DisplayParameterHelp(const Parameter::Pointer & ...@@ -756,11 +767,11 @@ std::string CommandLineLauncher::DisplayParameterHelp(const Parameter::Pointer &
oss << "["; oss << "[";
for(unsigned int i=0; i<keys.size(); i++) for(unsigned int i=0; i<keys.size(); i++)
{ {
oss<<keys[i]; oss<<keys[i];
if( i != keys.size()-1 ) if( i != keys.size()-1 )
oss << "/"; oss << "/";
} }
oss << "]"; oss << "]";
} }
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment