question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

INTERNAL ERROR with arbitrary "<XXX>" strings in message

See original GitHub issue

We are using loguru to capture py.test errors and redirect them to one or several files. So far, we have code similar to the following:

@pytest.fixture(autouse=True)
def init_logger(request, workspace):
    log_path = workspace.logdir / (request.node.name + ".log")

    # 1. log to file with ansi color codes
    logger.add(
            log_path.with_suffix(".color.log"),
            colorize=True)
    # 2. log to file with plain text
    logger.add(log_path, 
            colorize=False)

def pytest_runtest_logreport(report):
    """
    Pytest hook called after a test has completed.
    """

    def get_status(report):
        TestReport = namedtuple('TestReport', ['color', 'status'])

        was_xfail = hasattr(report, "wasxfail")
        if report.passed and not was_xfail:
            return TestReport("green", "PASSED")
        elif report.passed and was_xfail:
            return TestReport("yellow", "PASSED")
        elif report.failed:
            return TestReport("red", "FAILED")
        elif report.skipped:
            return TestReport("yellow", "SKIPPED")
        else:
            raise ValueError("Test report has unknown state")

    if report.when == "call":
        c, s = get_status(report)
        extra = (":\n" + f"{report.longrepr}" if report.failed else "")
        logger.opt(colors=True).info(f"<{c}>TEST {s}</{c}> {extra}")

The problem lies in the last line if extra contains something similar to loguru color tags. In our case, for the offending test py.test produces a failure message that points to the function below, which gets stored in extra:

def test():
    """Check if a file exists using `if [[ -e <file> ]]`."""
   # do stuff

Since py.test shows the function code up until the point of failure, the <file> text in the docstring is wrongly parsed as a color tag by loguru and the following internal error is produced:

INTERNALERROR> ValueError: Tag "<file>" does not corespond to any known ansi directive, make sure you did not misspelled it (or prepend '\' to escape it)

Even if we could escape this particular <file> string to avoid the error, there’s no way to guarantee that a stack dump produced by py.test doesn’t include some other string that might be misinterpreted as a color tag. Is there any way to prevent loguru from raising the internal error and just ignore the “tag” if it’s not recognized as a color?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
Delgancommented, Apr 1, 2020

Great, that’s was easy for once! 😄

I’m closing this issue then. 😉

0reactions
Delgancommented, Apr 3, 2020

You are right, thanks, I added a note about that in the documentation. 😉

Read more comments on GitHub >

github_iconTop Results From Across the Web

Implementing Error handling with a custom error struct and ...
I would like help implementing some error handling on my createTrade function so that I could catch 400 when improper types are passed,...
Read more >
Connection pools being reset with Error: 18056, Severity: 20 ...
I know the error number is different but the failure ID is the same with a number of the messages are identical). Failure...
Read more >
Error: "Internal Error 27555. The system cannot find the path specified"
To work around this problem, create an arbitrary file in the Program folder. The selected components will be uninstalled. If you completely uninstall...
Read more >
Event Type Definitions - Sentry Developer Documentation
A list of strings used to dictate how this event is supposed to be grouped with other events into issues. For more information...
Read more >
doc/app/errmsg - PALM model
PA0= PALM error messages = The following list displays the error ... This also requires to give the file activation string "restart" with ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found