lix/src/libutil/error.hh

203 lines
4.9 KiB
C++
Raw Normal View History

#ifndef error_hh
#define error_hh
2020-03-27 16:55:09 +00:00
#include "ansicolor.hh"
#include <string>
#include <optional>
2020-03-24 17:21:35 +00:00
#include <iostream>
2020-03-27 16:03:02 +00:00
#include <iomanip>
2020-04-03 14:48:20 +00:00
#include "types.hh"
2020-03-27 16:55:09 +00:00
2020-04-02 22:02:40 +00:00
namespace nix
{
2020-04-02 22:02:40 +00:00
typedef enum {
2020-04-02 20:25:43 +00:00
elWarning,
elError
} ErrLevel;
2020-04-02 22:02:40 +00:00
class ColumnRange
{
public:
unsigned int start;
unsigned int len;
};
2020-03-24 17:21:35 +00:00
class ErrorInfo;
2020-04-02 22:02:40 +00:00
class ErrLine
{
public:
int lineNumber;
std::optional<ColumnRange> columnRange;
std::optional<string> prevLineOfCode;
string errLineOfCode;
std::optional<string> nextLineOfCode;
2020-03-24 17:21:35 +00:00
};
2020-04-02 22:02:40 +00:00
class NixCode
{
public:
std::optional<string> nixFile;
std::optional<ErrLine> errLine;
2020-03-24 17:21:35 +00:00
};
2020-04-07 01:43:22 +00:00
// ----------------------------------------------------------------
// format for hints. same as fmt, except templated values
// are always in yellow.
2020-03-24 17:21:35 +00:00
template <class T>
2020-04-07 01:43:22 +00:00
class yellowify
{
public:
yellowify(T &s) : value(s) {}
T &value;
};
2020-03-24 17:21:35 +00:00
template <class T>
2020-04-07 01:43:22 +00:00
std::ostream& operator<<(std::ostream &out, const yellowify<T> &y)
{
return out << ANSI_YELLOW << y.value << ANSI_NORMAL;
}
2020-03-24 17:21:35 +00:00
2020-04-07 01:43:22 +00:00
class hintformat
{
public:
hintformat(string format) :fmt(format)
{
fmt.exceptions(boost::io::all_error_bits ^ boost::io::too_many_args_bit);
}
template<class T>
hintformat& operator%(const T &value)
{
fmt % yellowify(value);
return *this;
}
2020-04-07 01:43:22 +00:00
std::string str() const
{
return fmt.str();
}
template <typename U>
friend class AddHint;
private:
format fmt;
};
template<typename... Args>
inline hintformat hintfmt(const std::string & fs, const Args & ... args)
{
hintformat f(fs);
formatHelper(f, args...);
return f;
}
2020-04-07 01:43:22 +00:00
// -------------------------------------------------
// ErrorInfo.
2020-04-02 22:02:40 +00:00
class ErrorInfo
{
public:
ErrLevel level;
string name;
string description;
std::optional<NixCode> nixCode;
std::optional<string> hint;
static std::optional<string> programName;
2020-04-07 01:43:22 +00:00
static ErrorInfo ProgramError(const string &name,
const string &description,
const std::optional<hintformat> &hf);
2020-04-02 22:02:40 +00:00
2020-03-24 17:21:35 +00:00
2020-04-07 01:43:22 +00:00
static ErrorInfo ProgramWarning(const string &name,
const string &description,
const std::optional<hintformat> &hf);
2020-03-24 17:21:35 +00:00
2020-04-07 01:43:22 +00:00
template <class P>
static ErrorInfo NixLangError(const string &name,
const string &description,
const P &pos,
std::optional<string> prevloc,
string loc,
std::optional<string> nextloc,
const std::optional<hintformat> &hf)
2020-04-02 22:02:40 +00:00
{
2020-04-07 01:43:22 +00:00
return NixLangEI(elError, name, description, pos, prevloc, loc, nextloc, hf);
2020-04-02 22:02:40 +00:00
}
2020-04-07 01:43:22 +00:00
template <class P>
static ErrorInfo NixLangWarning(const string &name,
const string &description,
const P &pos,
std::optional<string> prevloc,
string loc,
std::optional<string> nextloc,
const std::optional<hintformat> &hf)
2020-04-02 22:02:40 +00:00
{
2020-04-07 01:43:22 +00:00
return NixLangEI(elWarning, name, description, pos, prevloc, loc, nextloc, hf);
2020-04-02 22:02:40 +00:00
}
2020-03-24 15:18:23 +00:00
2020-04-07 01:43:22 +00:00
private:
template <class P>
static ErrorInfo NixLangEI(ErrLevel level,
const string &name,
const string &description,
const P &pos,
std::optional<string> prevloc,
string loc,
std::optional<string> nextloc,
const std::optional<hintformat> &hf)
2020-04-02 22:02:40 +00:00
{
2020-04-07 01:43:22 +00:00
ErrorInfo ei(level);
ei.name = name;
ei.description = description;
if (hf.has_value())
ei.hint = std::optional<string>(hf->str());
else
ei.hint = std::nullopt;
ErrLine errline;
errline.lineNumber = pos.line;
errline.columnRange = { .start = pos.column, .len = 1 };
errline.prevLineOfCode = prevloc;
errline.errLineOfCode = loc;
errline.nextLineOfCode = nextloc;
NixCode nixcode;
nixcode.nixFile = pos.file;
nixcode.errLine = std::optional(errline);
ei.nixCode = std::optional(nixcode);
return ei;
2020-04-02 22:02:40 +00:00
}
2020-04-07 01:43:22 +00:00
static ErrorInfo ProgramEI(ErrLevel level,
const string &name,
const string &description,
const std::optional<hintformat> &hf);
// constructor is protected, so only the builder classes can create an ErrorInfo.
ErrorInfo(ErrLevel level)
2020-04-02 22:02:40 +00:00
{
2020-04-07 01:43:22 +00:00
this->level = level;
2020-04-02 22:02:40 +00:00
}
2020-03-24 15:18:23 +00:00
};
2020-03-27 16:03:02 +00:00
// --------------------------------------------------------
// error printing
// just to cout for now.
2020-04-07 01:43:22 +00:00
void printErrorInfo(const ErrorInfo &einfo);
2020-03-27 16:03:02 +00:00
}
#endif