
    g                        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 Zd ZdZdZ dZ!dZ"dZ#ddZ$d Z%d Z&d Z'y)zError Reporting Handler.    )absolute_import)division)unicode_literalsN)
exceptions)util)apis)command_loading)error_reporting_util)config)log)metrics)
properties)console_attr)	platformsc                 n    t        | t        j                        xr 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)errs    0lib/googlecloudsdk/command_lib/crash_handling.py_IsInstallationCorruptionr   '   s.     S/<<
= 6
S''
57    c           
          t        j                  dj                  | j                  |t        j
                  t        j                  j                  d   t        j                  j                  d                y)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_strings     r   _PrintInstallationActionr(   4   s_     ))
6 	++^^$22LLQO$22LLQO 	 	r   zcloud-sdk-user-errors'AIzaSyCUuWyME_r4XylltWNeydEjKSkgXkvpVyUgcloudzcloud-sdk-crashesz'AIzaSyAp4DSI_Z3-mK-B8U0t7GE34n74OWDJmakc                     t        j                  t        j                  t        j                        } |d      }| r|j                  dt               |S |j                  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_instances      r   _GetReportingClientr8   _   s_     ))$--9I9IJ, 7/""5*?@ 
 ""5*?@	r   c                    t        j                         r2t         j                  j                  j                  j                         ryt        j                         }t        j                  |      }t         j                  j                  j                  j                         }t        j                         }t        |       }t        j                   |      }	 |j"                  j%                  d      }|j'                  |t(        t*        j,                  | rt.        nt0        ||      }|j"                  j3                  ||      }t        j4                  |j6                  |j8                  |j:                  |j<                         y# t>        j@                  $ rK}	tB        jD                  jG                  djI                  tK        jL                  |	                   Y d}	~	yd}	~	ww xY 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GetCIDIfMetricsEnabledr8   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)
r5   
stacktracer   cidclientreportermethod_configrequesthttp_requestes
             r   ReportErrorrf   r   s    
&
&
(44<<>
 ##%*#II*%%22668'&&(#x(&  (('**::8DM,, ((!)}# - 'G ))<<w L))<+C+C%**L,@,@B 
	"	" '188!!!$	&' ''s   B,F G#AGG#c                    t        j                  |       }t        j                  j	                  d       t        |       rt        | |       yt        j                  dj                  t        | dt        |       j                        |             d|v r]t        j                  j                  d       t        j                  j                  d       t        j                  j                  d       t        d	       t        j                  j                  d
       t        j                  j                  d       t        j                  j                  d       t        j                  j                  d       y)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)r5   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   r]   r   r\   	exceptionr   r(   r!   r"   getattrtype__name__r   Printrf   r&   s     r   HandleGcloudCrashrn      s    $$S)*  !9:s#S*-II)00\49#5#56
D E"j0	ggmmFG 
ggmm ) *	ggmm
KMGGMM ' (GGMM%&GGMM ' (GGMM34r   c                 B     t        j                          f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                      	  | i | y # t         $ r]}t        |       t        j                  j                  j
                  j                         r t        j                  d       Y d }~y d }~ww xY w)Nr   )		Exceptionrn   r   rB   rC   print_unhandled_tracebacksrE   r#   exit)argskwargsre   target_functions      r   WrapperzCrashManager.<locals>.Wrapper   s]    t&v& 							:	:	B	B	D 		s    	A2AA--A2)	functoolswraps)rv   rw   s   ` r   CrashManagerrz      s'     ???# $ 
.r   )T)(__doc__
__future__r   r   r   rx   r#   rF   apitools.base.pyr   rZ   &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(   rS   r4   rP   rR   r3   r8   rf   rn   rz    r   r   <module>r      sz      &  '  
  > 7 9 3 ; & # ' * 4 .
7!H (A 
#A &%'P5<r   