Repository: oppia/oppia
File: core/controllers/base.py (Python)

Code before:
        else:
            logging.warning('Not a recognized request method.')
            self._render_exception_json_or_html(
                feconf.HANDLER_TYPE_JSON, values
            )

    def _log_exception_message(
        self, exception_type: str, log_type: LogType, error_message: str
    ) -> None:
        """Logs exception details.

        Args:
            exception_type: str. Name of the exception.
            log_type: LogType. Log level ('warning', 'exception').
            error_message: str. Detailed error message.

        Raises:
            Exception. Invalid log type value.
        """

        handler_class_name = self.__class__.__name__
        request_method = self.request.environ['REQUEST_METHOD']
        url = self.request.uri

        msg = (
            '\nType Exception: %s\n'
            'Error Message: %s\n'
            'URL requested: %s\n'
            'Request method: %s\n'
            'Handler class name: %s\n\n'
            % (
                exception_type,
                error_message,
                url,
                request_method,
                handler_class_name
            )
        )

        if log_type == LogType.WARNING:
            logging.warning(msg)
        elif log_type == LogType.EXCEPTION:
            logging.exception(msg)
        else:
            raise Exception('Invalid log type value: %s' % log_type)

    def handle_exception(
        self, exception: BaseException, unused_debug_mode: bool
    ) -> None:
        """Overwrites the default exception handler.


Review comment (suggestion):
Shouldn't the logged error include a stacktrace?

Basically, what is logged should include all the same info as raising an exception would, just with the additional details tacked on at the beginning. See the original issue description.

Code after:
                self.DELETE_HANDLER_ERROR_RETURN_TYPE, values)
        else:
            logging.warning('Not a recognized request method.')
            self._render_exception_json_or_html(
                feconf.HANDLER_TYPE_JSON, values
            )

    def _log_exception_message(
        self, exception_type: str, log_type: LogType, error_message: str
    ) -> None:
        """Logs exception details.

        Args:
            exception_type: str. Name of the exception.
            log_type: LogType. Log level ('warning', 'exception').
            error_message: str. Detailed error message.

        Raises:
            Exception. Invalid log type value.
        """

        handler_class_name = self.__class__.__name__
        request_method = self.request.environ['REQUEST_METHOD']
        url = self.request.uri
        stack_trace = traceback.format_exc()

        msg = (
            '\n\n%s: %s\n\n'
            'Stack Trace: \n%s\n'
            'URL requested: %s\n'
            'Request method: %s\n'
            'Handler class name: %s\n'
            % (
                exception_type,
                error_message,
                stack_trace,
                url,
                request_method,
                handler_class_name
            )
        )

        if log_type == LogType.WARNING:
            logging.warning(msg)
        elif log_type == LogType.EXCEPTION:
            logging.exception(msg)
        else:
            raise Exception('Invalid log type value: %s' % log_type)

    def handle_exception(
        self, exception: BaseException, unused_debug_mode: bool