Skip to content

Commit 0aed969

Browse files
Throw ArgumentNullException if DSN is null (#2655)
1 parent 0c386ee commit 0aed969

File tree

9 files changed

+31
-31
lines changed

9 files changed

+31
-31
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ without native/platform specific bindings and SDKs. See [this ticket for more de
1515
- Drop .NET 6 Mobile in favor of .NET 7 ([#2624](https://github.com/getsentry/sentry-dotnet/pull/2604))
1616

1717
API Changes:
18-
18+
- If `null` has been supplied as DSN when initializing Sentry, and ArgumentNullException is now thrown ([#2655](https://github.com/getsentry/sentry-dotnet/pull/2655))
1919
- IHasBreadcrumbs was removed. Use IEventLike instead. ([#2670](https://github.com/getsentry/sentry-dotnet/pull/2670))
2020
- ISpanContext was removed. Use ITraceContext instead. ([#2668](https://github.com/getsentry/sentry-dotnet/pull/2668))
2121
- Removed IHasTransactionNameSource. Use ITransactionContext instead. ([#2654](https://github.com/getsentry/sentry-dotnet/pull/2654))

src/Sentry/Internal/SettingLocator.cs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,19 +35,15 @@ public string GetDsn()
3535
// For DSN only
3636
// An empty string set on the option should NOT be converted to null because it is used
3737
// to indicate the the SDK is disabled.
38-
39-
var dsn = _options.Dsn;
40-
if (dsn != null)
38+
_options.Dsn ??= GetEnvironmentVariable(Constants.DsnEnvironmentVariable)
39+
?? AssemblyForAttributes?.GetCustomAttribute<DsnAttribute>()?.Dsn;
40+
if (_options.Dsn is null)
4141
{
42-
return dsn;
42+
throw new ArgumentNullException("You must supply a DSN to use Sentry." +
43+
"To disable Sentry, pass an empty string: \"\"." +
44+
"See https://docs.sentry.io/platforms/dotnet/configuration/options/#dsn");
4345
}
44-
45-
dsn = GetEnvironmentVariable(Constants.DsnEnvironmentVariable)
46-
?? AssemblyForAttributes?.GetCustomAttribute<DsnAttribute>()?.Dsn
47-
?? Sentry.Constants.DisableSdkDsnValue;
48-
49-
_options.Dsn = dsn;
50-
return dsn;
46+
return _options.Dsn;
5147
}
5248

5349
public string GetEnvironment() => GetEnvironment(true)!;

src/Sentry/SentrySdk.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ internal static IHub InitHub(SentryOptions options)
4040
// from anywhere else, return a disabled hub.
4141
if (Dsn.IsDisabled(dsnString))
4242
{
43-
options.LogWarning("Init was called but no DSN was provided nor located. Sentry SDK will be disabled.");
43+
options.LogWarning("Init called with an empty string as the DSN. Sentry SDK will be disabled.");
4444
return DisabledHub.Instance;
4545
}
4646

test/Sentry.AspNetCore.Tests/AspNetCoreSentryWebHostBuilder.IntegrationTests.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,9 @@ public void UseSentry_ValidDsnString_EnablesSdk()
2727
}
2828

2929
[Fact]
30-
public void UseSentry_NoDsnProvided_DisabledSdk()
30+
public void UseSentry_NoDsnProvided_ThrowsException()
3131
{
32-
_ = _webHostBuilder.UseSentry().Build();
33-
34-
Assert.False(SentrySdk.IsEnabled);
32+
Assert.Throws<ArgumentNullException>(() => _webHostBuilder.UseSentry().Build());
3533
}
3634

3735
[Fact]

test/Sentry.Extensions.Logging.Tests/SentryLoggerFactoryExtensionsTests.cs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ public void AddSentry_NoDiagnosticSet_MelSet()
6464
var sut = Substitute.For<ILoggerFactory>();
6565
_ = sut.AddSentry(o =>
6666
{
67+
o.Dsn = Sentry.Constants.DisableSdkDsnValue;
6768
o.Debug = true;
6869
options = o;
6970
});
@@ -79,6 +80,7 @@ public void AddSentry_DiagnosticSet_NoOverriden()
7980
var diagnosticLogger = Substitute.For<IDiagnosticLogger>();
8081
_ = sut.AddSentry(o =>
8182
{
83+
o.Dsn = Sentry.Constants.DisableSdkDsnValue;
8284
o.Debug = true;
8385
Assert.Null(o.DiagnosticLogger);
8486
o.DiagnosticLogger = diagnosticLogger;
@@ -93,7 +95,11 @@ public void AddSentry_WithOptionsCallback_CallbackInvoked()
9395
{
9496
var callbackInvoked = false;
9597
var expected = Substitute.For<ILoggerFactory>();
96-
_ = expected.AddSentry(_ => callbackInvoked = true);
98+
_ = expected.AddSentry(o =>
99+
{
100+
o.Dsn = Sentry.Constants.DisableSdkDsnValue;
101+
callbackInvoked = true;
102+
});
97103

98104
Assert.True(callbackInvoked);
99105
}
@@ -112,7 +118,7 @@ public void AddSentry_NoOptionsDelegate_ProviderAdded()
112118
public void AddSentry_ReturnsSameFactory()
113119
{
114120
var expected = Substitute.For<ILoggerFactory>();
115-
var actual = expected.AddSentry();
121+
var actual = expected.AddSentry(o => o.Dsn = Sentry.Constants.DisableSdkDsnValue);
116122

117123
Assert.Same(expected, actual);
118124
}
@@ -121,7 +127,7 @@ public void AddSentry_ReturnsSameFactory()
121127
public void AddSentry_ConfigureOptionsOverload_ReturnsSameFactory()
122128
{
123129
var expected = Substitute.For<ILoggerFactory>();
124-
var actual = expected.AddSentry(_ => { });
130+
var actual = expected.AddSentry(o => o.Dsn = Sentry.Constants.DisableSdkDsnValue);
125131

126132
Assert.Same(expected, actual);
127133
}
@@ -134,6 +140,7 @@ public void AddSentry_ConfigureOptionsOverload_InvokesCallback()
134140
var invoked = false;
135141
_ = expected.AddSentry(o =>
136142
{
143+
o.Dsn = Sentry.Constants.DisableSdkDsnValue;
137144
Assert.NotNull(o);
138145
invoked = true;
139146
});

test/Sentry.NLog.Tests/SentryTargetTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,7 @@ void Continuation(Exception _)
454454
[Fact]
455455
public void InitializeTarget_InitializesSdk()
456456
{
457-
_fixture.Options.Dsn = null;
457+
_fixture.Options.Dsn = Sentry.Constants.DisableSdkDsnValue;
458458
_fixture.SdkDisposeHandle = null;
459459
_fixture.Options.InitializeSdk = true;
460460

@@ -468,7 +468,7 @@ public void InitializeTarget_InitializesSdk()
468468
_fixture.GetLoggerFactory();
469469

470470
var logOutput = logWriter.ToString();
471-
Assert.Contains("Init was called but no DSN was provided nor located. Sentry SDK will be disabled.", logOutput);
471+
Assert.Contains("Init called with an empty string as the DSN. Sentry SDK will be disabled.", logOutput);
472472
}
473473
finally
474474
{

test/Sentry.Serilog.Tests/SerilogAspNetSentrySdkTestFixture.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ protected override void ConfigureBuilder(WebHostBuilder builder)
2828
builder.ConfigureLogging(loggingBuilder =>
2929
{
3030
var logger = new LoggerConfiguration()
31-
.WriteTo.Sentry()
31+
.WriteTo.Sentry(ValidDsn)
3232
.CreateLogger();
3333
loggingBuilder.AddSerilog(logger);
3434
});

test/Sentry.Tests/Internals/SettingLocatorTests.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -153,14 +153,11 @@ public void GetDsn_WithOptionAlreadySet_IgnoresAttribute()
153153
}
154154

155155
[Fact]
156-
public void GetDsn_WithNoValueAnywhere_ReturnsAndSetsDisabledDsn()
156+
public void GetDsn_WithNoValueAnywhere_ThrowsException()
157157
{
158158
var options = new SentryOptions();
159159

160-
var dsn = options.SettingLocator.GetDsn();
161-
162-
Assert.Equal(DisableSdkDsnValue, dsn);
163-
Assert.Equal(DisableSdkDsnValue, options.Dsn);
160+
Assert.Throws<ArgumentNullException>(() => options.SettingLocator.GetDsn());
164161
}
165162

166163
[Fact]

test/Sentry.Tests/SentrySdkTests.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,14 +141,15 @@ public void Init_EmptyDsn_LogsWarning()
141141
{
142142
var options = new SentryOptions
143143
{
144+
Dsn = Constants.DisableSdkDsnValue,
144145
DiagnosticLogger = _logger,
145146
Debug = true,
146147
InitNativeSdks = false
147148
};
148149

149150
using (SentrySdk.Init(options))
150151
{
151-
_logger.Received(1).Log(SentryLevel.Warning, "Init was called but no DSN was provided nor located. Sentry SDK will be disabled.");
152+
_logger.Received(1).Log(SentryLevel.Warning, "Init called with an empty string as the DSN. Sentry SDK will be disabled.");
152153
}
153154
}
154155

@@ -176,6 +177,7 @@ public void Init_EmptyDsnDisabledDiagnostics_DoesNotLogWarning()
176177
{
177178
var options = new SentryOptions
178179
{
180+
Dsn = Constants.DisableSdkDsnValue,
179181
DiagnosticLogger = _logger,
180182
Debug = false,
181183
InitNativeSdks = false,
@@ -821,14 +823,14 @@ public void Implements_ScopeManagement()
821823
[Fact]
822824
public void InitHub_NoDsn_DisposeDoesNotThrow()
823825
{
824-
var sut = SentrySdk.InitHub(new SentryOptions()) as IDisposable;
826+
var sut = SentrySdk.InitHub(new SentryOptions(){Dsn = Constants.DisableSdkDsnValue}) as IDisposable;
825827
sut?.Dispose();
826828
}
827829

828830
[Fact]
829831
public async Task InitHub_NoDsn_FlushAsyncDoesNotThrow()
830832
{
831-
var sut = SentrySdk.InitHub(new SentryOptions());
833+
var sut = SentrySdk.InitHub(new SentryOptions(){Dsn = Constants.DisableSdkDsnValue});
832834
await sut.FlushAsync();
833835
}
834836

0 commit comments

Comments
 (0)