示例#1
0
def _serializable_error_info_from_tb(
        tb: traceback.TracebackException) -> SerializableErrorInfo:
    return SerializableErrorInfo(
        # usually one entry, multiple lines for SyntaxError
        "".join(list(tb.format_exception_only())),
        tb.stack.format(),
        tb.exc_type.__name__ if tb.exc_type is not None else None,
        _serializable_error_info_from_tb(tb.__cause__)
        if tb.__cause__ else None,
    )
示例#2
0
 def exec(self):
     command = getattr(self, '_command_{}'.format(self.args.command))
     logging.info('Starting "{}" command...'.format(self.args.command))
     try:
         command()
     except Exception as e:
         tbe = TracebackException.from_exception(e)
         logging.critical(' '.join(list(tbe.format())))
         raise e
     logging.info('All done - terminating')
示例#3
0
def format_exception(e):
    """
    Convert an exception to a human-readable message and stacktrace
    """
    tb = TracebackException.from_exception(e)
    stack = []
    for raw in tb.format():
        stack += [s.strip() for s in raw.split("\n")]
    msg = "(%s) %s" % ((type(e).__name__, e))
    trace = filter(lambda s: s, map(indent, [s for s in stack[:-1]]))
    return "{}\n".format(msg) + "\n".join(trace)
示例#4
0
文件: ticket.py 项目: MarkDin/ticket
def main():
    x = XiHuSportsReserceTicker(14, 22, 1, [0])
    global message
    message = []
    try:
        x.run()
    except Exception:
        t = TracebackException(*sys.exc_info(), limit=None).format(chain=True)
        msg = ''.join(t)
        print(msg)
        send_email(msg, type_=0)
示例#5
0
async def handle(context: Optional[Context], exception: BaseException):
    if isinstance(exception, UsageException):
        await exception.notice()
        return

    if isinstance(exception.__cause__, UsageException):
        await exception.__cause__.notice()
        return

    if exception.__cause__ is not None:
        trace = TracebackException.from_exception(exception.__cause__)
    else:
        trace = TracebackException.from_exception(exception)

    stack = "".join(trace.format())
    if context is not None:
        await context.send("There was an error during your command :worried:")
        logger.error('Command: "%s"\n%s', context.message.content, stack)
        return

    logger.error("Command: <unkown>\n%s", stack)
示例#6
0
 async def _generate_log(error_id: str, request: Request,
                         exc: Exception) -> None:
     traceback_exception = TracebackException.from_exception(exc)
     logger.error(msg=str(exc),
                  extra={
                      'uuid': error_id,
                      'status_code': HTTP_500_INTERNAL_SERVER_ERROR,
                      'request.method': request.method,
                      'request.path': request.url.path,
                      'request.headers': dict(request.headers),
                      'traceback': ''.join(traceback_exception.format())
                  })
示例#7
0
def error_trace(e):
    # from traceback.print_exc
    limit = None
    chain = True
    tb = e.__traceback__
    value = e
    etype = type(e)
    out = []
    for line in TracebackException(type(value), value, tb,
                                   limit=limit).format(chain=chain):
        out += [line.strip("\n")]
    return '; '.join(out[-2:])
示例#8
0
    def __str__(self):
        from traceback import (
            TracebackException,
            format_exception_only,
            format_list,
        )

        # Move the cause to a non-private attribute
        self.cause = self.__cause__

        # Suppress automatic printing of the cause
        self.__cause__ = None

        info = None  # Information about the call that triggered the exception
        frames = []  # Frames for an abbreviated stacktrace
        dask_internal = True  # Flag if the frame is internal to dask

        # Iterate over frames from the base of the stack
        tb = TracebackException.from_exception(self.cause, capture_locals=True)
        for frame in tb.stack:
            if frame.name == 'execute_task':
                # Current frame is the dask internal call to execute a task

                # Retrieve information about the key/task that triggered the
                # exception. These are not the raw values of variables, but
                # their string repr().
                info = {name: frame.locals[name] for name in ('key', 'task')}

                # Remaining frames are related to the exception
                dask_internal = False

            if not dask_internal:
                # Don't display the locals when printing the traceback
                frame.locals = None

                # Store the frame for printing the traceback
                frames.append(frame)

        # Assemble the exception printout

        # Reporter information for debugging
        lines = [
            'when computing {key}, using\n\n{task}\n\n'.format(**info),
            'Use Reporter.describe(...) to trace the computation.\n\n',
            'Computation traceback:\n',
        ]
        # Traceback; omitting a few dask internal calls below execute_task
        lines.extend(format_list(frames[3:]))
        # Type and message of the original exception
        lines.extend(format_exception_only(self.cause.__class__, self.cause))

        return ''.join(lines)
 def when_calling_endpoint(self):
     method = self.request_data['method'].lower()
     path = self.request_data['endpoint']
     data = self.request_data.get('body')
     api = getattr(self._client, method)
     try:
         self.actual_response = api(
             path=path,
             data=json_stringify(data),
             content_type='application/json',
         )
     except Exception as e:
         self.actual_exception = TracebackException.from_exception(e)
示例#10
0
文件: utils.py 项目: danfossi/FQM
def log_error(error):
    ''' Utility to log error to `errors.txt` file.

    Parameters
    ----------
        error: Error instance
            error that we want to log.
    '''
    log_file = absolute_path('errors.log')

    not os.path.isfile(log_file) and os.system(f'touch {log_file}')
    with open(log_file, 'a') as file:
        file.write(f'{"#" * 5} {datetime.now()} {"#" * 5}\n')
        file.write(''.join(TracebackException.from_exception(error).format()))
示例#11
0
    def __init__(self,
                 exc_obj: BaseException,
                 exc_tb: TracebackType,
                 varsSoFar={},
                 execTime=0):
        # skip arepl traceback - the user should just see their own error
        exc_tb = exc_tb.tb_next

        self.traceback_exception = TracebackException(type(exc_obj), exc_obj,
                                                      exc_tb)
        self.friendly_message = "".join(self.traceback_exception.format())
        self.varsSoFar = pickle_user_vars(varsSoFar,
                                          get_settings().default_filter_vars,
                                          get_settings().default_filter_types)
        self.execTime = execTime

        # stack is empty in event of a syntax error
        # This is problematic because frontend has to handle syntax/regular error differently
        # to make it easier populate stack so frontend can handle them the same way
        if self.traceback_exception.exc_type is SyntaxError:
            self.traceback_exception.stack.append(
                FrameSummary(self.traceback_exception.filename,
                             int(self.traceback_exception.lineno), ""))
示例#12
0
def report_error(exc, val, tb):
    from traceback import TracebackException

    log = Path(Path().cwd(), "as.txt")

    with open(log, "a") as log:
        log.write("Exception in Tkinter callback")
        sys.last_type = exc
        sys.last_value = val
        sys.last_traceback = tb
        for line in TracebackException(type(val), val, tb,
                                       limit=None).format(chain=True):
            log.write(line)
        log.write("\n")
示例#13
0
    def report_end_result(self):
        StatTrackingReporter.report_end_result(self)

        result = "\n"
        for message in self.failure_messages:
            if isinstance(message, Exception):
                for line in TracebackException(type(message),
                                               message,
                                               message.__traceback__,
                                               limit=None).format(chain=True):
                    result += line
            else:
                print(message)
        result += "\n"
        return self._printable(result)
 def __str__(self) -> str:
     result = []
     if self.error:
         result.append(f"{self.error}")
     if self.container is not None:
         result.append(
             f"in {self.container.__class__.__module__}.{self.container.__class__.__name__}"
         )
     if self.ctx is not None:
         result.append(f"\nCONTEXT: {self.ctx!r}")
     if self.exc is not None and isinstance(self.exc, Exception):
         _exc = ''.join(
             TracebackException.from_exception(self.exc).format())
         result.append(f"\nTRACE: {_exc}")
     return ' '.join(result)
示例#15
0
def error_window(exc: TracebackException):
    message = ''.join(exc.format())

    layout = [[sg.Text(message)], [sg.Button('Ok')]]

    window = sg.Window('Error', layout)

    # --------------------- EVENT LOOP ---------------------
    while True:
        event, values = window.Read(
            timeout=100)  # wait for up to 100 ms for a GUI event
        if event is None or event == 'Ok':
            break

    # if user exits the window, then close the window and exit the GUI func
    window.Close()
示例#16
0
        def callback_wrapper(msg):
            msg = json.loads(msg)
            try:
                ret_value = callback(msg) or ""
                resp = {"type": msg["type"], "status": 200, "data": ret_value}
            except Exception as e:
                resp = {
                    "type": msg["type"],
                    "status": e.code if isinstance(e, WsError) else 500,
                    "data": str(e)
                }
                if e.__cause__:
                    t = TracebackException.from_exception(e.__cause__)
                    resp["traceback"] = "".join(t.format())

            return json.dumps(resp).encode("utf-8")
示例#17
0
def construct_user_code_exc(function, lineno, executed_code, exc_type,
                            exc_value, exc_traceback):
    """ Not generic enough to make it a real class constructor """
    exc_traceback = exc_traceback.tb_next.tb_next

    exc = TracebackException(exc_type, exc_value, exc_traceback)
    exc.stack[0]._line = executed_code

    first = FrameSummary(
        inspect.getfile(function),
        docs_start_lineno(function) + lineno,
        function.__name__,
        lookup_line=False,
    )
    exc.stack.insert(0, first)

    return UserCodeException(exc)
示例#18
0
    def exception(self, exc):
        """
        Helper to report an exception traceback from its object
        """
        traceback = TracebackException.from_exception(exc)
        formatted = ''.join(traceback.format())
        log.error(formatted)

        if formatted in self._last_exceptions:
            log.debug('Exception already logged')
            return

        # Retain the formatted exception in memory to avoid looping
        self._last_exceptions.append(formatted)
        self._loop.call_later(self.EXCEPTION_TTL, self._forget_exception,
                              formatted)

        self.send_report('exception', {'traceback': formatted})
示例#19
0
def log(title, message="", exception=None):
    """
    Logs a message, which will show up in CloudWatch Logs.

    """

    parts = [str(title)]
    for key, value in log.context.items():
        parts.append(key)
        parts.append(value)
    if message != "":
        parts.append(str(message))
    value = " ".join(parts)
    if exception:
        value += "\n"
        value += "\n".join(
            TracebackException.from_exception(exception).format())
    print(value.replace("\n", "\r"))
示例#20
0
def run_put_result_in_queue(func: Callable, gui_queue: queue.Queue, *args,
                            **kwargs):
    """
    A worker thread that communicates with the GUI through a queue
    This thread can block for as long as it wants and the GUI will not be affected
    :param func: Function to run
    :param gui_queue: (queue.Queue) Queue to communicate back to GUI that task is completed
    :return:
    """
    try:
        value = func(*args, **kwargs)
        result = Result(True, value=value)
        gui_queue.put(result)  # put a message into queue for GUI
    except Exception as e:
        te = TracebackException.from_exception(e)
        result = Result(False, exception=te)
        gui_queue.put(result)
        raise e
示例#21
0
    def exception(self, exc):
        """
        Helper to report an exception traceback from its object
        """
        traceback = TracebackException.from_exception(exc)
        formatted = ''.join(traceback.format())
        log.error(formatted)

        if formatted in self._last_exceptions:
            log.debug('Exception already logged')
            return

        # Retain the formatted exception in memory to avoid looping
        self._last_exceptions.append(formatted)
        self._loop.call_later(
            self.EXCEPTION_TTL, self._forget_exception, formatted
        )

        self.send_report('exception', {'traceback': formatted})
示例#22
0
        def server_connectivity_test_error_callback(
            server_scan_request: ServerScanRequest, connectivity_error: ConnectionToServerFailed
        ) -> None:
            for inner_observer in self._observers:
                inner_observer.server_connectivity_test_error(server_scan_request, connectivity_error)

            # Since the server is not reachable, there is nothing else to do
            server_scan_results_queue.put(
                ServerScanResult(
                    uuid=server_scan_request.uuid,
                    server_location=server_scan_request.server_location,
                    network_configuration=server_scan_request.network_configuration,
                    connectivity_status=ServerConnectivityStatusEnum.ERROR,
                    connectivity_error_trace=TracebackException.from_exception(connectivity_error),
                    connectivity_result=None,
                    scan_status=ServerScanStatusEnum.ERROR_NO_CONNECTIVITY,
                    scan_result=None,
                )
            )
示例#23
0
文件: utils.py 项目: Irmitya/zpy
    def print_exception(etype, value, tb, limit=None, file=None, chain=True):
        if file is None:
            file = stderr
        on_line = False
        for line in TracebackException(type(value), value, tb,
                                       limit=limit).format(chain=chain):
            end = ""
            tb = exc_info()[2]
            if line.startswith('Traceback'):
                line = f"Error in "
            elif line.startswith('  File "') and __package__ in line:
                filename = __package__ + line.split('  File "')[1].split(
                    '"')[0].split(__package__)[1]
                (line_no, function) = line.split(f' line ')[1], ''
                if ', ' in line_no:
                    (line_no, function) = line_no.split(', ', 1)
                line = f"{filename}\nline #{line_no} {function}"

            print(line, file=file, end="")
        print(*logs)
def print_prefixed_stack_trace_and_raise(
    file: TextIO = sys.stderr,
    prefix: str = PREFIX,
    scrub_message: str = SCRUB_MESSAGE,
    keep_message: bool = False,
    allow_list: list = [],
    add_timestamp: bool = False,
    err: Optional[BaseException] = None,
) -> None:
    """
    Print the current exception and stack trace to `file` (usually client
    standard error), prefixing the stack trace with `prefix`.
    Args:
        keep_message (bool): if True, don't scrub message. If false, scrub (unless
            allowed).
        allow_list (list): exception allow_list. Ignored if keep_message is True. If
            empty all messages will be srubbed.
        err: the error that was thrown. None accepted for backwards compatibility.
    """
    if err is None:
        err = sys.exc_info()[1]
    scrubbed_err = scrub_exception(err, scrub_message, prefix, keep_message,
                                   allow_list)

    tb_exception = TracebackException.from_exception(
        scrubbed_err)  # type: ignore

    for execution in tb_exception.format():
        if "return function(*func_args, **func_kwargs)" in execution:
            # Do not show the stack trace for our decorator.
            continue
        for line in execution.splitlines():
            if add_timestamp:
                current_time = time.strftime("%Y-%m-%d %H:%M:%S",
                                             time.localtime())
                print(f"{prefix} {current_time} {line}", file=file)
            else:
                print(f"{prefix} {line}", file=file)

    raise scrubbed_err  # type: ignore
def is_exception_allowed(exception: Union[BaseException, TracebackException],
                         allow_list: list) -> bool:
    """
    Check if message is allowed, either by `allow_list`, or `default_allow_list`.

    Args:
        exception (TracebackException): the exception to test
        allow_list (list): list of regex expressions. If any expression matches
            the exception name or message, it will be considered allowed.

    Returns:
        bool: True if message is allowed, False otherwise.
    """
    if not isinstance(exception, TracebackException):
        exception = TracebackException.from_exception(exception)

    # empty list means all messages are allowed
    for expr in allow_list + default_allow_list:
        if re.search(expr, getattr(exception, "_str", ""), re.IGNORECASE):
            return True
        if re.search(expr, getattr(exception.exc_type, "__name__", ""),
                     re.IGNORECASE):
            return True
    return False
示例#26
0
    async def dispatch(self, request: Request,
                       call_next: RequestResponseEndpoint) -> Response:
        try:
            response = await call_next(request)
        except Exception as e:
            tb = TracebackException.from_exception(e)
            logger.error(msg=str(e),
                         extra={
                             'status_code': HTTP_500_INTERNAL_SERVER_ERROR,
                             'request.method': request.method,
                             'request.path': request.url.path,
                             'request.headers': dict(request.headers),
                             'traceback': ''.join(tb.format())
                         })
            return JSONResponse(
                {
                    'detail': [{
                        'type': f'Unexpected error: [{type(e).__name__}]',
                        'msg': str(e)
                    }]
                },
                status_code=HTTP_400_BAD_REQUEST)

        return response
示例#27
0
def _retrieve_and_analyze_http_response(server_info: ServerConnectivityInfo) -> HttpHeadersScanResult:
    # Send HTTP requests until we no longer received an HTTP redirection, but allow only 4 redirections max
    _logger.info(f"Retrieving HTTP headers from {server_info}")
    redirections_count = 0
    next_location_path: Optional[str] = "/"
    http_error_trace = None

    while next_location_path and redirections_count < 4:
        _logger.info(f"Sending HTTP request to {next_location_path}")
        http_path_redirected_to = next_location_path

        # Perform the TLS handshake
        ssl_connection = server_info.get_preconfigured_tls_connection()
        ssl_connection.connect()

        try:
            # Send an HTTP GET request to the server
            ssl_connection.ssl_client.write(
                HttpRequestGenerator.get_request(
                    host=server_info.network_configuration.tls_server_name_indication, path=next_location_path
                )
            )
            http_response = HttpResponseParser.parse_from_ssl_connection(ssl_connection.ssl_client)

        except (OSError, NotAValidHttpResponseError, SslError) as e:
            # The server closed/rejected the connection, or didn't return a valid HTTP response
            http_error_trace = TracebackException.from_exception(e)

        finally:
            ssl_connection.close()

        if http_error_trace:
            break

        # Handle redirection if there is one
        next_location_path = _detect_http_redirection(
            http_response=http_response,
            server_host_name=server_info.network_configuration.tls_server_name_indication,
            server_port=server_info.server_location.port,
        )
        redirections_count += 1

    # Prepare the results
    initial_http_request = HttpRequestGenerator.get_request(
        host=server_info.network_configuration.tls_server_name_indication, path="/"
    ).decode("ascii")

    if http_error_trace:
        # If the server errored when receiving an HTTP request, return the error as the result
        return HttpHeadersScanResult(
            http_request_sent=initial_http_request,
            http_error_trace=http_error_trace,
            http_path_redirected_to=None,
            strict_transport_security_header=None,
            expect_ct_header=None,
        )
    else:
        # If no HTTP error happened, parse and return each header
        return HttpHeadersScanResult(
            http_request_sent=initial_http_request,
            http_path_redirected_to=http_path_redirected_to,
            http_error_trace=None,
            strict_transport_security_header=_parse_hsts_header_from_http_response(http_response),
            expect_ct_header=_parse_expect_ct_header_from_http_response(http_response),
        )
示例#28
0
def format_exception(exc: Exception) -> str:
    """
    Converts a exception object to a readable string
    """
    return "".join(TracebackException.from_exception(exc).format())
示例#29
0
def printEx(e):
    fixPrint("".join(TracebackException.from_exception(e).format()))
示例#30
0
 def create() -> TracebackException:
     try:
         raise RuntimeError("test")
     except RuntimeError as e:
         traceback_exc = TracebackException.from_exception(e)
     return traceback_exc
示例#31
0
def single ():
    """
    One-shot command line interface and pywb_ playback:

    .. code:: bash

        pip install pywb
        crocoite-grab http://example.com/ example.com.warc.gz
        rm -rf collections && wb-manager init test && wb-manager add test example.com.warc.gz
        wayback &
        $BROWSER http://localhost:8080

    .. _pywb: https://github.com/ikreymer/pywb
    """
    parser = argparse.ArgumentParser(description='Save website to WARC using Google Chrome.')
    parser.add_argument('--browser', help='DevTools URL', metavar='URL')
    parser.add_argument('--timeout', default=1*60*60, type=int, help='Maximum time for archival', metavar='SEC')
    parser.add_argument('--idle-timeout', default=30, type=int, help='Maximum idle seconds (i.e. no requests)', dest='idleTimeout', metavar='SEC')
    parser.add_argument('--behavior', help='Enable behavior script',
            dest='enabledBehaviorNames',
            default=list (behavior.availableMap.keys ()),
            choices=list (behavior.availableMap.keys ()),
            metavar='NAME', nargs='*')
    parser.add_argument('--warcinfo', help='Add extra information to warcinfo record',
            metavar='JSON', type=json.loads)
    parser.add_argument('url', help='Website URL', type=URL, metavar='URL')
    parser.add_argument('output', help='WARC filename', metavar='FILE')

    args = parser.parse_args ()

    logger = Logger (consumer=[DatetimeConsumer (), JsonPrintConsumer ()])

    ret = SingleExitStatus.Fail
    service = Process ()
    if args.browser:
        service = Passthrough (args.browser)
    settings = ControllerSettings (idleTimeout=args.idleTimeout, timeout=args.timeout)
    with open (args.output, 'wb') as fd, WarcHandler (fd, logger) as warcHandler:
        logger.connect (WarcHandlerConsumer (warcHandler))
        handler = [StatsHandler (), LogHandler (logger), warcHandler]
        b = list (map (lambda x: behavior.availableMap[x], args.enabledBehaviorNames))
        controller = SinglePageController (url=args.url, settings=settings,
                service=service, handler=handler, behavior=b, logger=logger,
                warcinfo=args.warcinfo)
        try:
            loop = asyncio.get_event_loop()
            run = asyncio.ensure_future (controller.run ())
            stop = lambda signum: run.cancel ()
            loop.add_signal_handler (signal.SIGINT, stop, signal.SIGINT)
            loop.add_signal_handler (signal.SIGTERM, stop, signal.SIGTERM)
            loop.run_until_complete(run)
            loop.close()
            ret = SingleExitStatus.Ok
        except Crashed:
            ret = SingleExitStatus.BrowserCrash
        except asyncio.CancelledError:
            # don’t log this one
            pass
        except browser.NavigateError:
            ret = SingleExitStatus.Navigate
        except Exception as e:
            ret = SingleExitStatus.Fail
            logger.error ('cli exception',
                    uuid='7fd69858-ecaa-4225-b213-8ab880aa3cc5',
                    traceback=list (TracebackException.from_exception (e).format ()))
        finally:
            r = handler[0].stats
            logger.info ('stats', context='cli', uuid='24d92d16-770e-4088-b769-4020e127a7ff', **r)

    return ret
示例#32
0
    def load_script(self,
                    script_path: Path,
                    external_args: str = '',
                    reloading=False) -> None:
        import shlex
        from traceback import FrameSummary, TracebackException

        self.toolbars.playback.stop()

        self.statusbar.label.setText('Evaluating')
        self.script_path = script_path
        sys.path.append(str(self.script_path.parent))

        # Rewrite args so external args will be forwarded correctly
        if external_args:
            self.external_args = shlex.split(external_args)
        try:
            argv_orig = sys.argv
            sys.argv = [script_path.name] + self.external_args
        except AttributeError:
            pass

        try:
            # pylint: disable=exec-used
            exec(self.script_path.read_text(encoding='utf-8'),
                 {'__file__': sys.argv[0]})
        except Exception as e:  # pylint: disable=broad-except
            self.script_exec_failed = True
            logging.error(e)

            te = TracebackException.from_exception(e)
            # remove the first stack frame, which contains our exec() invocation
            del te.stack[0]

            # replace <string> with script path only for the first stack frames
            # in order to keep intact exec() invocations down the stack
            # that we're not concerned with
            for i, frame in enumerate(te.stack):
                if frame.filename == '<string>':
                    te.stack[i] = FrameSummary(str(self.script_path),
                                               frame.lineno, frame.name)
                else:
                    break
            print(''.join(te.format()))

            self.handle_script_error(
                f'''An error occured while evaluating script:
                \n{str(e)}
                \nSee console output for details.''')
            return
        finally:
            sys.argv = argv_orig
            sys.path.pop()

        self.script_exec_failed = False

        if len(vs.get_outputs()) == 0:
            logging.error('Script has no outputs set.')
            self.handle_script_error('Script has no outputs set.')
            return

        if not reloading:
            self.toolbars.main.rescan_outputs()
            for toolbar in self.toolbars:
                toolbar.on_script_loaded()
            self.switch_output(self.OUTPUT_INDEX)

            self.load_storage()
        else:
            self.load_storage()
            for toolbar in self.toolbars:
                toolbar.on_script_loaded()