Skip to content

Errors in build.py not logged helpfully in "BUILD FAILED" error message #638

@dsynkov

Description

@dsynkov

Right now, when there is an error in the build.py script itself, the "BUILD FAILED" error message does not provide the full stack trace, nor does the logger provide information about the type of exception or line number. For example:

An AttributeError example in build.py

Given a sample task of:

@task('analyze')
def sample_task():
    import os
    os.getcd()

Where os.getcwd() is replaced with the incorrect os.getcd(), running pyb will print the following error message:

BUILD FAILED - module 'os' has no attribute 'getcd'

A more helpful error message would be:

BUILD FAILED - AttributeError: module 'os' has no attribute 'getcd' (build.py:37)

Where AttributeError is the type of exception, followed by the message, followed by the line number.

An KeyError example in build.py

Given a task of:

@task('analyze')
def sample_task():
    d = {'foo': 1, 'bar': 2}
    print(d['baz'])

The failure message will be:

BUILD FAILED - 'baz'

A more helpful error message would be:

BUILD FAILED - KeyError: 'baz' (build.py:22)

Where KeyError is the type of exception and 22 is the line number.

Potential Solution

Using sys.exc_info() to capture a tuple containing the exception type and traceback can provide the needed functionality to implement this. A sample implementation in the main method of clip.py might look something like:

        except PyBuilderException as e:
            exc_type, exc_obj, exc_tb = sys.exc_info()

            filename = exc_tb.tb_frame.f_code.co_filename
            lineno = exc_tb.tb_lineno

            msg = "{exc_type}: {e} ({filename}:{lineno})".format(
                exc_type=exc_type, e=e, filename=filename, lineno=lineno
            )

            print_build_status(str(msg), options, successful=False)
            return 1

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions