better exception message in some cases

This commit is contained in:
VaclavT 2021-11-07 12:24:00 +01:00
parent c8c25e9952
commit a5a0029341
2 changed files with 7 additions and 2 deletions

7
ml.cpp
View File

@ -636,6 +636,10 @@ std::string MlError::description() const {
return MlPerfMon::instance().callstack() + "error: the expression `" + cause->debug() + "` with message \"" + msg + "\""; return MlPerfMon::instance().callstack() + "error: the expression `" + cause->debug() + "` with message \"" + msg + "\"";
} }
const char * MlError::what() const noexcept {
return msg;
}
void MlEnvironment::combine(MlEnvironment const &other) { void MlEnvironment::combine(MlEnvironment const &other) {
// Normally, I would use the `insert` method of the `map` class, // Normally, I would use the `insert` method of the `map` class,
// but it doesn't overwrite previously declared values for keys. // but it doesn't overwrite previously declared values for keys.
@ -1844,8 +1848,7 @@ MlValue sprintf(std::vector<MlValue> args, MlEnvironment &env) {
if (args.size() < 1 || args.size() > 2) if (args.size() < 1 || args.size() > 2)
throw MlError(MlValue("sprintf", sprintf), env, args.size() > 2 ? TOO_MANY_ARGS : TOO_FEW_ARGS); throw MlError(MlValue("sprintf", sprintf), env, args.size() > 2 ? TOO_MANY_ARGS : TOO_FEW_ARGS);
return MlValue::string( return MlValue::string(mini_sprintf(args[0].as_string(), args.size() == 2 ? args[1].as_list() : std::vector<MlValue>{}));
mini_sprintf(args[0].as_string(), args.size() == 2 ? args[1].as_list() : std::vector<MlValue>{}));
} }
// >>> (map (lambda (x) (+ x 10)) '(1 2 3 4 5 6)) // >>> (map (lambda (x) (+ x 10)) '(1 2 3 4 5 6))

2
ml.h
View File

@ -81,6 +81,8 @@ public:
// Get the printable error description. // Get the printable error description.
std::string description() const; std::string description() const;
const char * what() const noexcept override;
private: private:
MlValue *cause; MlValue *cause;
MlEnvironment env; MlEnvironment env;