diff --git a/src/Sentry.AspNet/Internal/SystemWebRequestEventProcessor.cs b/src/Sentry.AspNet/Internal/SystemWebRequestEventProcessor.cs index 960a131004..ba4cfbd735 100644 --- a/src/Sentry.AspNet/Internal/SystemWebRequestEventProcessor.cs +++ b/src/Sentry.AspNet/Internal/SystemWebRequestEventProcessor.cs @@ -14,8 +14,11 @@ internal class SystemWebRequestEventProcessor : ISentryEventProcessor public SystemWebRequestEventProcessor(IRequestPayloadExtractor payloadExtractor, SentryOptions options) { - _options = options ?? throw new ArgumentNullException(nameof(options)); - PayloadExtractor = payloadExtractor ?? throw new ArgumentNullException(nameof(payloadExtractor)); + ArgumentNullException.ThrowIfNull(payloadExtractor); + ArgumentNullException.ThrowIfNull(options); + + _options = options; + PayloadExtractor = payloadExtractor; } public SentryEvent? Process(SentryEvent? @event) diff --git a/src/Sentry.AspNetCore.Grpc/ProtobufRequestExtractionDispatcher.cs b/src/Sentry.AspNetCore.Grpc/ProtobufRequestExtractionDispatcher.cs index 39aa1c678f..0749877ba0 100644 --- a/src/Sentry.AspNetCore.Grpc/ProtobufRequestExtractionDispatcher.cs +++ b/src/Sentry.AspNetCore.Grpc/ProtobufRequestExtractionDispatcher.cs @@ -23,9 +23,13 @@ public class ProtobufRequestExtractionDispatcher : IProtobufRequestPayloadExtrac public ProtobufRequestExtractionDispatcher(IEnumerable extractors, SentryOptions options, Func sizeSwitch) { - Extractors = extractors ?? throw new ArgumentNullException(nameof(extractors)); - _options = options ?? throw new ArgumentNullException(nameof(options)); - _sizeSwitch = sizeSwitch ?? throw new ArgumentNullException(nameof(sizeSwitch)); + ArgumentNullException.ThrowIfNull(extractors); + ArgumentNullException.ThrowIfNull(options); + ArgumentNullException.ThrowIfNull(sizeSwitch); + + Extractors = extractors; + _options = options; + _sizeSwitch = sizeSwitch; } /// diff --git a/src/Sentry.AspNetCore.Grpc/SentryGrpcInterceptor.cs b/src/Sentry.AspNetCore.Grpc/SentryGrpcInterceptor.cs index dd26151d99..c08fd6ccfc 100644 --- a/src/Sentry.AspNetCore.Grpc/SentryGrpcInterceptor.cs +++ b/src/Sentry.AspNetCore.Grpc/SentryGrpcInterceptor.cs @@ -27,7 +27,9 @@ public SentryGrpcInterceptor( Func hubAccessor, IOptions options) { - _hubAccessor = hubAccessor ?? throw new ArgumentNullException(nameof(hubAccessor)); + ArgumentNullException.ThrowIfNull(hubAccessor); + + _hubAccessor = hubAccessor; _options = options.Value; var hub = _hubAccessor(); foreach (var callback in _options.ConfigureScopeCallbacks) diff --git a/src/Sentry.AspNetCore/SentryAspNetCoreBuilder.cs b/src/Sentry.AspNetCore/SentryAspNetCoreBuilder.cs index 00ff5ef3f4..180ce95779 100644 --- a/src/Sentry.AspNetCore/SentryAspNetCoreBuilder.cs +++ b/src/Sentry.AspNetCore/SentryAspNetCoreBuilder.cs @@ -11,6 +11,8 @@ internal class SentryAspNetCoreBuilder : ISentryBuilder public SentryAspNetCoreBuilder(IServiceCollection services) { - Services = services ?? throw new ArgumentNullException(nameof(services)); + ArgumentNullException.ThrowIfNull(services); + + Services = services; } } diff --git a/src/Sentry.AspNetCore/SentryMiddleware.cs b/src/Sentry.AspNetCore/SentryMiddleware.cs index 0b710a99b3..3f7b9f54f6 100644 --- a/src/Sentry.AspNetCore/SentryMiddleware.cs +++ b/src/Sentry.AspNetCore/SentryMiddleware.cs @@ -62,7 +62,9 @@ public SentryMiddleware( IEnumerable eventProcessors, IEnumerable transactionProcessors) { - _getHub = getHub ?? throw new ArgumentNullException(nameof(getHub)); + ArgumentNullException.ThrowIfNull(getHub); + + _getHub = getHub; _options = options.Value; _hostingEnvironment = hostingEnvironment; _logger = logger; diff --git a/src/Sentry.Extensions.Logging/DelegateLogEntryFilter.cs b/src/Sentry.Extensions.Logging/DelegateLogEntryFilter.cs index ef05e073dc..f8ead34ab8 100644 --- a/src/Sentry.Extensions.Logging/DelegateLogEntryFilter.cs +++ b/src/Sentry.Extensions.Logging/DelegateLogEntryFilter.cs @@ -15,7 +15,11 @@ public class DelegateLogEntryFilter : ILogEntryFilter /// /// public DelegateLogEntryFilter(Func filter) - => _filter = filter ?? throw new ArgumentNullException(nameof(filter)); + { + ArgumentNullException.ThrowIfNull(filter); + + _filter = filter; + } /// public bool Filter( diff --git a/src/Sentry/Extensibility/RequestBodyExtractionDispatcher.cs b/src/Sentry/Extensibility/RequestBodyExtractionDispatcher.cs index be8d0ad014..09d03ca6db 100644 --- a/src/Sentry/Extensibility/RequestBodyExtractionDispatcher.cs +++ b/src/Sentry/Extensibility/RequestBodyExtractionDispatcher.cs @@ -20,9 +20,13 @@ public class RequestBodyExtractionDispatcher : IRequestPayloadExtractor /// The max request size to capture. public RequestBodyExtractionDispatcher(IEnumerable extractors, SentryOptions options, Func sizeSwitch) { - Extractors = extractors ?? throw new ArgumentNullException(nameof(extractors)); - _options = options ?? throw new ArgumentNullException(nameof(options)); - _sizeSwitch = sizeSwitch ?? throw new ArgumentNullException(nameof(sizeSwitch)); + ArgumentNullException.ThrowIfNull(extractors); + ArgumentNullException.ThrowIfNull(options); + ArgumentNullException.ThrowIfNull(sizeSwitch); + + Extractors = extractors; + _options = options; + _sizeSwitch = sizeSwitch; } /// diff --git a/src/Sentry/Internal/Http/DefaultSentryHttpClientFactory.cs b/src/Sentry/Internal/Http/DefaultSentryHttpClientFactory.cs index e6746283a6..98bcc91d8c 100644 --- a/src/Sentry/Internal/Http/DefaultSentryHttpClientFactory.cs +++ b/src/Sentry/Internal/Http/DefaultSentryHttpClientFactory.cs @@ -15,10 +15,7 @@ internal class DefaultSentryHttpClientFactory : ISentryHttpClientFactory /// The HTTP options. public HttpClient Create(SentryOptions options) { - if (options == null) - { - throw new ArgumentNullException(nameof(options)); - } + ArgumentNullException.ThrowIfNull(options); var handler = options.CreateHttpMessageHandler?.Invoke() ?? new HttpClientHandler(); if (handler is HttpClientHandler httpClientHandler) diff --git a/src/Sentry/Internal/Http/RetryAfterHandler.cs b/src/Sentry/Internal/Http/RetryAfterHandler.cs index 8478011d4d..a752a7c439 100644 --- a/src/Sentry/Internal/Http/RetryAfterHandler.cs +++ b/src/Sentry/Internal/Http/RetryAfterHandler.cs @@ -28,7 +28,11 @@ public RetryAfterHandler(HttpMessageHandler innerHandler) internal RetryAfterHandler(HttpMessageHandler innerHandler, ISystemClock clock) : base(innerHandler) - => _clock = clock ?? throw new ArgumentNullException(nameof(clock)); + { + ArgumentNullException.ThrowIfNull(clock); + + _clock = clock; + } /// /// Sends an HTTP request to the inner handler while verifying the Response status code for HTTP 429. diff --git a/src/Sentry/Internal/Polyfills.cs b/src/Sentry/Internal/Polyfills.cs index f5ce518ec3..e525de2b5e 100644 --- a/src/Sentry/Internal/Polyfills.cs +++ b/src/Sentry/Internal/Polyfills.cs @@ -69,10 +69,7 @@ internal static class EnumerableExtensions { internal static bool TryGetNonEnumeratedCount(this IEnumerable source, out int count) { - if (source is null) - { - throw new ArgumentNullException(nameof(source)); - } + ArgumentNullException.ThrowIfNull(source); if (source is ICollection genericCollection) { @@ -91,3 +88,26 @@ internal static bool TryGetNonEnumeratedCount(this IEnumerable } } #endif + +// TODO: remove when updating Polyfill: https://github.com/getsentry/sentry-dotnet/pull/4879 +#if !NET6_0_OR_GREATER +internal static class ArgumentNullExceptionExtensions +{ + extension(ArgumentNullException) + { + public static void ThrowIfNull([NotNull] object? argument, [CallerArgumentExpression(nameof(argument))] string? paramName = null) + { + if (argument is null) + { + Throw(paramName); + } + } + } + + [DoesNotReturn] + private static void Throw(string? paramName) + { + throw new ArgumentNullException(paramName); + } +} +#endif diff --git a/src/Sentry/Internal/SdkComposer.cs b/src/Sentry/Internal/SdkComposer.cs index d7263f8765..96a9e2015d 100644 --- a/src/Sentry/Internal/SdkComposer.cs +++ b/src/Sentry/Internal/SdkComposer.cs @@ -12,7 +12,9 @@ internal class SdkComposer public SdkComposer(SentryOptions options, BackpressureMonitor? backpressureMonitor) { - _options = options ?? throw new ArgumentNullException(nameof(options)); + ArgumentNullException.ThrowIfNull(options); + + _options = options; if (options.Dsn is null) { throw new ArgumentException("No DSN defined in the SentryOptions"); diff --git a/src/Sentry/SentryClient.cs b/src/Sentry/SentryClient.cs index 64da285d5e..42806fd05b 100644 --- a/src/Sentry/SentryClient.cs +++ b/src/Sentry/SentryClient.cs @@ -45,7 +45,9 @@ internal SentryClient( ISessionManager? sessionManager = null, BackpressureMonitor? backpressureMonitor = null) { - _options = options ?? throw new ArgumentNullException(nameof(options)); + ArgumentNullException.ThrowIfNull(options); + + _options = options; _backpressureMonitor = backpressureMonitor; _randomValuesFactory = randomValuesFactory ?? new SynchronizedRandomValuesFactory(); _sessionManager = sessionManager ?? new GlobalSessionManager(options); diff --git a/src/Sentry/SentryOptions.cs b/src/Sentry/SentryOptions.cs index e8bd13d457..25a6eb3183 100644 --- a/src/Sentry/SentryOptions.cs +++ b/src/Sentry/SentryOptions.cs @@ -1204,10 +1204,7 @@ public StackTraceMode StackTraceMode public void AddJsonConverter(JsonConverter converter) { // protect against null because user may not have nullability annotations enabled - if (converter == null!) - { - throw new ArgumentNullException(nameof(converter)); - } + ArgumentNullException.ThrowIfNull(converter); JsonExtensions.AddJsonConverter(converter); } @@ -1228,10 +1225,7 @@ public void AddJsonSerializerContext(Func contextBu where T : JsonSerializerContext { // protect against null because user may not have nullability annotations enabled - if (contextBuilder == null!) - { - throw new ArgumentNullException(nameof(contextBuilder)); - } + ArgumentNullException.ThrowIfNull(contextBuilder); JsonExtensions.AddJsonSerializerContext(contextBuilder); } @@ -1717,7 +1711,9 @@ public IEnumerable GetAllExceptionProcessors() /// The stack trace factory. public SentryOptions UseStackTraceFactory(ISentryStackTraceFactory sentryStackTraceFactory) { - SentryStackTraceFactory = sentryStackTraceFactory ?? throw new ArgumentNullException(nameof(sentryStackTraceFactory)); + ArgumentNullException.ThrowIfNull(sentryStackTraceFactory); + + SentryStackTraceFactory = sentryStackTraceFactory; return this; } diff --git a/test/Sentry.Tests/Protocol/DsnTests.cs b/test/Sentry.Tests/Protocol/DsnTests.cs index 41e91b702e..d9eb15349c 100644 --- a/test/Sentry.Tests/Protocol/DsnTests.cs +++ b/test/Sentry.Tests/Protocol/DsnTests.cs @@ -279,15 +279,8 @@ public override string ToString() private static void AssertEqual(DsnTestCase @case, Dsn dsn) { - if (@case == null) - { - throw new ArgumentNullException(nameof(@case)); - } - - if (dsn == null) - { - throw new ArgumentNullException(nameof(dsn)); - } + Assert.NotNull(@case); + Assert.NotNull(dsn); var uri = dsn.GetStoreEndpointUri();