<commit_before>#!/usr/bin/env python
"""
The error handler provides a centralised control for aborting the application
and logging errors for debugging later.
"""

import logging
import traceback
import sys
import cgi
import json
import urllib2
from framework.dependency_management.dependency_resolver import BaseComponent
from framework.dependency_management.interfaces import ErrorHandlerInterface

from framework.lib.exceptions import FrameworkAbortException, \
                                     PluginAbortException
from framework.lib.general import cprint
from framework.utils import OutputCleaner


class ErrorHandler(BaseComponent, ErrorHandlerInterface):
    Command = ''
    PaddingLength = 100
    COMPONENT_NAME = "error_handler"

    def __init__(self):
        self.register_in_service_locator()
        self.config = self.get_component("config")
        self.Core = None
        self.db = None
        self.db_error = None
        self.Padding = "\n" + "_" * self.PaddingLength + "\n\n"
        self.SubPadding = "\n" + "*" * self.PaddingLength + "\n"

    def init(self):
        self.Core = self.get_component("core")
        self.db = self.get_component("db")
        self.db_error = self.get_component("db_error")

    def SetCommand(self, command):
        self.Command = command

    def FrameworkAbort(self, message):
        """Abort the OWTF framework.

        :warning: If it happens really early and :class:`framework.core.Core`
            has note been instanciated yet, `sys.exit()` is called with error
            code -1

        :param str message: Descriptive message about the abort.

        :return: full message explaining the abort.
        :rtype: str

        """
        message = "Aborted by Framework: %s" % message
        logging.error(message)
        if self.Core is None:
            # Core being None means that OWTF is aborting super early.
            # Therefore, force a brutal exit and throw away the message.
            sys.exit(-1)
        else:
            self.Core.finish()
        return message

    def get_option_from_user(self, options):
        return raw_input(
            "Options: 'e'+Enter= Exit" + options + ", Enter= Next test\n")

    def UserAbort(self, level, partial_output = ''):
        # Levels so far can be Command or Plugin
        message = logging.info(
            "\nThe " + level + " was aborted by the user: Please check the "
            "report and plugin output files")
        message = (
            "\nThe " + level + " was aborted by the user: Please check the "
            "report and plugin output files")
        options = ""
        if 'Command' == level:
            options = ", 'p'+Enter= Move on to next plugin"
            option = 'p'
            if 'e' == option:
                if 'Command' == level:  # Try to save partial plugin results.
                    raise FrameworkAbortException(partial_output)
            elif 'p' == option:  # Move on to next plugin.
                # Jump to next handler and pass partial output to avoid losing
                # results.
                raise PluginAbortException(partial_output)
        return message

    def LogError(self, message, trace=None):
        try:
            self.db_error.Add(message, trace)  # Log error in the DB.
        except AttributeError:
            cprint("ERROR: DB is not setup yet: cannot log errors to file!")

    def AddOWTFBug(self, message):
        # TODO: http://blog.tplus1.com/index.php/2007/09/28/the-python-logging-module-is-much-better-than-print-statements/
        exc_type, exc_value, exc_traceback = sys.exc_info()
        err_trace_list = traceback.format_exception(
            exc_type, exc_value, exc_traceback)
        err_trace = OutputCleaner.anonymise_command("\n".join(err_trace_list))
        message = OutputCleaner.anonymise_command(message)
        output = self.Padding + "OWTF BUG: Please report the sanitised " \
                 "information below to help make this better. Thank you." + \
                 self.SubPadding
        output += "\nMessage: " + message + "\n"
        output += "\nError Trace:"
        output += "\n" + err_trace
        output += "\n"+self.Padding
        cprint(output)
        self.LogError(message, err_trace)

    def Add(self, message, bugType='owtf'):
        if 'owtf' == bugType:
            return self.AddOWTFBug(message)<commit_msg>Fixes #352<commit_after>#!/usr/bin/env python
"""
The error handler provides a centralised control for aborting the application
and logging errors for debugging later.
"""

import logging
import traceback
import sys
import cgi
import json
import urllib2
from framework.dependency_management.dependency_resolver import BaseComponent
from framework.dependency_management.interfaces import ErrorHandlerInterface

from framework.lib.exceptions import FrameworkAbortException, \
                                     PluginAbortException
from framework.lib.general import cprint
from framework.utils import OutputCleaner, print_version


class ErrorHandler(BaseComponent, ErrorHandlerInterface):
    Command = ''
    PaddingLength = 100
    COMPONENT_NAME = "error_handler"

    def __init__(self):
        self.register_in_service_locator()
        self.config = self.get_component("config")
        self.Core = None
        self.db = None
        self.db_error = None
        self.Padding = "\n" + "_" * self.PaddingLength + "\n\n"
        self.SubPadding = "\n" + "*" * self.PaddingLength + "\n"

    def init(self):
        self.Core = self.get_component("core")
        self.db = self.get_component("db")
        self.db_error = self.get_component("db_error")

    def SetCommand(self, command):
        self.Command = command

    def FrameworkAbort(self, message):
        """Abort the OWTF framework.

        :warning: If it happens really early and :class:`framework.core.Core`
            has note been instanciated yet, `sys.exit()` is called with error
            code -1

        :param str message: Descriptive message about the abort.

        :return: full message explaining the abort.
        :rtype: str

        """
        message = "Aborted by Framework: %s" % message
        logging.error(message)
        if self.Core is None:
            # Core being None means that OWTF is aborting super early.
            # Therefore, force a brutal exit and throw away the message.
            sys.exit(-1)
        else:
            self.Core.finish()
        return message

    def get_option_from_user(self, options):
        return raw_input(
            "Options: 'e'+Enter= Exit" + options + ", Enter= Next test\n")

    def UserAbort(self, level, partial_output = ''):
        # Levels so far can be Command or Plugin
        message = logging.info(
            "\nThe " + level + " was aborted by the user: Please check the "
            "report and plugin output files")
        message = (
            "\nThe " + level + " was aborted by the user: Please check the "
            "report and plugin output files")
        options = ""
        if 'Command' == level:
            options = ", 'p'+Enter= Move on to next plugin"
            option = 'p'
            if 'e' == option:
                if 'Command' == level:  # Try to save partial plugin results.
                    raise FrameworkAbortException(partial_output)
            elif 'p' == option:  # Move on to next plugin.
                # Jump to next handler and pass partial output to avoid losing
                # results.
                raise PluginAbortException(partial_output)
        return message

    def LogError(self, message, trace=None):
        try:
            self.db_error.Add(message, trace)  # Log error in the DB.
        except AttributeError:
            cprint("ERROR: DB is not setup yet: cannot log errors to file!")

    def AddOWTFBug(self, message):
        # TODO: http://blog.tplus1.com/index.php/2007/09/28/the-python-logging-module-is-much-better-than-print-statements/
        exc_type, exc_value, exc_traceback = sys.exc_info()
        err_trace_list = traceback.format_exception(
            exc_type, exc_value, exc_traceback)
        err_trace = OutputCleaner.anonymise_command("\n".join(err_trace_list))
        message = OutputCleaner.anonymise_command(message)
        output = self.Padding + "OWTF BUG: Please report the sanitised " \
                 "information below to help make this better. Thank you." + \
                 self.SubPadding + print_version(self.config.Rootdir, commit_hash=True, version=True) + \
                 self.SubPadding
        output += "\nMessage: " + message + "\n"
        output += "\nError Trace:"
        output += "\n" + err_trace
        output += "\n"+self.Padding
        cprint(output)
        self.LogError(message, err_trace)

    def Add(self, message, bugType='owtf'):
        if 'owtf' == bugType:
            return self.AddOWTFBug(message)
{'fraction_non_alphanumeric': 0.09510991475998205, 'fraction_numerical': 0.0037012113055181696, 'mean_word_length': 2.2651043573782497, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5599251', 'n_tokens_mistral': 2494, 'n_tokens_neox': 2376, 'n_words': 735}