aboutsummaryrefslogtreecommitdiff
path: root/src/error-demo/error-demo.cc
blob: 0216092e3609b2a84af7e741cbafc83f5824e800 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#include "logging.hh"
#include "nixexpr.hh"
#include "util.hh"

#include <iostream>
#include <optional>

using namespace nix;

MakeError(DemoError, Error);

int main()
{
    verbosity = lvlVomit;

    // In each program where errors occur, this has to be set.
    ErrorInfo::programName = std::optional("error-demo");

    // 'DemoError' appears as error name.
    try {
        throw DemoError("demo error was thrown");
    } catch (Error &e) {
        logger->logEI(e.info());
    }

    // appending to the hint from the previous error
    try {
        auto e = Error("initial error");
        throw DemoError(e.info());
    } catch (Error &e) {
        ErrorInfo ei = e.info();
        // using normaltxt to avoid the default yellow highlighting.
        ei.hint = hintfmt("%s; subsequent error message.", 
            normaltxt(e.info().hint ? e.info().hint->str() : ""));
        logger->logEI(ei);
    }

    // SysError; picks up errno
    try {
        auto x = readFile(-1);
    }
    catch (SysError &e) {
        std::cout << "errno was: " << e.errNo << std::endl;
        logError(e.info());
    }

    // current exception
    try {
        throw DemoError("DemoError handled as a %1%", "std::exception");
    }
    catch (...) {
        const std::exception_ptr &eptr = std::current_exception();
        try
        {
            std::rethrow_exception(eptr);
        }
        catch (std::exception& e)
        {
            std::cerr << e.what() << std::endl;
        }
    }

    // For completeness sake, show 'info' through 'vomit' levels.
    // But this is maybe a heavy format for those.
    logger->logEI(
        ErrorInfo { .level = lvlInfo,
                    .name = "Info name",
                    .description = "Info description",
        });

    logger->logEI(
        ErrorInfo { .level = lvlTalkative,
                    .name = "Talkative name",
                    .description = "Talkative description",
        });

    logger->logEI(
        ErrorInfo { .level = lvlChatty,
                    .name = "Chatty name",
                    .description = "Chatty description",
        });

    logger->logEI(
        ErrorInfo { .level = lvlDebug,
                    .name = "Debug name",
                    .description = "Debug description",
        });

    logger->logEI(
        ErrorInfo { .level = lvlVomit,
                    .name = "Vomit name",
                    .description = "Vomit description",
        });

    // Error in a program; no hint and no nix code.
    logError(
        ErrorInfo { .name = "name",
                    .description = "error description",
        });

    // Warning with name, description, and hint.
    // The hintfmt function makes all the substituted text yellow.
    logWarning(
        ErrorInfo { .name = "name",
                    .description = "error description",
                    .hint = hintfmt("there was a %1%", "warning"),
        });

    // Warning with nix file, line number, column, and the lines of
    // code where a warning occurred.
    SymbolTable testTable;
    auto problem_file = testTable.create("myfile.nix");

    logWarning(
        ErrorInfo { .name = "warning name",
                    .description = "warning description",
                    .hint = hintfmt("this hint has %1% templated %2%!!",
                        "yellow",
                        "values"),
                    .nixCode = NixCode {
                        .errPos = Pos(problem_file, 40, 13),
                        .prevLineOfCode = std::nullopt,
                        .errLineOfCode = "this is the problem line of code",
                        .nextLineOfCode = std::nullopt
                    }});

    // Error with previous and next lines of code.
    logError(
        ErrorInfo { .name = "error name",
                    .description = "error with code lines",
                    .hint = hintfmt("this hint has %1% templated %2%!!",
                        "yellow",
                        "values"),
                    .nixCode = NixCode {
                        .errPos = Pos(problem_file, 40, 13),
                        .prevLineOfCode = "previous line of code",
                        .errLineOfCode = "this is the problem line of code",
                        .nextLineOfCode = "next line of code",
                    }});


    // Error without lines of code.
    logError(
        ErrorInfo { .name = "error name",
                    .description = "error without any code lines.",
                    .hint = hintfmt("this hint has %1% templated %2%!!",
                        "yellow",
                        "values"),
                    .nixCode = NixCode {
                        .errPos = Pos(problem_file, 40, 13)
                    }});

    // Error with only hint and name..
    logError(
        ErrorInfo { .name = "error name",
                    .hint = hintfmt("hint %1%", "only"),
                    .nixCode = NixCode {
                        .errPos = Pos(problem_file, 40, 13)
                    }});

    return 0;
}