Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix exceptions thrown by FGFunction and caught by FGAerodynamics #1152

Merged
merged 2 commits into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 18 additions & 18 deletions src/math/FGFunction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,19 +53,19 @@

//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

class WrongNumberOfArguments : public runtime_error
class WrongNumberOfArguments : public BaseException
{
public:
WrongNumberOfArguments(const string &msg, const vector<FGParameter_ptr> &p,
Element* el)
: runtime_error(msg), Parameters(p), element(el) {}
: BaseException(msg), Parameters(p), element(el) {}

Check warning on line 61 in src/math/FGFunction.cpp

View check run for this annotation

Codecov / codecov/patch

src/math/FGFunction.cpp#L61

Added line #L61 was not covered by tests
size_t NumberOfArguments(void) const { return Parameters.size(); }
FGParameter* FirstParameter(void) const { return *(Parameters.cbegin()); }
const Element* GetElement(void) const { return element; }

private:
const vector<FGParameter_ptr> Parameters;
const Element* element;
const Element_ptr element;
};

//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Expand Down Expand Up @@ -158,7 +158,7 @@
cerr << ctxMsg << FGJSBBase::fgred << FGJSBBase::highint
<< "Malformed conditional check in function definition."
<< FGJSBBase::reset << endl;
throw("Fatal Error.");
throw BaseException("Fatal Error.");

Check warning on line 161 in src/math/FGFunction.cpp

View check run for this annotation

Codecov / codecov/patch

src/math/FGFunction.cpp#L161

Added line #L161 was not covered by tests
}
}

Expand Down Expand Up @@ -197,7 +197,7 @@
return e.FirstParameter();
}
else
throw e.what();
throw e;

Check warning on line 200 in src/math/FGFunction.cpp

View check run for this annotation

Codecov / codecov/patch

src/math/FGFunction.cpp#L200

Added line #L200 was not covered by tests
}
}

Expand Down Expand Up @@ -272,15 +272,15 @@
cerr << el->ReadFrom() << fgred << highint
<< "<" << el->GetName() << "> must have an even number of arguments."
<< reset << endl;
throw("Fatal Error");
throw BaseException("Fatal Error");

Check warning on line 275 in src/math/FGFunction.cpp

View check run for this annotation

Codecov / codecov/patch

src/math/FGFunction.cpp#L275

Added line #L275 was not covered by tests
}
break;
case OddEven::Odd:
if (Parameters.size() % 2 == 0) {
cerr << el->ReadFrom() << fgred << highint
<< "<" << el->GetName() << "> must have an odd number of arguments."
<< reset << endl;
throw("Fatal Error");
throw BaseException("Fatal Error");

Check warning on line 283 in src/math/FGFunction.cpp

View check run for this annotation

Codecov / codecov/patch

src/math/FGFunction.cpp#L283

Added line #L283 was not covered by tests
}
break;
default:
Expand Down Expand Up @@ -310,7 +310,7 @@
{
Name = el->GetAttributeValue("name");
Element* element = el->GetElement();

auto sum = [](const decltype(Parameters)& Parameters)->double {
double temp = 0.0;

Expand All @@ -319,7 +319,7 @@

return temp;
};

while (element) {
string operation = element->GetName();

Expand All @@ -338,7 +338,7 @@
cerr << element->ReadFrom()
<< fgred << "Illegal use of the special character '#'"
<< reset << endl;
throw("Fatal Error.");
throw BaseException("Fatal Error.");

Check warning on line 341 in src/math/FGFunction.cpp

View check run for this annotation

Codecov / codecov/patch

src/math/FGFunction.cpp#L341

Added line #L341 was not covered by tests
}
}

Expand Down Expand Up @@ -665,7 +665,7 @@
cerr << ctxMsg << fgred << highint
<< "The switch function index (" << temp
<< ") is negative." << reset << endl;
throw("Fatal error");
throw BaseException("Fatal error");

Check warning on line 668 in src/math/FGFunction.cpp

View check run for this annotation

Codecov / codecov/patch

src/math/FGFunction.cpp#L668

Added line #L668 was not covered by tests
}
size_t n = p.size()-1;
size_t i = static_cast<size_t>(temp+0.5);
Expand All @@ -678,7 +678,7 @@
<< ") selected a value above the range of supplied values"
<< "[0:" << n-1 << "]"
<< " - not enough values were supplied." << reset << endl;
throw("Fatal error");
throw BaseException("Fatal error");

Check warning on line 681 in src/math/FGFunction.cpp

View check run for this annotation

Codecov / codecov/patch

src/math/FGFunction.cpp#L681

Added line #L681 was not covered by tests
}
};
Parameters.push_back(new aFunc<decltype(f), 2>(f, fdmex, element, Prefix,
Expand Down Expand Up @@ -767,10 +767,10 @@
double sina = sin(alpha_local);
double cosb;

if (fabs(cosa) > fabs(sina))
if (fabs(cosa) > fabs(sina))

Check warning on line 770 in src/math/FGFunction.cpp

View check run for this annotation

Codecov / codecov/patch

src/math/FGFunction.cpp#L770

Added line #L770 was not covered by tests
cosb = wind_local(eX) / cosa;
else
cosb = wind_local(eZ) / sina;
cosb = wind_local(eZ) / sina;

Check warning on line 773 in src/math/FGFunction.cpp

View check run for this annotation

Codecov / codecov/patch

src/math/FGFunction.cpp#L773

Added line #L773 was not covered by tests

return atan2(wind_local(eY), cosb)*radtodeg;
};
Expand Down Expand Up @@ -839,7 +839,7 @@
cerr << ctxMsg << fgred << highint
<< "The index must be one of the integer value 1, 2 or 3."
<< reset << endl;
throw("Fatal error");
throw BaseException("Fatal error");

Check warning on line 842 in src/math/FGFunction.cpp

View check run for this annotation

Codecov / codecov/patch

src/math/FGFunction.cpp#L842

Added line #L842 was not covered by tests
}

FGQuaternion qa(eY, -alpha), qb(eZ, beta), qc(eX, -gamma);
Expand Down Expand Up @@ -867,7 +867,7 @@
cerr << ctxMsg << fgred << highint
<< "The index must be one of the integer value 1, 2 or 3."
<< reset << endl;
throw("Fatal error");
throw BaseException("Fatal error");

Check warning on line 870 in src/math/FGFunction.cpp

View check run for this annotation

Codecov / codecov/patch

src/math/FGFunction.cpp#L870

Added line #L870 was not covered by tests
}

FGQuaternion qa(eY, -alpha), qb(eZ, beta), qc(eX, -gamma);
Expand Down Expand Up @@ -954,7 +954,7 @@
cached = true;
}
}

//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

double FGFunction::GetValue(void) const
Expand Down Expand Up @@ -1007,7 +1007,7 @@
if (pNode->isTied()) {
cerr << el->ReadFrom()
<< "Property " << nName << " has already been successfully bound (late)." << endl;
throw("Failed to bind the property to an existing already tied node.");
throw BaseException("Failed to bind the property to an existing already tied node.");

Check warning on line 1010 in src/math/FGFunction.cpp

View check run for this annotation

Codecov / codecov/patch

src/math/FGFunction.cpp#L1010

Added line #L1010 was not covered by tests
}
}

Expand Down
27 changes: 8 additions & 19 deletions src/models/FGAerodynamics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -372,30 +372,19 @@
axis = axis_element->GetAttributeValue("name");
function_element = axis_element->FindElement("function");
while (function_element) {
string current_func_name = function_element->GetAttributeValue("name");
bool apply_at_cg = false;
if (function_element->HasAttribute("apply_at_cg")) {
if (function_element->GetAttributeValue("apply_at_cg") == "true") apply_at_cg = true;
}
if (!apply_at_cg) {
try {
ca.push_back( new FGFunction(FDMExec, function_element) );
} catch (const string& str) {
if (function_element->HasAttribute("apply_at_cg") &&
function_element->GetAttributeValue("apply_at_cg") == "true")
ca_atCG.push_back(new FGFunction(FDMExec, function_element));

Check warning on line 378 in src/models/FGAerodynamics.cpp

View check run for this annotation

Codecov / codecov/patch

src/models/FGAerodynamics.cpp#L376-L378

Added lines #L376 - L378 were not covered by tests
else
ca.push_back(new FGFunction(FDMExec, function_element));
} catch (BaseException& e) {
string current_func_name = function_element->GetAttributeValue("name");

Check warning on line 382 in src/models/FGAerodynamics.cpp

View check run for this annotation

Codecov / codecov/patch

src/models/FGAerodynamics.cpp#L380-L382

Added lines #L380 - L382 were not covered by tests
FGXMLLogging log(FDMExec->GetLogger(), axis_element, LogLevel::ERROR);
log << LogFormat::RED << "\nError loading aerodynamic function in "
<< current_func_name << ":" << str << " Aborting.\n" << LogFormat::RESET;
<< current_func_name << ":" << e.what() << " Aborting.\n" << LogFormat::RESET;

Check warning on line 385 in src/models/FGAerodynamics.cpp

View check run for this annotation

Codecov / codecov/patch

src/models/FGAerodynamics.cpp#L385

Added line #L385 was not covered by tests
return false;
}
} else {
try {
ca_atCG.push_back( new FGFunction(FDMExec, function_element) );
} catch (const string& str) {
FGXMLLogging log(FDMExec->GetLogger(), axis_element, LogLevel::ERROR);
log << LogFormat::RED << "\nError loading aerodynamic function in "
<< current_func_name << ":" << str << " Aborting.\n" << LogFormat::RESET;
return false;
}
}
function_element = axis_element->FindNextElement("function");
}
AeroFunctions[AxisIdx[axis]] = ca;
Expand Down
Loading