lix/src/libutil/error.hh

83 lines
1.3 KiB
C++
Raw Normal View History

#pragma once
#include "types.hh"
#include <string>
#include <optional>
using std::string;
using std::optional;
namespace nix {
enum ErrLevel
{ elWarning
, elError
};
class ColumnRange {
public:
unsigned int start;
unsigned int len;
};
class ErrLine {
public:
int lineNumber;
optional<ColumnRange> columnRange;
optional<string> prevLineOfCode;
string errLineOfCode;
optional<string> nextLineOfCode;
};
class NixCode {
public:
optional<string> nixFile;
optional<ErrLine> errLine;
};
class ErrorInfo {
public:
ErrLevel level;
2020-03-24 15:18:23 +00:00
string name;
string description;
2020-03-24 15:18:23 +00:00
string program;
optional<NixCode> nixCode;
string hint;
2020-03-24 15:18:23 +00:00
ErrorInfo& GetEI() { return *this; }
};
2020-03-24 15:18:23 +00:00
template <class T>
class AddName : private T
{
public:
T& name(const std::string &name){
GetEI().name = name;
return *this;
}
protected:
ErrorInfo& GetEI() { return T::GetEI(); }
};
template <class T>
class AddDescription : private T
{
public:
T& description(const std::string &description){
GetEI().description = description;
return *this;
}
protected:
ErrorInfo& GetEI() { return T::GetEI(); }
};
typedef AddName<AddDescription<ErrorInfo>> StandardError;
string showErrLine(ErrLine &errLine);
void print_code_lines(string &prefix, NixCode &nix_code);
void print_error(ErrorInfo &einfo);
}