Skip to content

Commit ccb6d0d

Browse files
magic5644glebournaulteXpl0it3r
authored
Update .NET version and enhance error handling with custom exceptions (#46)
* Update .NET version in workflow and project files; add custom exceptions for better error handling * Update macOS version in GitHub Actions workflow to macOS 15 * Remove outdated .NET Core versions from GitHub Actions workflow * Update Sources/DotNetGraph/Exceptions/CompilationException.cs Co-authored-by: Lukas Dürrenberger <eXpl0it3r@my-gate.net> --------- Co-authored-by: Gildas Le Bournault <glebournault@gmail.com> Co-authored-by: Lukas Dürrenberger <eXpl0it3r@my-gate.net>
1 parent 69a5b61 commit ccb6d0d

File tree

7 files changed

+57
-22
lines changed

7 files changed

+57
-22
lines changed

.github/workflows/dotnet.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ jobs:
1616
platform:
1717
- { name: Linux, os: ubuntu-22.04 }
1818
- { name: Windows, os: windows-2022 }
19-
- { name: macOS, os: macos-12 }
19+
- { name: macOS, os: macos-15 }
2020
dotnet:
21-
- { name: .NET Core 3.1, version: "3.1" }
22-
- { name: .NET 5, version: "5.0.x" }
2321
- { name: .NET 6, version: "6.0.x" }
2422
- { name: .NET 7, version: "7.0.x" }
23+
- { name: .NET 8, version: "8.0.x" }
24+
- { name: .NET 9, version: "9.0.x" }
2525

2626
steps:
2727
- uses: actions/checkout@v3
@@ -36,5 +36,5 @@ jobs:
3636
- name: Build
3737
run: dotnet build ./Sources -c Release
3838
- name: Test
39-
if: matrix.dotnet.version == '6.0.x'
39+
if: matrix.dotnet.version == '8.0.x'
4040
run: dotnet test ./Sources -v n --no-restore --no-build -c Release
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>net6.0</TargetFramework>
4+
<TargetFramework>net8.0</TargetFramework>
55
<IsPackable>false</IsPackable>
66
<Nullable>enable</Nullable>
77
</PropertyGroup>
88

99
<ItemGroup>
10-
<PackageReference Include="FluentAssertions" Version="6.11.0"/>
11-
<PackageReference Include="MSTest" Version="3.1.1"/>
10+
<PackageReference Include="FluentAssertions" Version="7.0.0" />
11+
<PackageReference Include="MSTest" Version="3.7.0" />
1212
</ItemGroup>
1313

1414
<ItemGroup>
15-
<ProjectReference Include="..\DotNetGraph\DotNetGraph.csproj"/>
15+
<ProjectReference Include="..\DotNetGraph\DotNetGraph.csproj" />
1616
</ItemGroup>
1717

1818
</Project>

Sources/DotNetGraph/Core/DotEdge.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Threading.Tasks;
44
using DotNetGraph.Attributes;
55
using DotNetGraph.Compilation;
6+
using DotNetGraph.Exceptions;
67

78
namespace DotNetGraph.Core
89
{
@@ -51,7 +52,7 @@ public DotPointAttribute Pos
5152
public override async Task CompileAsync(CompilationContext context)
5253
{
5354
if (From is null || To is null)
54-
throw new Exception("Can't compile edge with null From and/or To");
55+
throw new CompilationException("Can't compile edge with null From and/or To");
5556

5657
await context.WriteIndentationAsync();
5758
await From.CompileAsync(context);

Sources/DotNetGraph/Core/DotElement.cs

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Threading.Tasks;
44
using DotNetGraph.Attributes;
55
using DotNetGraph.Compilation;
6+
using DotNetGraph.Exceptions;
67

78
namespace DotNetGraph.Core
89
{
@@ -28,27 +29,27 @@ public bool HasAttribute(string name)
2829
{
2930
return Attributes.ContainsKey(name);
3031
}
31-
32-
public IDotAttribute GetAttribute(string name)
33-
{
34-
if (Attributes.TryGetValue(name, out var attribute))
35-
return attribute;
36-
throw new Exception($"There is no attribute named '{name}'");
37-
}
3832

39-
public IDotAttribute GetAttributeOrDefault(string name, IDotAttribute defaultValue = default)
33+
public IDotAttribute GetAttribute(string name)
4034
{
4135
if (Attributes.TryGetValue(name, out var attribute))
4236
return attribute;
43-
return defaultValue;
37+
throw new AttributeNotFoundException(name);
4438
}
4539

4640
public T GetAttribute<T>(string name) where T : IDotAttribute
4741
{
4842
var attribute = GetAttribute(name);
4943
if (attribute is T result)
5044
return result;
51-
throw new Exception($"Attribute with name '{name}' doesn't match the expected type (expected: {typeof(T)}, current: {attribute.GetType()})");
45+
throw new AttributeTypeNotMatchException(name, typeof(T), attribute.GetType());
46+
}
47+
48+
public IDotAttribute GetAttributeOrDefault(string name, IDotAttribute defaultValue = default)
49+
{
50+
if (Attributes.TryGetValue(name, out var attribute))
51+
return attribute;
52+
return defaultValue;
5253
}
5354

5455
public T GetAttributeOrDefault<T>(string name, T defaultValue = default) where T : IDotAttribute
@@ -57,7 +58,7 @@ public T GetAttributeOrDefault<T>(string name, T defaultValue = default) where T
5758
{
5859
if (attribute is T result)
5960
return result;
60-
throw new Exception($"Attribute with name '{name}' doesn't match the expected type (expected: {typeof(T)}, current: {attribute.GetType()})");
61+
throw new AttributeTypeNotMatchException(name, typeof(T), attribute.GetType());
6162
}
6263

6364
return defaultValue;
@@ -79,7 +80,7 @@ public bool TryGetAttribute(string name, out IDotAttribute attribute)
7980
public bool TryGetAttribute<T>(string name, out T attribute) where T : IDotAttribute
8081
{
8182
var result = TryGetAttribute(name, out var untypedAttribute);
82-
if (result is false)
83+
if (!result)
8384
{
8485
attribute = default;
8586
return false;
@@ -91,7 +92,7 @@ public bool TryGetAttribute<T>(string name, out T attribute) where T : IDotAttri
9192
return true;
9293
}
9394

94-
throw new Exception($"Attribute with name '{name}' doesn't match the expected type (expected: {typeof(T)}, current: {untypedAttribute.GetType()})");
95+
throw new AttributeTypeNotMatchException(name, typeof(T), untypedAttribute.GetType());
9596
}
9697

9798
public void SetAttribute(string name, IDotAttribute value)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using System;
2+
3+
namespace DotNetGraph.Exceptions
4+
{
5+
public class AttributeNotFoundException : Exception
6+
{
7+
public AttributeNotFoundException(string attributeName) : base($"There is no attribute named '{attributeName}'")
8+
{
9+
}
10+
}
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using System;
2+
3+
namespace DotNetGraph.Exceptions
4+
{
5+
public class AttributeTypeNotMatchException : Exception
6+
{
7+
public AttributeTypeNotMatchException(string attributeName, Type expectedType, Type currentType) : base($"Attribute with name '{attributeName}' doesn't match the expected type (expected: {expectedType}, current: {currentType})")
8+
{
9+
}
10+
}
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using System;
2+
3+
namespace DotNetGraph.Exceptions
4+
{
5+
public class CompilationException : Exception
6+
{
7+
public CompilationException(string compilationError) : base(compilationError)
8+
{
9+
}
10+
}
11+
}

0 commit comments

Comments
 (0)