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

Add OpenSim::Component overload to OpenSim::Exception #3987

Merged
merged 5 commits into from
Jan 22, 2025
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ v4.6
- Add utility method `createVectorLinspaceInterval` for the `std::vector` type and add unit tests. Utilize the new utility method to fix a bug (#3976) in creating the uniformly sampled time interval from the experimental data sampling frequency in `APDMDataReader` and `XsensDataReader` (#3977).
- Fix Point Kinematics Reporter variable and initialization error and add unit tests (#3966)
- `OpenSim::ContactHalfSpace`, `OpenSim::ContactMesh`, and `OpenSim::ContactSphere` now check their associated `Appearance`'s `is_visible` flag when deciding whether to emit their associated decorations (#3993).
- The message associated with `OpenSim::PropertyException` now includes the full absolute path to the component that threw the exception (#3987).

v4.5.1
======
Expand Down
40 changes: 40 additions & 0 deletions OpenSim/Common/AbstractProperty.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,55 @@
// INCLUDES
//============================================================================
#include "AbstractProperty.h"

#include "Assertion.h"
#include "Component.h"
#include "Object.h"

#include <limits>
#include <sstream>
#include <utility>

using namespace OpenSim;
using namespace SimTK;
using namespace std;

namespace
{
static std::string generateInvalidPropertyValueMessage(
const std::string& propertyName,
const std::string& errorMsg)
{
std::stringstream ss;
ss << "Property '" << propertyName << "' has an invalid property value.\n";
ss << "(details: " << errorMsg << ").\n";
return std::move(ss).str();
}
}

InvalidPropertyValue::InvalidPropertyValue(const std::string& file,
size_t line,
const std::string& func,
const Object& obj,
const std::string& propertyName,
const std::string& errorMsg) :
Exception(file, line, func, obj) {

addMessage(generateInvalidPropertyValueMessage(propertyName, errorMsg));
}

InvalidPropertyValue::InvalidPropertyValue(
const std::string& file,
size_t line,
const std::string& func,
const Component& component,
const std::string& propertyName,
const std::string& errorMsg) :
Exception(file, line, func, component) {

addMessage(generateInvalidPropertyValueMessage(propertyName, errorMsg));
}


//=============================================================================
// CONSTRUCTION
Expand Down
21 changes: 13 additions & 8 deletions OpenSim/Common/AbstractProperty.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,24 +35,29 @@
namespace OpenSim {

class Object;
class Component;
template <class T> class Property;

//==============================================================================
/// Property Exceptions
//==============================================================================
class InvalidPropertyValue : public Exception {
class OSIMCOMMON_API InvalidPropertyValue : public Exception {
public:
InvalidPropertyValue(const std::string& file,
InvalidPropertyValue(
const std::string& file,
size_t line,
const std::string& func,
const Object& obj,
const std::string& propertyName,
const std::string& errorMsg) :
Exception(file, line, func, obj) {
std::string msg = "Property '" + propertyName;
msg += "' has an invalid value.\n(details: " + errorMsg + ").\n";
addMessage(msg);
}
const std::string& errorMsg);

InvalidPropertyValue(
const std::string& file,
size_t line,
const std::string& func,
const Component& component,
const std::string& propertyName,
const std::string& errorMsg);
};


Expand Down
34 changes: 29 additions & 5 deletions OpenSim/Common/Exception.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,15 @@
* Author: Frank C. Anderson
*/


// INCLUDES
#include <iostream>
#include <string>
#include "osimCommonDLL.h"
#include "Exception.h"

#include "Component.h"
#include "IO.h"
#include "Object.h"
#include "osimCommonDLL.h"

#include <iostream>
#include <string>


using namespace OpenSim;
Expand Down Expand Up @@ -104,6 +105,29 @@ Exception::Exception(const std::string& file,
addMessage(msg);
}

Exception::Exception(
const std::string& file,
size_t line,
const std::string& func,
const Component& component)
: Exception{file, line, func} {

const std::string className = component.getConcreteClassName();
const std::string absolutePath = component.getAbsolutePathString();
addMessage("\tIn Component '" + absolutePath + "' of type " + className + ".");
}

Exception::Exception(
const std::string& file,
size_t line,
const std::string& func,
const Component& component,
const std::string& msg)
: Exception{file, line, func, component} {

addMessage(msg);
}

void
Exception::addMessage(const std::string& msg) {
if(_msg.length() == 0)
Expand Down
21 changes: 21 additions & 0 deletions OpenSim/Common/Exception.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@

namespace OpenSim {

class Component;
class Object;


Expand Down Expand Up @@ -176,6 +177,26 @@ class OSIMCOMMON_API Exception : public std::exception {
const Object& obj,
const std::string& msg);

/** The message created by this constructor will contain the class name and
absolute path of the provided Component. Use this when throwing from derived
components. Use OPENSIM_THROW<> macros at throw sites. */
Exception(
const std::string& file,
size_t line,
const std::string& func,
const Component& component);

/** The message created by this constructor will contain the class name and
ansolute path of the provided Component, and also accepts a message. Use this
when throwing from derived components. Use OPENSIM_THROW<> macros at throw
sites. */
Exception(
const std::string& file,
size_t line,
const std::string& func,
const Component& component,
const std::string& msg);

/** Use this when you want to throw an Exception (with OPENSIM_THROW or
OPENSIM_THROW_IF) and also provide a message that is formatted
using fmt::format() syntax. */
Expand Down
Loading