Cleaning up helpers around errors

This commit is contained in:
nishant-sachdeva
2022-09-19 10:51:14 +05:30
parent 1fbee8259a
commit c8011d8719
21 changed files with 271 additions and 231 deletions
-40
View File
@@ -39,34 +39,6 @@ Error::Error(
m_errorId(_errorId),
m_type(_type)
{
switch (m_type)
{
case Type::CodeGenerationError:
m_typeName = "CodeGenerationError";
break;
case Type::DeclarationError:
m_typeName = "DeclarationError";
break;
case Type::DocstringParsingError:
m_typeName = "DocstringParsingError";
break;
case Type::Info:
m_typeName = "Info";
break;
case Type::ParserError:
m_typeName = "ParserError";
break;
case Type::SyntaxError:
m_typeName = "SyntaxError";
break;
case Type::TypeError:
m_typeName = "TypeError";
break;
case Type::Warning:
m_typeName = "Warning";
break;
}
if (_location.isValid())
*this << errinfo_sourceLocation(_location);
if (!_secondaryLocation.infos.empty())
@@ -84,15 +56,3 @@ SecondarySourceLocation const* Error::secondarySourceLocation() const noexcept
{
return boost::get_error_info<errinfo_secondarySourceLocation>(*this);
}
optional<Error::Severity> Error::severityFromString(string _input)
{
boost::algorithm::to_lower(_input);
boost::algorithm::trim(_input);
for (Severity severity: {Severity::Error, Severity::Warning, Severity::Info})
if (_input == formatErrorSeverityLowercase(severity))
return severity;
return nullopt;
}
+51 -26
View File
@@ -31,6 +31,7 @@
#include <boost/preprocessor/cat.hpp>
#include <boost/preprocessor/facilities/empty.hpp>
#include <boost/preprocessor/facilities/overload.hpp>
#include <boost/algorithm/string/case_conv.hpp>
#include <string>
#include <utility>
@@ -174,6 +175,15 @@ public:
ParserError,
TypeError,
SyntaxError,
IOError,
FatalError,
JSONError,
InternalCompilerError,
CompilerError,
Exception,
UnimplementedFeatureError,
YulException,
SMTLogicException,
Warning,
Info
};
@@ -195,7 +205,6 @@ public:
ErrorId errorId() const { return m_errorId; }
Type type() const { return m_type; }
std::string const& typeName() const { return m_typeName; }
SourceLocation const* sourceLocation() const noexcept;
SecondarySourceLocation const* secondarySourceLocation() const noexcept;
@@ -211,11 +220,12 @@ public:
static constexpr Severity errorSeverity(Type _type)
{
if (_type == Type::Info)
return Severity::Info;
if (_type == Type::Warning)
return Severity::Warning;
return Severity::Error;
switch (_type)
{
case Type::Info: return Severity::Info;
case Type::Warning: return Severity::Warning;
default: return Severity::Error;
}
}
static bool isError(Severity _severity)
@@ -238,35 +248,50 @@ public:
static std::string formatErrorSeverity(Severity _severity)
{
if (_severity == Severity::Info)
return "Info";
if (_severity == Severity::Warning)
return "Warning";
solAssert(isError(_severity), "");
return "Error";
switch (_severity)
{
case Severity::Info: return "Info";
case Severity::Warning: return "Warning";
case Severity::Error: return "Error";
}
util::unreachable();
}
static std::string formatErrorType(Type _type)
{
switch (_type)
{
case Type::IOError: return "IOError";
case Type::FatalError: return "FatalError";
case Type::JSONError: return "JSONError";
case Type::InternalCompilerError: return "InternalCompilerError";
case Type::CompilerError: return "CompilerError";
case Type::Exception: return "Exception";
case Type::CodeGenerationError: return "CodeGenerationError";
case Type::DeclarationError: return "DeclarationError";
case Type::DocstringParsingError: return "DocstringParsingError";
case Type::ParserError: return "ParserError";
case Type::SyntaxError: return "SyntaxError";
case Type::TypeError: return "TypeError";
case Type::UnimplementedFeatureError: return "UnimplementedFeatureError";
case Type::YulException: return "YulException";
case Type::SMTLogicException: return "SMTLogicException";
case Type::Warning: return "Warning";
case Type::Info: return "Info";
}
util::unreachable();
}
static std::string formatErrorSeverityLowercase(Severity _severity)
{
switch (_severity)
{
case Severity::Info:
return "info";
case Severity::Warning:
return "warning";
case Severity::Error:
solAssert(isError(_severity), "");
return "error";
}
solAssert(false, "");
std::string severityValue = formatErrorSeverity(_severity);
boost::algorithm::to_lower(severityValue);
return severityValue;
}
static std::optional<Severity> severityFromString(std::string _input);
private:
ErrorId m_errorId;
Type m_type;
std::string m_typeName;
};
}
+3 -4
View File
@@ -30,7 +30,7 @@ using namespace solidity::langutil;
SourceReferenceExtractor::Message SourceReferenceExtractor::extract(
CharStreamProvider const& _charStreamProvider,
util::Exception const& _exception,
string _severity
Error::Type _type
)
{
SourceLocation const* location = boost::get_error_info<errinfo_sourceLocation>(_exception);
@@ -44,7 +44,7 @@ SourceReferenceExtractor::Message SourceReferenceExtractor::extract(
for (auto const& info: secondaryLocation->infos)
secondary.emplace_back(extract(_charStreamProvider, &info.second, info.first));
return Message{std::move(primary), _severity, std::move(secondary), nullopt};
return Message{std::move(primary), _type, std::move(secondary), nullopt};
}
SourceReferenceExtractor::Message SourceReferenceExtractor::extract(
@@ -52,8 +52,7 @@ SourceReferenceExtractor::Message SourceReferenceExtractor::extract(
Error const& _error
)
{
string severity = Error::formatErrorSeverity(Error::errorSeverity(_error.type()));
Message message = extract(_charStreamProvider, _error, severity);
Message message = extract(_charStreamProvider, _error, _error.type());
message.errorId = _error.errorId();
return message;
}
+2 -2
View File
@@ -55,12 +55,12 @@ namespace SourceReferenceExtractor
struct Message
{
SourceReference primary;
std::string severity; // "Error", "Warning", "Info", ...
Error::Type type;
std::vector<SourceReference> secondary;
std::optional<ErrorId> errorId;
};
Message extract(CharStreamProvider const& _charStreamProvider, util::Exception const& _exception, std::string _severity);
Message extract(CharStreamProvider const& _charStreamProvider, util::Exception const& _exception, Error::Type _type);
Message extract(CharStreamProvider const& _charStreamProvider, Error const& _error);
SourceReference extract(CharStreamProvider const& _charStreamProvider, SourceLocation const* _location, std::string message = "");
}
+19 -17
View File
@@ -65,19 +65,18 @@ AnsiColorized SourceReferenceFormatter::frameColored() const
return AnsiColorized(m_stream, m_colored, {BOLD, BLUE});
}
AnsiColorized SourceReferenceFormatter::errorColored(optional<Error::Severity> _severity) const
AnsiColorized SourceReferenceFormatter::errorColored(Error::Severity _severity) const
{
// We used to color messages of any severity as errors so this seems like a good default
// for cases where severity cannot be determined.
char const* textColor = RED;
if (_severity.has_value())
switch (_severity.value())
{
case Error::Severity::Error: textColor = RED; break;
case Error::Severity::Warning: textColor = YELLOW; break;
case Error::Severity::Info: textColor = WHITE; break;
}
switch (_severity)
{
case Error::Severity::Error: textColor = RED; break;
case Error::Severity::Warning: textColor = YELLOW; break;
case Error::Severity::Info: textColor = WHITE; break;
}
return AnsiColorized(m_stream, m_colored, {BOLD, textColor});
}
@@ -176,15 +175,18 @@ void SourceReferenceFormatter::printSourceLocation(SourceReference const& _ref)
}
}
void SourceReferenceFormatter::printExceptionInformation(SourceReferenceExtractor::Message const& _msg)
void SourceReferenceFormatter::printExceptionInformation(SourceReferenceExtractor::Message const& _msg, bool _printFullType)
{
// exception header line
optional<Error::Severity> severity = Error::severityFromString(_msg.severity);
errorColored(severity) << _msg.severity;
if (m_withErrorIds && _msg.errorId.has_value())
errorColored(severity) << " (" << _msg.errorId.value().error << ")";
messageColored() << ": " << _msg.primary.message << '\n';
errorColored(Error::errorSeverity(_msg.type)) << (
_printFullType ?
Error::formatErrorType(_msg.type) :
Error::formatErrorSeverity(Error::errorSeverity(_msg.type))
);
if (m_withErrorIds && _msg.errorId.has_value())
errorColored(Error::errorSeverity(_msg.type)) << " (" << _msg.errorId.value().error << ")";
messageColored() << ": " << _msg.primary.message << '\n';
printSourceLocation(_msg.primary);
for (auto const& secondary: _msg.secondary)
@@ -197,9 +199,9 @@ void SourceReferenceFormatter::printExceptionInformation(SourceReferenceExtracto
m_stream << '\n';
}
void SourceReferenceFormatter::printExceptionInformation(util::Exception const& _exception, std::string const& _severity)
void SourceReferenceFormatter::printExceptionInformation(util::Exception const& _exception, Error::Type _type, bool _printFullType)
{
printExceptionInformation(SourceReferenceExtractor::extract(m_charStreamProvider, _exception, _severity));
printExceptionInformation(SourceReferenceExtractor::extract(m_charStreamProvider, _exception, _type), _printFullType);
}
void SourceReferenceFormatter::printErrorInformation(ErrorList const& _errors)
+8 -7
View File
@@ -51,22 +51,23 @@ public:
/// Prints source location if it is given.
void printSourceLocation(SourceReference const& _ref);
void printExceptionInformation(SourceReferenceExtractor::Message const& _msg);
void printExceptionInformation(util::Exception const& _exception, std::string const& _severity);
void printExceptionInformation(SourceReferenceExtractor::Message const& _msg, bool _printFullType=false);
void printExceptionInformation(util::Exception const& _exception, Error::Type _type, bool _printFullType=false);
void printErrorInformation(langutil::ErrorList const& _errors);
void printErrorInformation(Error const& _error);
static std::string formatExceptionInformation(
util::Exception const& _exception,
std::string const& _name,
Error::Type _type,
CharStreamProvider const& _charStreamProvider,
bool _colored = false,
bool _withErrorIds = false
bool _withErrorIds = false,
bool _printFullType = false
)
{
std::ostringstream errorOutput;
SourceReferenceFormatter formatter(errorOutput, _charStreamProvider, _colored, _withErrorIds);
formatter.printExceptionInformation(_exception, _name);
formatter.printExceptionInformation(_exception, _type, _printFullType);
return errorOutput.str();
}
@@ -77,7 +78,7 @@ public:
{
return formatExceptionInformation(
_error,
Error::formatErrorSeverity(Error::errorSeverity(_error.type())),
_error.type(),
_charStreamProvider
);
}
@@ -87,7 +88,7 @@ public:
private:
util::AnsiColorized normalColored() const;
util::AnsiColorized frameColored() const;
util::AnsiColorized errorColored(std::optional<langutil::Error::Severity> _severity) const;
util::AnsiColorized errorColored(langutil::Error::Severity _severity) const;
util::AnsiColorized messageColored() const;
util::AnsiColorized secondaryColored() const;
util::AnsiColorized highlightColored() const;