o
    g                     @   s  d Z ddlmZ ddlmZ ddlmZ ddlZddlZddlZddlm	Z
 ddlmZ ddlmZ dd	lmZ dd
lmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ dd Zdd ZdZdZ dZ!dZ"dZ#d#ddZ$dd Z%dd  Z&d!d" Z'dS )$zError Reporting Handler.    )absolute_import)division)unicode_literalsN)
exceptions)util)apis)command_loading)error_reporting_util)config)log)metrics)
properties)console_attr)	platformsc                 C   s   t | tjot | jtS )zDetermines if the error may be from installation corruption.

  Args:
    err: Exception err.

  Returns:
    bool, True if installation error, False otherwise
  )
isinstancer   CommandLoadFailureroot_exceptionImportError)err r   F/tmp/google-cloud-sdk/lib/googlecloudsdk/command_lib/crash_handling.py_IsInstallationCorruption'   s   	
r   c              	   C   s2   t dj| j|tjtjjd tjjd d dS )zlPrompts installation error action.

  Args:
    err: Exception err.
    err_string: Exception err string.
  a  gcloud failed to load ({command}): {err_str}

This usually indicates corruption in your gcloud installation or problems with your Python interpreter.

Please verify that the following is the path to a working Python {py_major_version}.{py_minor_version}+ executable:
    {executable}
If it is not, please set the CLOUDSDK_PYTHON environment variable to point to a working Python {py_major_version}.{py_minor_version}+ executable.

If you are still experiencing problems, please run the following command to reinstall:
    $ gcloud components reinstall

If that command fails, please reinstall the Google Cloud CLI using the instructions here:
    https://cloud.google.com/sdk/r      )commanderr_str
executablepy_major_versionpy_minor_versionN)	r   errorformatr   sysr   r   PythonVersionMIN_SUPPORTED_PY3_VERSIONr   
err_stringr   r   r   _PrintInstallationAction4   s   

r%   zcloud-sdk-user-errors'AIzaSyCUuWyME_r4XylltWNeydEjKSkgXkvpVyUgcloudzcloud-sdk-crashesz'AIzaSyAp4DSI_Z3-mK-B8U0t7GE34n74OWDJmakTc                 C   s>   t tjtj}|dd}| r|dt |S |dt |S )a  Returns a client that uses an API key for Cloud SDK crash reports.

  Args:
     is_crash: bool, True use CRASH_REPORTING_PARAM, if False use
     ERROR_REPORTING_PARAM.

  Returns:
    An error reporting client that uses an API key for Cloud SDK crash reports.
  F)get_credentialskey)	core_apisGetClientClassr   API_NAMEAPI_VERSIONAddGlobalParamCRASH_REPORTING_PARAMERROR_REPORTING_PARAM)is_crashclient_classclient_instancer   r   r   _GetReportingClient_   s   

r4   c           
   
   C   s   t  rt jjj rdS t }t	|}t jj
j }t
 }t| }t|}z,|jd}|j|ttj| r;tnt||d}|j||}t
|j|j|j|j W dS  tj yv }	 zt!j"#d$t%&|	 W Y d}	~	dS d}	~	ww )zReport the anonymous crash information to the Error Reporting service.

  This will report the actively handled exception.
  Args:
    is_crash: bool, True if this is a crash, False if it is a user error.
  NReport)error_messageserviceversionprojectrequest_urluserz&Unable to report crash stacktrace:
{0})'r   IsDefaultUniverseVALUEScoredisable_usage_reportingGetBool	traceback
format_excr	   %RemovePrivateInformationFromTracebackr   command_nameGetGetCIDIfMetricsEnabledr4   r   ErrorReportingprojects_eventsGetMethodConfigGenerateReportRequestSERVICEr
   CLOUD_SDK_VERSIONCRASH_PROJECTERROR_PROJECTPrepareHttpRequestCustomBeaconurlhttp_methodbodyheadersapitools_exceptionsErrorr   file_only_loggerr   r   r   SafeText)
r1   
stacktracer   cidclientreportermethod_configrequesthttp_requester   r   r   ReportErrorr   sF   


ra   c              	   C   s   t | }tjd t| rt| | dS tdt	| dt
| j| d|v r<tjd tjd tjd tdd	 tjd
 tjd tjd tjd dS )znChecks if installation error occurred, then proceeds with Error Reporting.

  Args:
    err: Exception err.
  zBEGIN CRASH STACKTRACEzgcloud crashed ({0}): {1}
error_namezcertificate verify failedz{
gcloud's default CA certificates failed to verify your connection, which can happen if you are behind a proxy or firewall.zGTo use a custom CA certificates file, please run the following command:z?  gcloud config set core/custom_ca_certs_file /path/to/ca_certsT)r1   zJ
If you would like to report this issue, please run the following command:z  gcloud feedbackzG
To check gcloud for common problems, please run the following command:z  gcloud info --run-diagnosticsN)r   rX   r   rW   	exceptionr   r%   r   r   getattrtype__name__r   Printra   r#   r   r   r   HandleGcloudCrash   s(   

rh   c                    s   t   fdd}|S )zContext manager for handling gcloud crashes.

  Good for wrapping multiprocessing and multithreading target functions.

  Args:
    target_function (function): Unit test to decorate.

  Returns:
    Decorator function.
  c               
      s^   z
 | i | W d S  t y. } zt| tjjj r td W Y d }~d S d }~ww )Nr   )		Exceptionrh   r   r=   r>   print_unhandled_tracebacksr@   r    exit)argskwargsr`   target_functionr   r   Wrapper   s   zCrashManager.<locals>.Wrapper)	functoolswraps)ro   rp   r   rn   r   CrashManager   s   rs   )T)(__doc__
__future__r   r   r   rq   r    rA   apitools.base.pyr   rU   &googlecloudsdk.api_lib.error_reportingr   googlecloudsdk.api_lib.utilr   r*   googlecloudsdk.callioper   googlecloudsdk.command_libr	   googlecloudsdk.corer
   r   r   r   googlecloudsdk.core.consoler   googlecloudsdk.core.utilr   r   r%   rN   r0   rK   rM   r/   r4   ra   rh   rs   r   r   r   r   <module>   s:   $
(