Skip to content

Commit

Permalink
fix: take into account verbosity when showing SolcError [APE-1558] (#126
Browse files Browse the repository at this point in the history
)
  • Loading branch information
antazoey authored Nov 22, 2023
1 parent c11e583 commit 669fa22
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 2 deletions.
5 changes: 3 additions & 2 deletions ape_solidity/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
IncorrectMappingFormatError,
RuntimeErrorType,
RuntimeErrorUnion,
SolcCompileError,
SolcInstallError,
)

Expand Down Expand Up @@ -475,7 +476,7 @@ def compile(
try:
output = compile_standard(input_json, **arguments)
except SolcError as err:
raise CompilerError(str(err)) from err
raise SolcCompileError(err) from err

contracts = output.get("contracts", {})
input_contract_names: List[str] = []
Expand Down Expand Up @@ -592,7 +593,7 @@ def compile_code(
allow_empty=True,
)
except SolcError as err:
raise CompilerError(str(err)) from err
raise SolcCompileError(err) from err

output = result[next(iter(result.keys()))]
return ContractType(
Expand Down
22 changes: 22 additions & 0 deletions ape_solidity/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
from typing import Dict, Type, Union

from ape.exceptions import CompilerError, ConfigError, ContractLogicError
from ape.logging import LogLevel, logger
from solcx.exceptions import SolcError


class SolcInstallError(CompilerError):
Expand All @@ -16,6 +18,26 @@ def __init__(self):
)


class SolcCompileError(CompilerError):
"""
Specifically, only errors arising from ``solc`` compile methods.
This error is a modified version of ``SolcError`` to take into
account Ape's logging verbosity.
"""

def __init__(self, solc_error: SolcError):
self.solc_error = solc_error

def __str__(self) -> str:
if logger.level <= LogLevel.DEBUG:
# Show everything when in DEBUG mode.
return str(self.solc_error)

else:
# Only show the error and line-number(s) where it occurred.
return self.solc_error.message


class IncorrectMappingFormatError(ConfigError, ValueError):
def __init__(self):
super().__init__(
Expand Down
44 changes: 44 additions & 0 deletions tests/test_exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import pytest
from ape.logging import logger
from solcx.exceptions import SolcError

from ape_solidity.exceptions import SolcCompileError

MESSAGE = "__message__"
COMMAND = ["solc", "command"]
RETURN_CODE = 123
STDOUT_DATA = "<stdout data>"
STDERR_DATA = "<stderr data>"


@pytest.fixture(scope="module")
def solc_error():
return SolcError(
message=MESSAGE,
command=COMMAND,
return_code=RETURN_CODE,
stdout_data=STDOUT_DATA,
stderr_data=STDERR_DATA,
)


def test_solc_compile_error(solc_error):
error = SolcCompileError(solc_error)
actual = str(error)
assert MESSAGE in actual
assert f"{RETURN_CODE}" not in actual
assert " ".join(COMMAND) not in actual
assert STDOUT_DATA not in actual
assert STDERR_DATA not in actual


def test_solc_compile_error_verbose(solc_error):
logger.set_level("DEBUG")
error = SolcCompileError(solc_error)
actual = str(error)
assert MESSAGE in actual
assert f"{RETURN_CODE}" in actual
assert " ".join(COMMAND) in actual
assert STDOUT_DATA in actual
assert STDERR_DATA in actual
logger.set_level("INFO")

0 comments on commit 669fa22

Please sign in to comment.