Esempio n. 1
0
        private static Exception?HandleErrorCode(int errorCode, string?name, SafeHandle?handle, object?context)
        {
            System.Exception?exception = null;

            switch (errorCode)
            {
            case Interop.Errors.ERROR_INVALID_NAME:
            case Interop.Errors.ERROR_INVALID_HANDLE:
            case Interop.Errors.ERROR_FILE_NOT_FOUND:
                if ((name != null) && (name.Length != 0))
                {
                    exception = new WaitHandleCannotBeOpenedException(SR.Format(SR.WaitHandleCannotBeOpenedException_InvalidHandle, name));
                }
                else
                {
                    exception = new WaitHandleCannotBeOpenedException();
                }
                break;
            }

            return(exception);
        }
Esempio n. 2
0
 public int Closing(bool initiator, System.Exception?ex) => _transceiver.Closing(initiator, ex);
Esempio n. 3
0
        /// <summary>
        /// Enqueues the data.
        /// </summary>
        /// <param name="dataObject">The data object.</param>
        /// <param name="level">The level.</param>
        /// <param name="custom">The custom.</param>
        /// <param name="timeout">The timeout.</param>
        /// <param name="signal">The signal.</param>
        /// <returns>PayloadBundle.</returns>
        internal PayloadBundle?EnqueueData(
            object dataObject,
            ErrorLevel level,
            IDictionary <string, object?>?custom,
            TimeSpan?timeout     = null,
            SemaphoreSlim?signal = null
            )
        {
            // here is the last chance to decide if we need to actually send this payload
            // based on the current config settings and rate-limit conditions:
            if (string.IsNullOrWhiteSpace(this._config.RollbarDestinationOptions.AccessToken) ||
                !this._config.RollbarDeveloperOptions.Enabled ||
                (level < this._config.RollbarDeveloperOptions.LogLevel) ||
                ((this._payloadQueue.AccessTokenQueuesMetadata != null) && this._payloadQueue.AccessTokenQueuesMetadata.IsTransmissionSuspended)
                )
            {
                // nice shortcut:
                return(null);
            }

            if (this._config.RollbarDeveloperOptions.RethrowExceptionsAfterReporting)
            {
                System.Exception?exception = dataObject as System.Exception;
                if (exception == null &&
                    dataObject is Data data &&
                    data.Body != null
                    )
                {
                    exception = data.Body.OriginalException;
                }

                if (exception != null)
                {
                    try
                    {
                        // Here we need to create another logger instance with similar config but configured not to re-throw.
                        // This would prevent infinite recursive calls (in case if we used this instance or any re-throwing instance).
                        // Because we will be re-throwing the exception after reporting, let's report it fully-synchronously.
                        // This logic is on a heavy side. But, fortunately, RethrowExceptionsAfterReporting is intended to be
                        // a development time option:
                        var config = new RollbarLoggerConfig();
                        config.Reconfigure(this._config);
                        config.RollbarDeveloperOptions.RethrowExceptionsAfterReporting = false;
                        using var rollbar = RollbarFactory.CreateNew(config);
                        rollbar.AsBlockingLogger(TimeSpan.FromSeconds(1)).Log(level, dataObject, custom);
                    }
                    catch
                    {
                        // In case there was a TimeoutException (or any un-expected exception),
                        // there is nothing we can do here.
                        // We tried our best...
                    }
                    finally
                    {
                        if (exception is AggregateException aggregateException)
                        {
                            exception = aggregateException.Flatten();
                        }
                        ExceptionDispatchInfo.Capture(exception).Throw();
                    }

                    return(null);
                }
            }


            PayloadBundle?payloadBundle = null;

            try
            {
                payloadBundle = CreatePayloadBundle(dataObject, level, custom, timeout, signal);
            }
            catch (System.Exception exception)
            {
                RollbarErrorUtility.Report(
                    this,
                    dataObject,
                    InternalRollbarError.BundlingError,
                    null,
                    exception,
                    payloadBundle
                    );
                return(null);
            }

            try
            {
                this._payloadQueue.Enqueue(payloadBundle);
            }
            catch (System.Exception exception)
            {
                RollbarErrorUtility.Report(
                    this,
                    dataObject,
                    InternalRollbarError.EnqueuingError,
                    null,
                    exception,
                    payloadBundle
                    );
            }

            return(payloadBundle);
        }
Esempio n. 4
0
 // If we are initiating the connection closure, wait for the peer
 // to close the TCP/IP connection. Otherwise, close immediately.
 public int Closing(bool initiator, System.Exception?ex) =>
 initiator ? SocketOperation.Read : SocketOperation.None;
Esempio n. 5
0
        private static CommonSecurityDescriptor CreateInternal(ResourceType resourceType, bool isContainer, string?name, SafeHandle?handle, AccessControlSections includeSections, bool createByName, ExceptionFromErrorCode?exceptionFromErrorCode, object?exceptionContext)
        {
            int error;
            RawSecurityDescriptor?rawSD;

            if (createByName && name == null)
            {
                throw new ArgumentNullException(nameof(name));
            }
            else if (!createByName && handle == null)
            {
                throw new ArgumentNullException(nameof(handle));
            }

            error = Win32.GetSecurityInfo(resourceType, name, handle, includeSections, out rawSD);

            if (error != Interop.Errors.ERROR_SUCCESS)
            {
                System.Exception?exception = null;

                if (exceptionFromErrorCode != null)
                {
                    exception = exceptionFromErrorCode(error, name, handle, exceptionContext);
                }

                if (exception == null)
                {
                    if (error == Interop.Errors.ERROR_ACCESS_DENIED)
                    {
                        exception = new UnauthorizedAccessException();
                    }
                    else if (error == Interop.Errors.ERROR_INVALID_OWNER)
                    {
                        exception = new InvalidOperationException(SR.AccessControl_InvalidOwner);
                    }
                    else if (error == Interop.Errors.ERROR_INVALID_PRIMARY_GROUP)
                    {
                        exception = new InvalidOperationException(SR.AccessControl_InvalidGroup);
                    }
                    else if (error == Interop.Errors.ERROR_INVALID_PARAMETER)
                    {
                        exception = new InvalidOperationException(SR.Format(SR.AccessControl_UnexpectedError, error));
                    }
                    else if (error == Interop.Errors.ERROR_INVALID_NAME)
                    {
                        exception = new ArgumentException(SR.Argument_InvalidName, nameof(name));
                    }
                    else if (error == Interop.Errors.ERROR_FILE_NOT_FOUND)
                    {
                        exception = new FileNotFoundException(name);
                    }
                    else if (error == Interop.Errors.ERROR_PATH_NOT_FOUND)
                    {
                        exception = isContainer switch
                        {
                            false => new FileNotFoundException(name),
                            true => new DirectoryNotFoundException(name)
                        };
                    }
                    else if (error == Interop.Errors.ERROR_NO_SECURITY_ON_OBJECT)
                    {
                        exception = new NotSupportedException(SR.AccessControl_NoAssociatedSecurity);
                    }
                    else if (error == Interop.Errors.ERROR_PIPE_NOT_CONNECTED)
                    {
                        exception = new InvalidOperationException(SR.InvalidOperation_DisconnectedPipe);
                    }
                    else
                    {
                        Debug.Fail($"Win32GetSecurityInfo() failed with unexpected error code {error}");
                        exception = new InvalidOperationException(SR.Format(SR.AccessControl_UnexpectedError, error));
                    }
                }

                throw exception;
            }

            return(new CommonSecurityDescriptor(isContainer, false /* isDS */, rawSD !, true));
        }
Esempio n. 6
0
        //
        // Attempts to persist the security descriptor onto the object
        //

        private void Persist(string?name, SafeHandle?handle, AccessControlSections includeSections, object?exceptionContext)
        {
            WriteLock();

            try
            {
                int           error;
                SecurityInfos securityInfo = 0;

                SecurityIdentifier?owner = null, group = null;
                SystemAcl?         sacl = null;
                DiscretionaryAcl?  dacl = null;

                if ((includeSections & AccessControlSections.Owner) != 0 && _securityDescriptor.Owner != null)
                {
                    securityInfo |= SecurityInfos.Owner;
                    owner         = _securityDescriptor.Owner;
                }

                if ((includeSections & AccessControlSections.Group) != 0 && _securityDescriptor.Group != null)
                {
                    securityInfo |= SecurityInfos.Group;
                    group         = _securityDescriptor.Group;
                }

                if ((includeSections & AccessControlSections.Audit) != 0)
                {
                    securityInfo |= SecurityInfos.SystemAcl;
                    if (_securityDescriptor.IsSystemAclPresent &&
                        _securityDescriptor.SystemAcl != null &&
                        _securityDescriptor.SystemAcl.Count > 0)
                    {
                        sacl = _securityDescriptor.SystemAcl;
                    }
                    else
                    {
                        sacl = null;
                    }

                    if ((_securityDescriptor.ControlFlags & ControlFlags.SystemAclProtected) != 0)
                    {
                        securityInfo = (SecurityInfos)((uint)securityInfo | ProtectedSystemAcl);
                    }
                    else
                    {
                        securityInfo = (SecurityInfos)((uint)securityInfo | UnprotectedSystemAcl);
                    }
                }

                if ((includeSections & AccessControlSections.Access) != 0 && _securityDescriptor.IsDiscretionaryAclPresent)
                {
                    securityInfo |= SecurityInfos.DiscretionaryAcl;

                    // if the DACL is in fact a crafted replaced for NULL replacement, then we will persist it as NULL
                    if (_securityDescriptor.DiscretionaryAcl !.EveryOneFullAccessForNullDacl)
                    {
                        dacl = null;
                    }
                    else
                    {
                        dacl = _securityDescriptor.DiscretionaryAcl;
                    }

                    if ((_securityDescriptor.ControlFlags & ControlFlags.DiscretionaryAclProtected) != 0)
                    {
                        securityInfo = unchecked ((SecurityInfos)((uint)securityInfo | ProtectedDiscretionaryAcl));
                    }
                    else
                    {
                        securityInfo = (SecurityInfos)((uint)securityInfo | UnprotectedDiscretionaryAcl);
                    }
                }

                if (securityInfo == 0)
                {
                    //
                    // Nothing to persist
                    //

                    return;
                }

                error = Win32.SetSecurityInfo(_resourceType, name, handle, securityInfo, owner, group, sacl, dacl);

                if (error != Interop.Errors.ERROR_SUCCESS)
                {
                    System.Exception?exception = null;

                    if (_exceptionFromErrorCode != null)
                    {
                        exception = _exceptionFromErrorCode(error, name, handle, exceptionContext);
                    }

                    if (exception == null)
                    {
                        if (error == Interop.Errors.ERROR_ACCESS_DENIED)
                        {
                            exception = new UnauthorizedAccessException();
                        }
                        else if (error == Interop.Errors.ERROR_INVALID_OWNER)
                        {
                            exception = new InvalidOperationException(SR.AccessControl_InvalidOwner);
                        }
                        else if (error == Interop.Errors.ERROR_INVALID_PRIMARY_GROUP)
                        {
                            exception = new InvalidOperationException(SR.AccessControl_InvalidGroup);
                        }
                        else if (error == Interop.Errors.ERROR_INVALID_NAME)
                        {
                            exception = new ArgumentException(SR.Argument_InvalidName, nameof(name));
                        }
                        else if (error == Interop.Errors.ERROR_INVALID_HANDLE)
                        {
                            exception = new NotSupportedException(SR.AccessControl_InvalidHandle);
                        }
                        else if (error == Interop.Errors.ERROR_FILE_NOT_FOUND)
                        {
                            exception = new FileNotFoundException();
                        }
                        else if (error == Interop.Errors.ERROR_NO_SECURITY_ON_OBJECT)
                        {
                            exception = new NotSupportedException(SR.AccessControl_NoAssociatedSecurity);
                        }
                        else
                        {
                            Debug.Fail($"Unexpected error code {error}");
                            exception = new InvalidOperationException(SR.Format(SR.AccessControl_UnexpectedError, error));
                        }
                    }

                    throw exception;
                }

                //
                // Everything goes well, let us clean the modified flags.
                // We are in proper write lock, so just go ahead
                //

                this.OwnerModified       = false;
                this.GroupModified       = false;
                this.AccessRulesModified = false;
                this.AuditRulesModified  = false;
            }
Esempio n. 7
0
 public int Closing(bool initiator, System.Exception?ex) => _delegate.Closing(initiator, ex);
Esempio n. 8
0
 public void Publish(MqttNetLogLevel logLevel, string message, object[] parameters, System.Exception?exception)
 {
     Publish(logLevel, source, message, parameters, exception);
 }
Esempio n. 9
0
 public MinimumAvatarPacketCreativeException(string?message, System.Exception?innerException)
     : base(message, innerException)
 {
 }
Esempio n. 10
0
            internal async ValueTask NextConnectorAsync()
            {
                System.Exception?lastException = null;
                for (int i = 0; i < _connectors.Count; ++i)
                {
                    ConnectorInfo connector = _connectors[i];
                    try
                    {
                        ICommunicatorObserver?obsv = _factory._communicator.Observer;
                        if (obsv != null)
                        {
                            _observer = obsv.GetConnectionEstablishmentObserver(connector.Endpoint,
                                                                                connector.Connector.ToString() !);
                            if (_observer != null)
                            {
                                _observer.Attach();
                            }
                        }

                        if (_factory._communicator.TraceLevels.Network >= 2)
                        {
                            _factory._communicator.Logger.Trace(_factory._communicator.TraceLevels.NetworkCat,
                                                                $"trying to establish {connector.Endpoint.Name} connection to " +
                                                                $"{connector.Connector}");
                        }

                        // TODO: Connection establishement code needs to be re-factored to use async/await
                        Connection connection = _factory.CreateConnection(connector.Connector.Connect(), connector);
                        await connection.StartAsync().ConfigureAwait(false);

                        if (_observer != null)
                        {
                            _observer.Detach();
                        }

                        _factory.FinishGetConnection(_connectors, connector, connection, this);
                        return;
                    }
                    catch (CommunicatorDestroyedException ex)
                    {
                        lastException = ex;
                        break; // No need to continue
                    }
                    catch (System.Exception ex)
                    {
                        if (_factory._communicator.TraceLevels.Network >= 2)
                        {
                            Debug.Assert(connector != null);
                            _factory._communicator.Logger.Trace(_factory._communicator.TraceLevels.NetworkCat,
                                                                $"failed to establish {connector.Endpoint.Name} connection to " +
                                                                $"{connector.Connector}\n{ex}");
                        }
                        if (_observer != null)
                        {
                            _observer.Failed(ex.GetType().FullName ?? "System.Exception");
                            _observer.Detach();
                        }
                        _factory.HandleConnectionException(ex, _hasMore || (i + 1) < _connectors.Count);
                        lastException = ex;
                    }
                }

                _factory.FinishGetConnection(_connectors, lastException !, this);
            }