utils: some more convenience for error/result
This commit is contained in:
@ -41,6 +41,7 @@ struct Error final : public std::exception {
|
|||||||
Query,
|
Query,
|
||||||
SchemaMismatch,
|
SchemaMismatch,
|
||||||
Store,
|
Store,
|
||||||
|
AssertionFailure
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -50,6 +51,7 @@ struct Error final : public std::exception {
|
|||||||
* #param msgarg the error diecription
|
* #param msgarg the error diecription
|
||||||
*/
|
*/
|
||||||
Error(Code codearg, const std::string& msgarg) : code_{codearg}, what_{msgarg} {}
|
Error(Code codearg, const std::string& msgarg) : code_{codearg}, what_{msgarg} {}
|
||||||
|
Error(Code codearg, std::string&& msgarg) : code_{codearg}, what_{std::move(msgarg)} {}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Build an error from an error-code and a format string
|
* Build an error from an error-code and a format string
|
||||||
|
|||||||
@ -76,7 +76,29 @@ Err(const Error& err)
|
|||||||
return tl::unexpected(err);
|
return tl::unexpected(err);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
* convenience
|
||||||
|
*/
|
||||||
|
|
||||||
|
static inline tl::unexpected<Error>
|
||||||
|
Err(Error::Code errcode, std::string&& msg="")
|
||||||
|
{
|
||||||
|
return Err(Error{errcode, std::move(msg)});
|
||||||
|
}
|
||||||
|
|
||||||
|
__attribute__((format(printf, 2, 0)))
|
||||||
|
static inline tl::unexpected<Error>
|
||||||
|
Err(Error::Code errcode, const char* frm, ...)
|
||||||
|
{
|
||||||
|
va_list args;
|
||||||
|
va_start(args, frm);
|
||||||
|
auto str{vformat(frm, args)};
|
||||||
|
va_end(args);
|
||||||
|
|
||||||
|
return Err(errcode, std::move(str));
|
||||||
|
}
|
||||||
|
|
||||||
}// namespace Mu
|
}// namespace Mu
|
||||||
|
|
||||||
|
|
||||||
#endif /* MU_RESULT_HH__ */
|
#endif /* MU_RESULT_HH__ */
|
||||||
|
|||||||
Reference in New Issue
Block a user