-
Notifications
You must be signed in to change notification settings - Fork 8
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
DM-9846: Improve handling of error messages #4
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,70 @@ | ||
#include "astshim/base.h" | ||
|
||
#include <sstream> | ||
#include <stdexcept> | ||
#include <string> | ||
#include <vector> | ||
|
||
namespace ast { | ||
namespace { | ||
|
||
static std::ostringstream errorMsgStream; | ||
|
||
/* | ||
Write an error message to `errorMsgStream` | ||
|
||
Intended to be registered as an error handler to AST by calling `astSetPutErr(reportError)`. | ||
*/ | ||
void reportError(int errNum, const char * errMsg) { | ||
errorMsgStream << errMsg; | ||
} | ||
|
||
/* | ||
Instantiate this class to register `reportError` as an AST error handler. | ||
*/ | ||
class ErrorHandler { | ||
public: | ||
ErrorHandler() { | ||
astSetPutErr(reportError); | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is not bound but I have been doing that for most classes. It seems fussy for such a tiny class, but I'll add it. |
||
ErrorHandler(ErrorHandler const &) = delete; | ||
ErrorHandler(ErrorHandler &&) = delete; | ||
ErrorHandler & operator=(ErrorHandler const &) = delete; | ||
ErrorHandler & operator=(ErrorHandler &&) = delete; | ||
|
||
static std::string getErrMsg() { | ||
auto errMsg = errorMsgStream.str(); | ||
// clear status bits | ||
errorMsgStream.clear(); | ||
if (errMsg.empty()) { | ||
errMsg = "Failed with AST status = " + std::to_string(astStatus); | ||
} else { | ||
// empty the stream | ||
errorMsgStream.str(""); | ||
} | ||
astClearStatus; | ||
return errMsg; | ||
} | ||
}; | ||
|
||
} // <anonymous> | ||
|
||
void assertOK(AstObject * rawPtr1, AstObject * rawPtr2) { | ||
// Construct ErrorHandler once, the first time this function is called. | ||
// This is done to initialize `errorMsgStream` and register `reportError` as the AST error handler. | ||
// See https://isocpp.org/wiki/faq/ctors#static-init-order-on-first-use | ||
static ErrorHandler * errHandler = new ErrorHandler(); | ||
if (!astOK) { | ||
if (rawPtr1) { | ||
astAnnul(rawPtr1); | ||
} | ||
if (rawPtr2) { | ||
astAnnul(rawPtr2); | ||
} | ||
throw std::runtime_error(errHandler->getErrMsg()); | ||
} | ||
} | ||
|
||
ConstArray2D arrayFromVector(std::vector<double> const &vec, int nAxes) { | ||
if (vec.size() % nAxes != 0) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did you mean to remove the "longer-term" bit as well?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes. The current code will probably suffice for LSST and it's not clear to me which, if any, additional AST features we will want to add. Much as I would like python doc strings I suspect it is not practical unless we come up with an automated way to add them.