Skip to content

Commit 2de62e9

Browse files
committed
Throw custom exception when time provider is not set.
1 parent e099927 commit 2de62e9

File tree

4 files changed

+18
-3
lines changed

4 files changed

+18
-3
lines changed

src/HeboTech.TimeService/HeboTech.TimeService.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<PackageReleaseNotes>https://github.com/hbjorgo/TimeService/releases</PackageReleaseNotes>
1111
<PackageTags>time timeservice time-service timefunc time-func</PackageTags>
1212
<RepositoryUrl>https://github.com/hbjorgo/TimeService</RepositoryUrl>
13-
<Version>1.1.0</Version>
13+
<Version>1.2.0</Version>
1414
</PropertyGroup>
1515

1616
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System;
2+
3+
namespace HeboTech.TimeService
4+
{
5+
public class TimeProviderNotSetException : Exception
6+
{
7+
public TimeProviderNotSetException()
8+
{
9+
}
10+
11+
public TimeProviderNotSetException(string message) : base(message)
12+
{
13+
}
14+
}
15+
}

src/HeboTech.TimeService/TimeService.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public static class TimeService
99
/// <summary>
1010
/// Gets a DateTime object that represents the current time
1111
/// </summary>
12-
public static DateTime Now => timeFunc();
12+
public static DateTime Now => timeFunc?.Invoke() ?? throw new TimeProviderNotSetException("Time provider is not set");
1313

1414
/// <summary>
1515
/// Sets the current time. The func can either return a constant time, or a dynamic one (like DateTime.Now).

src/Tests/HeboTech.TimeService.Tests/TimeServiceTests.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public class TimeServiceTests
1010
[Fact]
1111
public void A_SystemTime_now_throws_exception_when_provider_is_not_set_test()
1212
{
13-
Assert.Throws<NullReferenceException>(() => TimeService.Now);
13+
Assert.Throws<TimeProviderNotSetException>(() => TimeService.Now);
1414
}
1515

1616
[Fact]

0 commit comments

Comments
 (0)