From 1fc9e55bc9df22e647cbbf10ef83b51e1d242f80 Mon Sep 17 00:00:00 2001 From: checktheroads Date: Sun, 18 Oct 2020 12:14:17 -0700 Subject: [PATCH] fix exception printing bug for directly-raised custom exceptions --- hyperglass/exceptions.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/hyperglass/exceptions.py b/hyperglass/exceptions.py index 2ff6e56..105c7fc 100644 --- a/hyperglass/exceptions.py +++ b/hyperglass/exceptions.py @@ -1,6 +1,7 @@ """Custom exceptions for hyperglass.""" # Standard Library +import sys import json as _json from typing import Dict, List, Union, Optional, Sequence @@ -46,7 +47,11 @@ class HyperglassError(Exception): else: log.info(repr(self)) - console.print_exception(extra_lines=6) + if all(sys.exc_info()): + # Rich will raise a ValueError if print_exception() is used + # outside of a try/except block. Only use Rich for traceback + # printing if the exception is caught. + console.print_exception(extra_lines=6) def __str__(self) -> str: """Return the instance's error message."""