mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Support proper error reporting in StandardCompiler
This commit is contained in:
parent
b3c6b1e936
commit
f25efceaf8
@ -58,6 +58,37 @@ Json::Value formatFatalError(string const& _type, string const& _message)
|
|||||||
return output;
|
return output;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Json::Value formatErrorWithException(
|
||||||
|
Exception const& _exception,
|
||||||
|
bool const& _warning,
|
||||||
|
string const& _type,
|
||||||
|
string const& _component,
|
||||||
|
string const& _message,
|
||||||
|
function<Scanner const&(string const&)> const& _scannerFromSourceName
|
||||||
|
)
|
||||||
|
{
|
||||||
|
string message;
|
||||||
|
string formattedMessage = SourceReferenceFormatter::formatExceptionInformation(_exception, _message, _scannerFromSourceName);
|
||||||
|
|
||||||
|
// NOTE: the below is partially a copy from SourceReferenceFormatter
|
||||||
|
SourceLocation const* location = boost::get_error_info<errinfo_sourceLocation>(_exception);
|
||||||
|
|
||||||
|
if (string const* description = boost::get_error_info<errinfo_comment>(_exception))
|
||||||
|
message = ((_message.length() > 0) ? (_message + ":") : "") + *description;
|
||||||
|
else
|
||||||
|
message = _message;
|
||||||
|
|
||||||
|
if (location && location->sourceName)
|
||||||
|
{
|
||||||
|
Json::Value sourceLocation = Json::objectValue;
|
||||||
|
sourceLocation["file"] = *location->sourceName;
|
||||||
|
sourceLocation["start"] = location->start;
|
||||||
|
sourceLocation["end"] = location->end;
|
||||||
|
}
|
||||||
|
|
||||||
|
return formatError(_warning, _type, _component, message, formattedMessage, location);
|
||||||
|
}
|
||||||
|
|
||||||
StringMap createSourceList(Json::Value const& _input)
|
StringMap createSourceList(Json::Value const& _input)
|
||||||
{
|
{
|
||||||
StringMap sources;
|
StringMap sources;
|
||||||
@ -102,50 +133,105 @@ Json::Value StandardCompiler::compileInternal(Json::Value const& _input)
|
|||||||
|
|
||||||
auto scannerFromSourceName = [&](string const& _sourceName) -> solidity::Scanner const& { return m_compilerStack.scanner(_sourceName); };
|
auto scannerFromSourceName = [&](string const& _sourceName) -> solidity::Scanner const& { return m_compilerStack.scanner(_sourceName); };
|
||||||
|
|
||||||
|
Json::Value errors = Json::arrayValue;
|
||||||
|
bool success = false;
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
// @TODO check return value and parse errors
|
success = m_compilerStack.compile(optimize, optimizeRuns, libraries);
|
||||||
m_compilerStack.compile(optimize, optimizeRuns, libraries);
|
|
||||||
|
for (auto const& error: m_compilerStack.errors())
|
||||||
|
{
|
||||||
|
auto err = dynamic_pointer_cast<Error const>(error);
|
||||||
|
|
||||||
|
errors.append(formatErrorWithException(
|
||||||
|
*error,
|
||||||
|
err->type() == Error::Type::Warning,
|
||||||
|
err->typeName(),
|
||||||
|
"general",
|
||||||
|
"",
|
||||||
|
scannerFromSourceName
|
||||||
|
));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
catch (Error const& _error)
|
catch (Error const& _error)
|
||||||
{
|
{
|
||||||
if (_error.type() == Error::Type::DocstringParsingError)
|
if (_error.type() == Error::Type::DocstringParsingError)
|
||||||
cerr << "Documentation parsing error: " << *boost::get_error_info<errinfo_comment>(_error) << endl;
|
errors.append(formatError(
|
||||||
|
false,
|
||||||
|
"DocstringParsingError",
|
||||||
|
"general",
|
||||||
|
"Documentation parsing error: " + *boost::get_error_info<errinfo_comment>(_error)
|
||||||
|
));
|
||||||
else
|
else
|
||||||
SourceReferenceFormatter::printExceptionInformation(cerr, _error, _error.typeName(), scannerFromSourceName);
|
errors.append(formatErrorWithException(
|
||||||
|
_error,
|
||||||
return Json::Value();
|
false,
|
||||||
|
_error.typeName(),
|
||||||
|
"general",
|
||||||
|
"",
|
||||||
|
scannerFromSourceName
|
||||||
|
));
|
||||||
}
|
}
|
||||||
catch (CompilerError const& _exception)
|
catch (CompilerError const& _exception)
|
||||||
{
|
{
|
||||||
SourceReferenceFormatter::printExceptionInformation(cerr, _exception, "Compiler error", scannerFromSourceName);
|
errors.append(formatErrorWithException(
|
||||||
return Json::Value();
|
_exception,
|
||||||
|
false,
|
||||||
|
"CompilerError",
|
||||||
|
"general",
|
||||||
|
"Compiler error (" + _exception.lineInfo() + ")",
|
||||||
|
scannerFromSourceName
|
||||||
|
));
|
||||||
}
|
}
|
||||||
catch (InternalCompilerError const& _exception)
|
catch (InternalCompilerError const& _exception)
|
||||||
{
|
{
|
||||||
cerr << "Internal compiler error during compilation:" << endl
|
errors.append(formatErrorWithException(
|
||||||
<< boost::diagnostic_information(_exception);
|
_exception,
|
||||||
return Json::Value();
|
false,
|
||||||
|
"InternalCompilerError",
|
||||||
|
"general",
|
||||||
|
"Internal compiler error (" + _exception.lineInfo() + ")", scannerFromSourceName
|
||||||
|
));
|
||||||
}
|
}
|
||||||
catch (UnimplementedFeatureError const& _exception)
|
catch (UnimplementedFeatureError const& _exception)
|
||||||
{
|
{
|
||||||
cerr << "Unimplemented feature:" << endl
|
errors.append(formatErrorWithException(
|
||||||
<< boost::diagnostic_information(_exception);
|
_exception,
|
||||||
return Json::Value();
|
false,
|
||||||
|
"UnimplementedFeatureError",
|
||||||
|
"general",
|
||||||
|
"Unimplemented feature (" + _exception.lineInfo() + ")",
|
||||||
|
scannerFromSourceName));
|
||||||
}
|
}
|
||||||
catch (Exception const& _exception)
|
catch (Exception const& _exception)
|
||||||
{
|
{
|
||||||
cerr << "Exception during compilation: " << boost::diagnostic_information(_exception) << endl;
|
errors.append(formatError(
|
||||||
return Json::Value();
|
false,
|
||||||
|
"Exception",
|
||||||
|
"general",
|
||||||
|
"Exception during compilation: " + boost::diagnostic_information(_exception)
|
||||||
|
));
|
||||||
}
|
}
|
||||||
catch (...)
|
catch (...)
|
||||||
{
|
{
|
||||||
cerr << "Unknown exception during compilation." << endl;
|
errors.append(formatError(
|
||||||
return Json::Value();
|
false,
|
||||||
|
"Exception",
|
||||||
|
"general",
|
||||||
|
"Unknown exception during compilation."
|
||||||
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
Json::Value output = Json::objectValue;
|
Json::Value output = Json::objectValue;
|
||||||
|
|
||||||
|
if (errors.size() > 0)
|
||||||
|
output["errors"] = errors;
|
||||||
|
|
||||||
|
/// Inconsistent state - stop here to receive error reports from users
|
||||||
|
if (!success && (errors.size() == 0))
|
||||||
|
return formatFatalError("InternalCompilerError", "No error reported, but compilation failed.");
|
||||||
|
|
||||||
output["sources"] = Json::objectValue;
|
output["sources"] = Json::objectValue;
|
||||||
unsigned sourceIndex = 0;
|
unsigned sourceIndex = 0;
|
||||||
for (auto const& source: m_compilerStack.sourceNames())
|
for (auto const& source: m_compilerStack.sourceNames())
|
||||||
|
Loading…
Reference in New Issue
Block a user