Ejemplo n.º 1
0
        /// <summary>
        /// Constructs a new instance of a ProviderInvocationException
        /// using the specified data
        /// </summary>
        ///
        /// <param name="resourceId">
        /// The resource ID to use as the format message for the error.
        /// </param>
        ///
        /// <param name="resourceStr">
        /// This is the message template string.
        /// </param>
        ///
        /// <param name="provider">
        /// The provider information used when formatting the error message.
        /// </param>
        ///
        /// <param name="path">
        /// The path used when formatting the error message.
        /// </param>
        ///
        /// <param name="e">
        /// The exception that was thrown by the provider. This will be set as
        /// the ProviderInvocationException's InnerException and the message will
        /// be used when formatting the error message.
        /// </param>
        ///
        /// <param name="useInnerExceptionErrorMessage">
        /// If true, the error record from the inner exception will be used if it contains one.
        /// If false, the error message specified by the resourceId will be used.
        /// </param>
        ///
        /// <returns>
        /// A new instance of a ProviderInvocationException.
        /// </returns>
        ///
        /// <exception cref="ProviderInvocationException">
        /// Wraps <paramref name="e"/> in a ProviderInvocationException
        /// and then throws it.
        /// </exception>
        ///
        internal ProviderInvocationException NewProviderInvocationException(
            string resourceId,
            string resourceStr,
            ProviderInfo provider,
            string path,
            Exception e,
            bool useInnerExceptionErrorMessage)
        {
            //  If the innerException was itself thrown by
            //  ProviderBase.ThrowTerminatingError, it is already a
            //  ProviderInvocationException, and we don't want to
            //  re-wrap it.
            ProviderInvocationException pie = e as ProviderInvocationException;

            if (null != pie)
            {
                pie._providerInfo = provider;
                return(pie);
            }

            pie = new ProviderInvocationException(resourceId, resourceStr, provider, path, e, useInnerExceptionErrorMessage);

            // Log a provider health event

            MshLog.LogProviderHealthEvent(
                ExecutionContext,
                provider.Name,
                pie,
                Severity.Warning);

            return(pie);
        }
Ejemplo n.º 2
0
 internal void WriteError(ErrorRecord errorRecord)
 {
     if (this.Stopping)
     {
         PipelineStoppedException exception = new PipelineStoppedException();
         throw exception;
     }
     if (this.streamErrors)
     {
         if (this.command == null)
         {
             throw PSTraceSource.NewInvalidOperationException("SessionStateStrings", "ErrorStreamingNotEnabled", new object[0]);
         }
         tracer.WriteLine("Writing error package to command error pipe", new object[0]);
         this.command.WriteError(errorRecord);
     }
     else
     {
         this.accumulatedErrorObjects.Add(errorRecord);
         if ((errorRecord.ErrorDetails != null) && (errorRecord.ErrorDetails.TextLookupError != null))
         {
             Exception textLookupError = errorRecord.ErrorDetails.TextLookupError;
             errorRecord.ErrorDetails.TextLookupError = null;
             MshLog.LogProviderHealthEvent(this.ExecutionContext, this.ProviderInstance.ProviderInfo.Name, textLookupError, Severity.Warning);
         }
     }
 }
        } // WriteObject

        /// <summary>
        /// Writes the error to the pipeline or accumulates the error in an internal
        /// buffer.
        /// </summary>
        /// <param name="errorRecord">
        /// The error record to write to the pipeline or the internal buffer.
        /// </param>
        /// <exception cref="InvalidOperationException">
        /// The CmdletProvider could not stream the error because no
        /// cmdlet was specified to stream the output through.
        /// </exception>
        /// <exception cref="PipelineStoppedException">
        /// If the pipeline has been signaled for stopping but
        /// the provider calls this method.
        /// </exception>
        internal void WriteError(ErrorRecord errorRecord)
        {
            // Making sure to obey the StopProcessing by
            // throwing an exception anytime a provider tries
            // to WriteError

            if (Stopping)
            {
                PipelineStoppedException stopPipeline =
                    new PipelineStoppedException();

                throw stopPipeline;
            }

            if (_streamErrors)
            {
                if (_command != null)
                {
                    s_tracer.WriteLine("Writing error package to command error pipe");

                    _command.WriteError(errorRecord);
                }
                else
                {
                    InvalidOperationException e =
                        PSTraceSource.NewInvalidOperationException(
                            SessionStateStrings.ErrorStreamingNotEnabled);
                    throw e;
                }
            }
            else
            {
                // Since we are not streaming, just add the object to the accumulatedErrorObjects
                _accumulatedErrorObjects.Add(errorRecord);

                if (errorRecord.ErrorDetails != null &&
                    errorRecord.ErrorDetails.TextLookupError != null)
                {
                    Exception textLookupError = errorRecord.ErrorDetails.TextLookupError;
                    errorRecord.ErrorDetails.TextLookupError = null;
                    MshLog.LogProviderHealthEvent(
                        this.ExecutionContext,
                        this.ProviderInstance.ProviderInfo.Name,
                        textLookupError,
                        Severity.Warning);
                }
            }
        } // WriteError
        /// <summary>
        /// If there are any errors accumulated, the first error is thrown.
        /// </summary>
        /// <param name="wrapExceptionInProviderException">
        /// If true, the error will be wrapped in a ProviderInvocationException before
        /// being thrown. If false, the error will be thrown as is.
        /// </param>
        /// <exception cref="ProviderInvocationException">
        /// If <paramref name="wrapExceptionInProviderException"/> is true, the
        /// first exception that was written to the error pipeline by a CmdletProvider
        /// is wrapped and thrown.
        /// </exception>
        /// <exception>
        /// If <paramref name="wrapExceptionInProviderException"/> is false,
        /// the first exception that was written to the error pipeline by a CmdletProvider
        /// is thrown.
        /// </exception>
        internal void ThrowFirstErrorOrDoNothing(bool wrapExceptionInProviderException)
        {
            if (HasErrors())
            {
                Collection <ErrorRecord> errors = GetAccumulatedErrorObjects();

                if (errors != null && errors.Count > 0)
                {
                    // Throw the first exception

                    if (wrapExceptionInProviderException)
                    {
                        ProviderInfo providerInfo = null;
                        if (this.ProviderInstance != null)
                        {
                            providerInfo = this.ProviderInstance.ProviderInfo;
                        }

                        ProviderInvocationException e =
                            new ProviderInvocationException(
                                providerInfo,
                                errors[0]);

                        // Log a provider health event

                        MshLog.LogProviderHealthEvent(
                            this.ExecutionContext,
                            providerInfo != null ? providerInfo.Name : "unknown provider",
                            e,
                            Severity.Warning);

                        throw e;
                    }
                    else
                    {
                        throw errors[0].Exception;
                    }
                }
            }
        }
Ejemplo n.º 5
0
 internal void ThrowFirstErrorOrDoNothing(bool wrapExceptionInProviderException)
 {
     if (this.HasErrors())
     {
         Collection <ErrorRecord> accumulatedErrorObjects = this.GetAccumulatedErrorObjects();
         if ((accumulatedErrorObjects != null) && (accumulatedErrorObjects.Count > 0))
         {
             if (!wrapExceptionInProviderException)
             {
                 throw accumulatedErrorObjects[0].Exception;
             }
             ProviderInfo provider = null;
             if (this.ProviderInstance != null)
             {
                 provider = this.ProviderInstance.ProviderInfo;
             }
             ProviderInvocationException exception = new ProviderInvocationException(provider, accumulatedErrorObjects[0]);
             MshLog.LogProviderHealthEvent(this.ExecutionContext, (provider != null) ? provider.Name : "unknown provider", exception, Severity.Warning);
             throw exception;
         }
     }
 }