Skip to content

Commit a4a8ded

Browse files
CopilotjamescrosswellFlash0ver
authored
ref: Use .NET 6.0 ArgumentNullException throw helpers (#4985)
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: jamescrosswell <728212+jamescrosswell@users.noreply.github.com> Co-authored-by: Stefan Pölz <38893694+Flash0ver@users.noreply.github.com>
1 parent 90c1206 commit a4a8ded

File tree

14 files changed

+76
-41
lines changed

14 files changed

+76
-41
lines changed

src/Sentry.AspNet/Internal/SystemWebRequestEventProcessor.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,11 @@ internal class SystemWebRequestEventProcessor : ISentryEventProcessor
1414

1515
public SystemWebRequestEventProcessor(IRequestPayloadExtractor payloadExtractor, SentryOptions options)
1616
{
17-
_options = options ?? throw new ArgumentNullException(nameof(options));
18-
PayloadExtractor = payloadExtractor ?? throw new ArgumentNullException(nameof(payloadExtractor));
17+
ArgumentNullException.ThrowIfNull(payloadExtractor);
18+
ArgumentNullException.ThrowIfNull(options);
19+
20+
_options = options;
21+
PayloadExtractor = payloadExtractor;
1922
}
2023

2124
public SentryEvent? Process(SentryEvent? @event)

src/Sentry.AspNetCore.Grpc/ProtobufRequestExtractionDispatcher.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,13 @@ public class ProtobufRequestExtractionDispatcher : IProtobufRequestPayloadExtrac
2323
public ProtobufRequestExtractionDispatcher(IEnumerable<IProtobufRequestPayloadExtractor> extractors,
2424
SentryOptions options, Func<RequestSize> sizeSwitch)
2525
{
26-
Extractors = extractors ?? throw new ArgumentNullException(nameof(extractors));
27-
_options = options ?? throw new ArgumentNullException(nameof(options));
28-
_sizeSwitch = sizeSwitch ?? throw new ArgumentNullException(nameof(sizeSwitch));
26+
ArgumentNullException.ThrowIfNull(extractors);
27+
ArgumentNullException.ThrowIfNull(options);
28+
ArgumentNullException.ThrowIfNull(sizeSwitch);
29+
30+
Extractors = extractors;
31+
_options = options;
32+
_sizeSwitch = sizeSwitch;
2933
}
3034

3135
/// <summary>

src/Sentry.AspNetCore.Grpc/SentryGrpcInterceptor.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ public SentryGrpcInterceptor(
2727
Func<IHub> hubAccessor,
2828
IOptions<SentryAspNetCoreOptions> options)
2929
{
30-
_hubAccessor = hubAccessor ?? throw new ArgumentNullException(nameof(hubAccessor));
30+
ArgumentNullException.ThrowIfNull(hubAccessor);
31+
32+
_hubAccessor = hubAccessor;
3133
_options = options.Value;
3234
var hub = _hubAccessor();
3335
foreach (var callback in _options.ConfigureScopeCallbacks)

src/Sentry.AspNetCore/SentryAspNetCoreBuilder.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ internal class SentryAspNetCoreBuilder : ISentryBuilder
1111

1212
public SentryAspNetCoreBuilder(IServiceCollection services)
1313
{
14-
Services = services ?? throw new ArgumentNullException(nameof(services));
14+
ArgumentNullException.ThrowIfNull(services);
15+
16+
Services = services;
1517
}
1618
}

src/Sentry.AspNetCore/SentryMiddleware.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,9 @@ public SentryMiddleware(
6262
IEnumerable<ISentryEventProcessor> eventProcessors,
6363
IEnumerable<ISentryTransactionProcessor> transactionProcessors)
6464
{
65-
_getHub = getHub ?? throw new ArgumentNullException(nameof(getHub));
65+
ArgumentNullException.ThrowIfNull(getHub);
66+
67+
_getHub = getHub;
6668
_options = options.Value;
6769
_hostingEnvironment = hostingEnvironment;
6870
_logger = logger;

src/Sentry.Extensions.Logging/DelegateLogEntryFilter.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@ public class DelegateLogEntryFilter : ILogEntryFilter
1515
/// </summary>
1616
/// <param name="filter"></param>
1717
public DelegateLogEntryFilter(Func<string, LogLevel, EventId, Exception?, bool> filter)
18-
=> _filter = filter ?? throw new ArgumentNullException(nameof(filter));
18+
{
19+
ArgumentNullException.ThrowIfNull(filter);
20+
21+
_filter = filter;
22+
}
1923

2024
/// <inheritdoc />
2125
public bool Filter(

src/Sentry/Extensibility/RequestBodyExtractionDispatcher.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,13 @@ public class RequestBodyExtractionDispatcher : IRequestPayloadExtractor
2020
/// <param name="sizeSwitch">The max request size to capture.</param>
2121
public RequestBodyExtractionDispatcher(IEnumerable<IRequestPayloadExtractor> extractors, SentryOptions options, Func<RequestSize> sizeSwitch)
2222
{
23-
Extractors = extractors ?? throw new ArgumentNullException(nameof(extractors));
24-
_options = options ?? throw new ArgumentNullException(nameof(options));
25-
_sizeSwitch = sizeSwitch ?? throw new ArgumentNullException(nameof(sizeSwitch));
23+
ArgumentNullException.ThrowIfNull(extractors);
24+
ArgumentNullException.ThrowIfNull(options);
25+
ArgumentNullException.ThrowIfNull(sizeSwitch);
26+
27+
Extractors = extractors;
28+
_options = options;
29+
_sizeSwitch = sizeSwitch;
2630
}
2731

2832
/// <summary>

src/Sentry/Internal/Http/DefaultSentryHttpClientFactory.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,7 @@ internal class DefaultSentryHttpClientFactory : ISentryHttpClientFactory
1515
/// <param name="options">The HTTP options.</param>
1616
public HttpClient Create(SentryOptions options)
1717
{
18-
if (options == null)
19-
{
20-
throw new ArgumentNullException(nameof(options));
21-
}
18+
ArgumentNullException.ThrowIfNull(options);
2219

2320
var handler = options.CreateHttpMessageHandler?.Invoke() ?? new HttpClientHandler();
2421
if (handler is HttpClientHandler httpClientHandler)

src/Sentry/Internal/Http/RetryAfterHandler.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,11 @@ public RetryAfterHandler(HttpMessageHandler innerHandler)
2828

2929
internal RetryAfterHandler(HttpMessageHandler innerHandler, ISystemClock clock)
3030
: base(innerHandler)
31-
=> _clock = clock ?? throw new ArgumentNullException(nameof(clock));
31+
{
32+
ArgumentNullException.ThrowIfNull(clock);
33+
34+
_clock = clock;
35+
}
3236

3337
/// <summary>
3438
/// Sends an HTTP request to the inner handler while verifying the Response status code for HTTP 429.

src/Sentry/Internal/Polyfills.cs

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,7 @@ internal static class EnumerableExtensions
6969
{
7070
internal static bool TryGetNonEnumeratedCount<TSource>(this IEnumerable<TSource> source, out int count)
7171
{
72-
if (source is null)
73-
{
74-
throw new ArgumentNullException(nameof(source));
75-
}
72+
ArgumentNullException.ThrowIfNull(source);
7673

7774
if (source is ICollection<TSource> genericCollection)
7875
{
@@ -91,3 +88,26 @@ internal static bool TryGetNonEnumeratedCount<TSource>(this IEnumerable<TSource>
9188
}
9289
}
9390
#endif
91+
92+
// TODO: remove when updating Polyfill: https://github.com/getsentry/sentry-dotnet/pull/4879
93+
#if !NET6_0_OR_GREATER
94+
internal static class ArgumentNullExceptionExtensions
95+
{
96+
extension(ArgumentNullException)
97+
{
98+
public static void ThrowIfNull([NotNull] object? argument, [CallerArgumentExpression(nameof(argument))] string? paramName = null)
99+
{
100+
if (argument is null)
101+
{
102+
Throw(paramName);
103+
}
104+
}
105+
}
106+
107+
[DoesNotReturn]
108+
private static void Throw(string? paramName)
109+
{
110+
throw new ArgumentNullException(paramName);
111+
}
112+
}
113+
#endif

0 commit comments

Comments
 (0)