Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: print exceptions to stderr (ESPTOOL-731) #913

6 changes: 5 additions & 1 deletion espefuse/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import espefuse.efuse.esp32s3beta2 as esp32s3beta2_efuse

import esptool
from esptool.loader import REDIRECT_ERRORS

DefChip = namedtuple("DefChip", ["chip_name", "efuse_lib", "chip_class"])

Expand Down Expand Up @@ -294,7 +295,10 @@ def _main():
try:
main()
except esptool.FatalError as e:
print("\nA fatal error occurred: %s" % e)
print(
"\nA fatal error occurred: %s" % e,
file=sys.stderr if REDIRECT_ERRORS else sys.stdout,
)
sys.exit(2)


Expand Down
9 changes: 7 additions & 2 deletions espsecure/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import ecdsa

import esptool
from esptool.loader import REDIRECT_ERRORS

SIG_BLOCK_MAGIC = 0xE7

Expand Down Expand Up @@ -1810,15 +1811,19 @@ def _main():
try:
main()
except esptool.FatalError as e:
print("\nA fatal error occurred: %s" % e)
print(
"\nA fatal error occurred: %s" % e,
file=sys.stderr if REDIRECT_ERRORS else sys.stdout,
)
sys.exit(2)
except ValueError as e:
try:
if [arg for arg in e.args if "Could not deserialize key data." in arg]:
print(
"Note: This error originates from the cryptography module. "
"It is likely not a problem with espsecure, "
"please make sure you are using a compatible OpenSSL backend."
"please make sure you are using a compatible OpenSSL backend.",
file=sys.stderr if REDIRECT_ERRORS else sys.stdout,
)
finally:
raise
Expand Down
26 changes: 19 additions & 7 deletions esptool/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,12 @@
write_mem,
)
from esptool.config import load_config_file
from esptool.loader import DEFAULT_CONNECT_ATTEMPTS, ESPLoader, list_ports
from esptool.loader import (
DEFAULT_CONNECT_ATTEMPTS,
ESPLoader,
REDIRECT_ERRORS,
list_ports,
)
from esptool.targets import CHIP_DEFS, CHIP_LIST, ESP32ROM
from esptool.util import (
FatalError,
Expand Down Expand Up @@ -1072,23 +1077,30 @@ def _main():
try:
main()
except FatalError as e:
print(f"\nA fatal error occurred: {e}")
print(
f"\nA fatal error occurred: {e}",
file=sys.stderr if REDIRECT_ERRORS else sys.stdout,
)
sys.exit(2)
except serial.serialutil.SerialException as e:
print(f"\nA serial exception error occurred: {e}")
file = sys.stderr if REDIRECT_ERRORS else sys.stdout
print(f"\nA serial exception error occurred: {e}", file=file)
print(
"Note: This error originates from pySerial. "
"It is likely not a problem with esptool, "
"but with the hardware connection or drivers."
"but with the hardware connection or drivers.",
file=file,
)
print(
"For troubleshooting steps visit: "
"https://docs.espressif.com/projects/esptool/en/latest/troubleshooting.html"
"https://docs.espressif.com/projects/esptool/en/latest/troubleshooting.html", # noqa: E501
file=file,
)
sys.exit(1)
except StopIteration:
print(traceback.format_exc())
print("A fatal error occurred: The chip stopped responding.")
file = sys.stderr if REDIRECT_ERRORS else sys.stdout
print(traceback.format_exc(), file=file)
print("A fatal error occurred: The chip stopped responding.", file=file)
sys.exit(2)


Expand Down
2 changes: 2 additions & 0 deletions esptool/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@
DEFAULT_CONNECT_ATTEMPTS = cfg.getint("connect_attempts", 7)
# Number of times to try writing a data block
WRITE_BLOCK_ATTEMPTS = cfg.getint("write_block_attempts", 3)
# Redirect errors to stderr
REDIRECT_ERRORS = cfg.getboolean("redirect_errors", False)

STUBS_DIR = os.path.join(os.path.dirname(__file__), "targets", "stub_flasher")

Expand Down
5 changes: 4 additions & 1 deletion flasher_stub/esptool_test_stub.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,8 @@
try:
esptool.main()
except esptool.FatalError as e:
print("\nA fatal error occurred: %s" % e)
print(
"\nA fatal error occurred: %s" % e,
file=sys.stderr if globals()["redirect_errors"] else sys.stdout,
)
sys.exit(2)
6 changes: 4 additions & 2 deletions flasher_stub/wrap_stub.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@
import os.path
import sys

sys.path.append("..")
import esptool # noqa: E402
from esptool.loader import REDIRECT_ERRORS

sys.path.append("..")

THIS_DIR = os.path.dirname(__file__)
BUILD_DIR = os.path.join(THIS_DIR, "build")
Expand Down Expand Up @@ -52,7 +54,7 @@ def wrap_stub(elf_file):
stub.get("data_start", 0),
stub["entry"],
),
file=sys.stderr,
file=sys.stderr if REDIRECT_ERRORS else sys.stdout,
)

return stub
Expand Down