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

Code before:

        msg = (
            'Type Exception: %s\n'
            'Error Message: %s\n'
            'URL requested: %s\n'
            'Request method: %s\n'
            'Handler class name: %s\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)

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

        Args:
            exception: Exception. The exception that was thrown.
            unused_debug_mode: bool. True if the web application is running
                in debug mode.
        """
        handler_class_name = self.__class__.__name__
        request_method = self.request.environ['REQUEST_METHOD']
        exception_type = type(exception).__name__
        if isinstance(exception, self.NotLoggedInException):
            # This checks if the response should be JSON or HTML.
            # For GET requests, there is no payload, so we check against
            # GET_HANDLER_ERROR_RETURN_TYPE.
            # Otherwise, we check whether self.payload exists.

            # This check is to avoid throwing of 401 when payload doesn't
            # exists and self.payload is replaced by RaiseErrorOnGet object.
            # TODO(#13155): Change this to self.normalized_payload
            #  once schema is implemented for all handlers.
            payload_exists = (
                self.payload is not None and
                not isinstance(self.payload, RaiseErrorOnGet)
            )
            self._log_exception_message(
                exception_type,
                LogType.WARNING,

Review comment (suggestion):
else raise exception

Or, do 
```
   if XYZ:
       ...
   else:
       assert that it's the remaining condition
       ...
```

Then, you properly handle all cases, not just a few.

Code after:

        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
    ) -> None:
        """Overwrites the default exception handler.

        Args:
            exception: Exception. The exception that was thrown.
            unused_debug_mode: bool. True if the web application is running
                in debug mode.
        """
        handler_class_name = self.__class__.__name__
        request_method = self.request.environ['REQUEST_METHOD']
        exception_type = type(exception).__name__
        if isinstance(exception, self.NotLoggedInException):
            # This checks if the response should be JSON or HTML.
            # For GET requests, there is no payload, so we check against
            # GET_HANDLER_ERROR_RETURN_TYPE.
            # Otherwise, we check whether self.payload exists.

            # This check is to avoid throwing of 401 when payload doesn't
            # exists and self.payload is replaced by RaiseErrorOnGet object.
            # TODO(#13155): Change this to self.normalized_payload