aboutsummaryrefslogtreecommitdiff
path: root/src/error-demo
diff options
context:
space:
mode:
Diffstat (limited to 'src/error-demo')
-rw-r--r--src/error-demo/error-demo.cc87
-rw-r--r--src/error-demo/local.mk10
2 files changed, 97 insertions, 0 deletions
diff --git a/src/error-demo/error-demo.cc b/src/error-demo/error-demo.cc
new file mode 100644
index 000000000..8fb20d38c
--- /dev/null
+++ b/src/error-demo/error-demo.cc
@@ -0,0 +1,87 @@
+#include "error.hh"
+#include "nixexpr.hh"
+
+#include <iostream>
+#include <optional>
+
+int main()
+{
+ using namespace nix;
+
+ // In each program where errors occur, this has to be set.
+ ErrorInfo::programName = std::optional("error-demo");
+
+ // There are currently four error types:
+ //
+ // ProgramError, ProgramWarning, NixLangError, NixLangWarning.
+ //
+ // Each error type is created with a specific sequence of builder functions.
+ // Unlike with a constructor, each parameter is clearly named.
+ // 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.
+
+ // ProgramError takes name, description, and an optional hint.
+ printErrorInfo( ProgramError()
+ .name("name")
+ .description("error description")
+ .nohint()
+ );
+
+ // ProgramWarning takes name, description, and an optional hint.
+ // 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")
+ // the templated value, 'warning', is automatically colored yellow.
+ .hint(hintfmt("there was a %1%", "warning"))
+ );
+
+ /*
+ // some invalid errors:
+
+ // type error: no hint function.
+ ProgramError()
+ .name("name")
+ .description("error description");
+
+ // type error: description before name.
+ ProgramError()
+ .description("error description")
+ .name("name")
+ .nohint();
+
+ // type error: hint function with regular boost format, not special
+ hintfmt. ProgramError() .description("error description") .name("name")
+ .hint(format("there was a %1%") % "warning");
+ */
+
+ // NixLangWarning adds nix file, line number, column range, and the lines of
+ // code where a warning occurred.
+
+ SymbolTable testTable;
+ auto problem_symbol = testTable.create("problem");
+
+ printErrorInfo(NixLangWarning()
+ .name("warning name")
+ .description("warning description")
+ .pos(Pos(problem_symbol, 40, 13))
+ .linesOfCode(std::nullopt,
+ "this is the problem line of code",
+ std::nullopt)
+ .hint(hintfmt("this hint has %1% templated %2%!!", "yellow" , "values")));
+
+ // NixLangError is just the same as NixLangWarning, except for the Error
+ // flag.
+ printErrorInfo(NixLangError()
+ .name("error name")
+ .description("error description")
+ .pos(Pos(problem_symbol, 40, 13))
+ .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")));
+
+ return 0;
+}
diff --git a/src/error-demo/local.mk b/src/error-demo/local.mk
new file mode 100644
index 000000000..121cab9e3
--- /dev/null
+++ b/src/error-demo/local.mk
@@ -0,0 +1,10 @@
+programs += error-demo
+
+error-demo_DIR := $(d)
+
+error-demo_SOURCES := \
+ $(wildcard $(d)/*.cc) \
+
+error-demo_LIBS = libutil
+
+error-demo_LDFLAGS = -pthread $(SODIUM_LIBS) $(EDITLINE_LIBS) $(BOOST_LDFLAGS) -lboost_context -lboost_thread -lboost_system