import logging

from django.core.exceptions import ValidationError
from requests import HTTPError, RequestException

from .. import celeryconf
from ..core import JobStatus
from .installation_utils import install_app
from .models import AppInstallation

logger = logging.getLogger(__name__)


@celeryconf.app.task
def install_app_task(job_id, activate=False):
    app_installation = AppInstallation.objects.get(id=job_id)
    try:
        install_app(app_installation, activate=activate)
        app_installation.delete()
        return
    except ValidationError as e:
        msg = ", ".join([f"{name}: {err}" for name, err in e.message_dict.items()])
        app_installation.message = msg
    except (RequestException, HTTPError) as e:
        logger.warning("Failed to install an app. error: %s", e)
        app_installation.message = (
            "Failed to connect to app. Try later or contact with app support."
        )
    except Exception:
        app_installation.message = "Unknow error. Contact with app support."
    app_installation.status = JobStatus.FAILED
    app_installation.save()

import logging

from django.core.exceptions import ValidationError
from requests import HTTPError, RequestException

from .. import celeryconf
from ..core import JobStatus
from .installation_utils import install_app
from .models import AppInstallation

logger = logging.getLogger(__name__)


@celeryconf.app.task
def install_app_task(job_id, activate=False):
    app_installation = AppInstallation.objects.get(id=job_id)
    try:
        install_app(app_installation, activate=activate)
        app_installation.delete()
        return
    except ValidationError as e:
        msg = ", ".join([f"{name}: {err}" for name, err in e.message_dict.items()])
        app_installation.message = msg
    except (RequestException, HTTPError) as e:
        logger.warning("Failed to install an app. error: %s", e)
        app_installation.message = (
            "Failed to connect to app. Try later or contact with app support."
        )
    except Exception as e:
        logger.warning("Failed to install app. error %s", e)
        app_installation.message = f"Error {e}. Contact with app support."
    app_installation.status = JobStatus.FAILED
    app_installation.save()

Add more context to install app msg
Add more context to install app msg

  import logging
  
  from django.core.exceptions import ValidationError
  from requests import HTTPError, RequestException
  
  from .. import celeryconf
  from ..core import JobStatus
  from .installation_utils import install_app
  from .models import AppInstallation
  
  logger = logging.getLogger(__name__)
  
  
  @celeryconf.app.task
  def install_app_task(job_id, activate=False):
      app_installation = AppInstallation.objects.get(id=job_id)
      try:
          install_app(app_installation, activate=activate)
          app_installation.delete()
          return
      except ValidationError as e:
          msg = ", ".join([f"{name}: {err}" for name, err in e.message_dict.items()])
          app_installation.message = msg
      except (RequestException, HTTPError) as e:
          logger.warning("Failed to install an app. error: %s", e)
          app_installation.message = (
              "Failed to connect to app. Try later or contact with app support."
          )
-     except Exception:
+     except Exception as e:
+         logger.warning("Failed to install app. error %s", e)
-         app_installation.message = "Unknow error. Contact with app support."
+         app_installation.message = f"Error {e}. Contact with app support."
      app_installation.status = JobStatus.FAILED
      app_installation.save()
  
Add more context to install app msg
## Code Before:
import logging

from django.core.exceptions import ValidationError
from requests import HTTPError, RequestException

from .. import celeryconf
from ..core import JobStatus
from .installation_utils import install_app
from .models import AppInstallation

logger = logging.getLogger(__name__)


@celeryconf.app.task
def install_app_task(job_id, activate=False):
    app_installation = AppInstallation.objects.get(id=job_id)
    try:
        install_app(app_installation, activate=activate)
        app_installation.delete()
        return
    except ValidationError as e:
        msg = ", ".join([f"{name}: {err}" for name, err in e.message_dict.items()])
        app_installation.message = msg
    except (RequestException, HTTPError) as e:
        logger.warning("Failed to install an app. error: %s", e)
        app_installation.message = (
            "Failed to connect to app. Try later or contact with app support."
        )
    except Exception:
        app_installation.message = "Unknow error. Contact with app support."
    app_installation.status = JobStatus.FAILED
    app_installation.save()

## Instruction:
Add more context to install app msg
## Code After:
import logging

from django.core.exceptions import ValidationError
from requests import HTTPError, RequestException

from .. import celeryconf
from ..core import JobStatus
from .installation_utils import install_app
from .models import AppInstallation

logger = logging.getLogger(__name__)


@celeryconf.app.task
def install_app_task(job_id, activate=False):
    app_installation = AppInstallation.objects.get(id=job_id)
    try:
        install_app(app_installation, activate=activate)
        app_installation.delete()
        return
    except ValidationError as e:
        msg = ", ".join([f"{name}: {err}" for name, err in e.message_dict.items()])
        app_installation.message = msg
    except (RequestException, HTTPError) as e:
        logger.warning("Failed to install an app. error: %s", e)
        app_installation.message = (
            "Failed to connect to app. Try later or contact with app support."
        )
    except Exception as e:
        logger.warning("Failed to install app. error %s", e)
        app_installation.message = f"Error {e}. Contact with app support."
    app_installation.status = JobStatus.FAILED
    app_installation.save()
