mirror of
https://github.com/NixOS/nix
synced 2025-06-29 19:03:16 +02:00
add ErrorInfo to BaseError
This commit is contained in:
parent
15e9564fd1
commit
d3052197fe
6 changed files with 192 additions and 186 deletions
|
@ -1,122 +0,0 @@
|
|||
#ifndef error_hh
|
||||
#define error_hh
|
||||
|
||||
#include "ansicolor.hh"
|
||||
#include <string>
|
||||
#include <optional>
|
||||
#include <iostream>
|
||||
#include "types.hh"
|
||||
|
||||
namespace nix
|
||||
{
|
||||
|
||||
typedef enum {
|
||||
lvlError = 0,
|
||||
lvlWarn,
|
||||
lvlInfo,
|
||||
lvlTalkative,
|
||||
lvlChatty,
|
||||
lvlDebug,
|
||||
lvlVomit
|
||||
} Verbosity;
|
||||
|
||||
struct ErrPos
|
||||
{
|
||||
int line;
|
||||
int column;
|
||||
string file;
|
||||
|
||||
template <class P>
|
||||
ErrPos& operator=(const P &pos)
|
||||
{
|
||||
line = pos.line;
|
||||
column = pos.column;
|
||||
file = pos.file;
|
||||
return *this;
|
||||
}
|
||||
|
||||
template <class P>
|
||||
ErrPos(const P &p)
|
||||
{
|
||||
*this = p;
|
||||
}
|
||||
};
|
||||
|
||||
struct NixCode
|
||||
{
|
||||
ErrPos errPos;
|
||||
std::optional<string> prevLineOfCode;
|
||||
string errLineOfCode;
|
||||
std::optional<string> nextLineOfCode;
|
||||
};
|
||||
|
||||
// ----------------------------------------------------------------
|
||||
// format function for hints. same as fmt, except templated values
|
||||
// are always in yellow.
|
||||
|
||||
template <class T>
|
||||
struct yellowify
|
||||
{
|
||||
yellowify(T &s) : value(s) {}
|
||||
T &value;
|
||||
};
|
||||
|
||||
template <class T>
|
||||
std::ostream& operator<<(std::ostream &out, const yellowify<T> &y)
|
||||
{
|
||||
return out << ANSI_YELLOW << y.value << ANSI_NORMAL;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
std::string str() const
|
||||
{
|
||||
return fmt.str();
|
||||
}
|
||||
|
||||
template <typename U>
|
||||
friend class AddHint;
|
||||
private:
|
||||
format fmt;
|
||||
};
|
||||
|
||||
std::ostream& operator<<(std::ostream &os, const hintformat &hf);
|
||||
|
||||
template<typename... Args>
|
||||
inline hintformat hintfmt(const std::string & fs, const Args & ... args)
|
||||
{
|
||||
hintformat f(fs);
|
||||
formatHelper(f, args...);
|
||||
return f;
|
||||
}
|
||||
|
||||
// -------------------------------------------------
|
||||
// ErrorInfo.
|
||||
struct ErrorInfo
|
||||
{
|
||||
Verbosity level;
|
||||
string name;
|
||||
string description;
|
||||
std::optional<hintformat> hint;
|
||||
std::optional<NixCode> nixCode;
|
||||
|
||||
static std::optional<string> programName;
|
||||
};
|
||||
|
||||
std::ostream& operator<<(std::ostream &out, const ErrorInfo &einfo);
|
||||
|
||||
}
|
||||
|
||||
#endif
|
Loading…
Add table
Add a link
Reference in a new issue