2020-04-02 20:08:05 +00:00
|
|
|
#include "../../src/libutil/error.hh"
|
2020-03-23 21:29:49 +00:00
|
|
|
|
|
|
|
#include <iostream>
|
2020-04-02 22:02:40 +00:00
|
|
|
#include <optional>
|
2020-03-23 21:29:49 +00:00
|
|
|
|
2020-04-02 22:02:40 +00:00
|
|
|
int main()
|
2020-03-27 16:03:02 +00:00
|
|
|
{
|
2020-04-02 22:02:40 +00:00
|
|
|
using namespace nix;
|
2020-03-23 21:29:49 +00:00
|
|
|
|
2020-04-01 21:51:14 +00:00
|
|
|
// In each program where errors occur, this has to be set.
|
2020-04-02 03:30:19 +00:00
|
|
|
ErrorInfo::programName = std::optional("error-test");
|
2020-03-25 16:52:03 +00:00
|
|
|
|
2020-04-01 21:51:14 +00:00
|
|
|
// There are currently four error types:
|
2020-04-02 22:02:40 +00:00
|
|
|
//
|
2020-04-01 21:51:14 +00:00
|
|
|
// ProgramError, ProgramWarning, NixLangError, NixLangWarning.
|
2020-04-02 22:02:40 +00:00
|
|
|
//
|
2020-04-01 21:51:14 +00:00
|
|
|
// Each error type is created with a specific sequence of builder functions.
|
|
|
|
// Unlike with a constructor, each parameter is clearly named.
|
2020-04-02 22:02:40 +00:00
|
|
|
// If the sequence of function calls isn't followed, then there's a type
|
|
|
|
// error. This should make for a consistent look in the code when errors are
|
|
|
|
// created.
|
2020-03-31 18:42:41 +00:00
|
|
|
|
2020-04-01 21:51:14 +00:00
|
|
|
// ProgramError takes name, description, and an optional hint.
|
2020-04-02 22:02:40 +00:00
|
|
|
printErrorInfo( ProgramError()
|
|
|
|
.name("name")
|
|
|
|
.description("error description")
|
|
|
|
.nohint()
|
|
|
|
);
|
2020-03-31 18:42:41 +00:00
|
|
|
|
2020-04-01 21:51:14 +00:00
|
|
|
// ProgramWarning takes name, description, and an optional hint.
|
2020-04-02 22:02:40 +00:00
|
|
|
// The hint is in the form of a hintfmt class, which wraps boost::format(),
|
|
|
|
// and makes all the substituted text yellow.
|
|
|
|
printErrorInfo( ProgramWarning()
|
|
|
|
.name("warning name")
|
|
|
|
.description("warning description")
|
|
|
|
.hint(hintfmt("there was a %1%") %
|
|
|
|
"warning") // 'warning' will be yellow.
|
|
|
|
);
|
2020-03-31 18:42:41 +00:00
|
|
|
|
2020-04-01 21:51:14 +00:00
|
|
|
/*
|
|
|
|
// some invalid errors:
|
2020-04-02 22:02:40 +00:00
|
|
|
|
2020-04-01 21:51:14 +00:00
|
|
|
// type error: no hint function.
|
|
|
|
ProgramError()
|
|
|
|
.name("name")
|
|
|
|
.description("error description");
|
2020-04-01 00:29:41 +00:00
|
|
|
|
2020-04-01 21:51:14 +00:00
|
|
|
// type error: description before name.
|
|
|
|
ProgramError()
|
|
|
|
.description("error description")
|
|
|
|
.name("name")
|
|
|
|
.nohint();
|
2020-04-01 00:29:41 +00:00
|
|
|
|
2020-04-02 22:02:40 +00:00
|
|
|
// type error: hint function with regular boost format, not special
|
|
|
|
hintfmt. ProgramError() .description("error description") .name("name")
|
2020-04-01 21:51:14 +00:00
|
|
|
.hint(format("there was a %1%") % "warning");
|
|
|
|
*/
|
2020-04-01 00:29:41 +00:00
|
|
|
|
2020-04-02 22:02:40 +00:00
|
|
|
// NixLangWarning adds nix file, line number, column range, and the lines of
|
|
|
|
// code where a warning occurred.
|
|
|
|
printErrorInfo(NixLangWarning()
|
|
|
|
.name("warning name")
|
|
|
|
.description("warning description")
|
|
|
|
.nixFile("myfile.nix")
|
|
|
|
.lineNumber(40)
|
|
|
|
.columnRange(13, 7)
|
|
|
|
.linesOfCode(std::nullopt,
|
|
|
|
"this is the problem line of code",
|
|
|
|
std::nullopt)
|
|
|
|
.hint(hintfmt("this hint has %1% templated %2%!!") %
|
|
|
|
"yellow" % "values"));
|
2020-03-31 18:42:41 +00:00
|
|
|
|
2020-04-02 22:02:40 +00:00
|
|
|
// NixLangError is just the same as NixLangWarning, except for the Error
|
|
|
|
// flag.
|
|
|
|
printErrorInfo(NixLangError()
|
|
|
|
.name("error name")
|
|
|
|
.description("error description")
|
|
|
|
.nixFile("myfile.nix")
|
|
|
|
.lineNumber(40)
|
|
|
|
.columnRange(13, 7)
|
|
|
|
.linesOfCode(std::optional("previous line of code"),
|
|
|
|
"this is the problem line of code",
|
|
|
|
std::optional("next line of code"))
|
|
|
|
.hint(hintfmt("this hint has %1% templated %2%!!") %
|
|
|
|
"yellow" % "values"));
|
2020-03-24 15:18:23 +00:00
|
|
|
|
2020-04-01 21:51:14 +00:00
|
|
|
return 0;
|
2020-03-23 21:29:49 +00:00
|
|
|
}
|