Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions CR.Exceptions.AspNet.Tests/Component/LogLevelMapTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using CR.Exceptions.AspNet.Mapping;
using CR.Exceptions.Tests.Shared;
using CR.Exceptions.Tests.Shared;
using Microsoft.Extensions.Logging;

namespace CR.Exceptions.AspNet.Tests.Component;
Expand Down
3 changes: 1 addition & 2 deletions CR.Exceptions.AspNet.Tests/Component/StatusCodeMapTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using CR.Exceptions.AspNet.Mapping;
using CR.Exceptions.Tests.Shared;
using CR.Exceptions.Tests.Shared;
using Microsoft.AspNetCore.Http;

namespace CR.Exceptions.AspNet.Tests.Component;
Expand Down
3 changes: 1 addition & 2 deletions CR.Exceptions.AspNet.Tests/Unit/LogLevelMapBuilderTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using CR.Exceptions.AspNet.Mapping;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging;

namespace CR.Exceptions.AspNet.Tests.Unit;

Expand Down
3 changes: 1 addition & 2 deletions CR.Exceptions.AspNet.Tests/Unit/StatusCodeMapBuilderTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using CR.Exceptions.AspNet.Mapping;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http;

namespace CR.Exceptions.AspNet.Tests.Unit;

Expand Down
3 changes: 1 addition & 2 deletions CR.Exceptions.AspNet/CrExceptionHandler.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using CR.Exceptions.AspNet.Mapping;
using Microsoft.AspNetCore.Diagnostics;
using Microsoft.AspNetCore.Diagnostics;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
Expand Down
5 changes: 2 additions & 3 deletions CR.Exceptions.AspNet/Mapping/LogLevelMap.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
using CR.Exceptions.Mapping;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging;
using System.Collections.Frozen;
using System.Diagnostics.CodeAnalysis;

namespace CR.Exceptions.AspNet.Mapping;
namespace CR.Exceptions.AspNet;

public class LogLevelMap : TypeMap<LogLevel>
{
Expand Down
5 changes: 2 additions & 3 deletions CR.Exceptions.AspNet/Mapping/LogLevelMapBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using CR.Exceptions.Mapping;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging;

namespace CR.Exceptions.AspNet.Mapping;
namespace CR.Exceptions.AspNet;

public class LogLevelMapBuilder : MapBuilder<Type, LogLevel>
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Microsoft.Extensions.Logging;

namespace CR.Exceptions.AspNet.Mapping;
namespace CR.Exceptions.AspNet;

public static class LogLevelMapBuilderExtensions
{
Expand Down
5 changes: 2 additions & 3 deletions CR.Exceptions.AspNet/Mapping/StatusCodeMap.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
using CR.Exceptions.Mapping;
using System.Collections.Frozen;
using System.Collections.Frozen;
using System.Diagnostics.CodeAnalysis;

namespace CR.Exceptions.AspNet.Mapping;
namespace CR.Exceptions.AspNet;

public class StatusCodeMap : TypeMap<int>
{
Expand Down
5 changes: 2 additions & 3 deletions CR.Exceptions.AspNet/Mapping/StatusCodeMapBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using CR.Exceptions.Mapping;
using System.Net;
using System.Net;

namespace CR.Exceptions.AspNet.Mapping;
namespace CR.Exceptions.AspNet;

public class StatusCodeMapBuilder : MapBuilder<Type, int>
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Microsoft.AspNetCore.Http;

namespace CR.Exceptions.AspNet.Mapping;
namespace CR.Exceptions.AspNet;

public static class StatusCodeMapBuilderExtensions
{
Expand Down
3 changes: 1 addition & 2 deletions CR.Exceptions.AspNet/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using CR.Exceptions.AspNet.Mapping;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection;

namespace CR.Exceptions.AspNet;

Expand Down
2 changes: 1 addition & 1 deletion CR.Exceptions.Tests.Shared/TestUnknownException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ public sealed class TestUnknownException : CrException
{
private static readonly ImmutableArray<CrError> _errors = [new("TestUnknownCode", "TestUnknownMessage")];

public TestUnknownException() : base(_errors, "Test unknown exception message") { }
public TestUnknownException(Exception? innerException = null) : base(_errors, "Test unknown exception message", innerException) { }
}
3 changes: 1 addition & 2 deletions CR.Exceptions.Tests/Component/ExceptionFactoryTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using CR.Exceptions.Mapping;
using CR.Exceptions.Tests.Shared;
using CR.Exceptions.Tests.Shared;

namespace CR.Exceptions.Tests.Component;

Expand All @@ -9,7 +8,7 @@
private const string NonExistentCode = "non_existent_code";

[Fact]
public void TryCreate_ShouldReturn_TrueAndException_WhenCodeExists()

Check warning on line 11 in CR.Exceptions.Tests/Component/ExceptionFactoryTests.cs

View workflow job for this annotation

GitHub Actions / test

Remove the underscores from member name CR.Exceptions.Tests.Component.ExceptionFactoryTests.TryCreate_ShouldReturn_TrueAndException_WhenCodeExists() (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1707)
{
var factory = GetDefaultFactory(ExistentCode);
var result = factory.TryCreate(ExistentCode, out var exception);
Expand All @@ -20,7 +19,7 @@
}

[Fact]
public void TryCreate_ShouldReturn_FalseAndNull_WhenCodeDoesNotExist()

Check warning on line 22 in CR.Exceptions.Tests/Component/ExceptionFactoryTests.cs

View workflow job for this annotation

GitHub Actions / test

Remove the underscores from member name CR.Exceptions.Tests.Component.ExceptionFactoryTests.TryCreate_ShouldReturn_FalseAndNull_WhenCodeDoesNotExist() (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1707)
{
var factory = GetDefaultFactory("?");
var result = factory.TryCreate(NonExistentCode, out var exception);
Expand All @@ -30,7 +29,7 @@
}

[Fact]
public void Create_ShouldReturn_Exception_WhenCodeExists()

Check warning on line 32 in CR.Exceptions.Tests/Component/ExceptionFactoryTests.cs

View workflow job for this annotation

GitHub Actions / test

Remove the underscores from member name CR.Exceptions.Tests.Component.ExceptionFactoryTests.Create_ShouldReturn_Exception_WhenCodeExists() (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1707)
{
var factory = GetDefaultFactory(ExistentCode);
var exception = factory.Create(ExistentCode);
Expand Down
5 changes: 2 additions & 3 deletions CR.Exceptions.Tests/Component/ExceptionTranslatorTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using CR.Exceptions.Mapping;
using CR.Exceptions.Tests.Shared;
using CR.Exceptions.Tests.Shared;

namespace CR.Exceptions.Tests.Component;

Expand All @@ -9,7 +8,7 @@
private static readonly TestUnknownException NonExistentException = new();

[Fact]
public void TryTranslate_ShouldReturn_TrueAndException_WhenExceptionExists()

Check warning on line 11 in CR.Exceptions.Tests/Component/ExceptionTranslatorTests.cs

View workflow job for this annotation

GitHub Actions / test

Remove the underscores from member name CR.Exceptions.Tests.Component.ExceptionTranslatorTests.TryTranslate_ShouldReturn_TrueAndException_WhenExceptionExists() (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1707)
{
var translator = GetDefaultTranslator();
var result = translator.TryTranslate(ExistentException, out var exception);
Expand All @@ -20,7 +19,7 @@
}

[Fact]
public void TryTranslate_ShouldReturn_FalseAndNull_WhenExceptionDoesNotExist()

Check warning on line 22 in CR.Exceptions.Tests/Component/ExceptionTranslatorTests.cs

View workflow job for this annotation

GitHub Actions / test

Remove the underscores from member name CR.Exceptions.Tests.Component.ExceptionTranslatorTests.TryTranslate_ShouldReturn_FalseAndNull_WhenExceptionDoesNotExist() (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1707)
{
var translator = GetDefaultTranslator();
var result = translator.TryTranslate(NonExistentException, out var exception);
Expand All @@ -30,7 +29,7 @@
}

[Fact]
public void Translate_ShouldReturn_Exception_WhenExceptionExists()

Check warning on line 32 in CR.Exceptions.Tests/Component/ExceptionTranslatorTests.cs

View workflow job for this annotation

GitHub Actions / test

Remove the underscores from member name CR.Exceptions.Tests.Component.ExceptionTranslatorTests.Translate_ShouldReturn_Exception_WhenExceptionExists() (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1707)
{
var translator = GetDefaultTranslator();
var exception = translator.Translate(ExistentException);
Expand All @@ -40,7 +39,7 @@
}

[Fact]
public void Translate_ShouldThrow_WhenExceptionDoesNotExist()

Check warning on line 42 in CR.Exceptions.Tests/Component/ExceptionTranslatorTests.cs

View workflow job for this annotation

GitHub Actions / test

Remove the underscores from member name CR.Exceptions.Tests.Component.ExceptionTranslatorTests.Translate_ShouldThrow_WhenExceptionDoesNotExist() (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1707)
{
var translator = GetDefaultTranslator();

Expand All @@ -50,7 +49,7 @@
private static ExceptionTranslator GetDefaultTranslator()
{
return new ExceptionTranslatorBuilder()
.Map<TestInternalException>(() => new TestUnknownException())
.Map<TestInternalException>(ex => new TestUnknownException(ex))
.Build();
}
}
3 changes: 1 addition & 2 deletions CR.Exceptions.Tests/Unit/ExceptionFactoryBuilderTests.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
using CR.Exceptions.Mapping;
using CR.Exceptions.Tests.Shared;
using CR.Exceptions.Tests.Shared;

namespace CR.Exceptions.Tests.Unit;

public sealed class ExceptionFactoryBuilderTests
{
[Fact]
public void Map_ShouldThrow_WhenDuplicateRegistered()

Check warning on line 8 in CR.Exceptions.Tests/Unit/ExceptionFactoryBuilderTests.cs

View workflow job for this annotation

GitHub Actions / test

Remove the underscores from member name CR.Exceptions.Tests.Unit.ExceptionFactoryBuilderTests.Map_ShouldThrow_WhenDuplicateRegistered() (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1707)
{
const string code = "duplicate";

Expand Down
7 changes: 3 additions & 4 deletions CR.Exceptions.Tests/Unit/ExceptionTranslatorBuilderTests.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
using CR.Exceptions.Mapping;
using CR.Exceptions.Tests.Shared;
using CR.Exceptions.Tests.Shared;

namespace CR.Exceptions.Tests.Unit;

public sealed class ExceptionTranslatorBuilderTests
{
[Fact]
public void Map_ShouldThrow_WhenDuplicateRegistered()

Check warning on line 8 in CR.Exceptions.Tests/Unit/ExceptionTranslatorBuilderTests.cs

View workflow job for this annotation

GitHub Actions / test

Remove the underscores from member name CR.Exceptions.Tests.Unit.ExceptionTranslatorBuilderTests.Map_ShouldThrow_WhenDuplicateRegistered() (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1707)
{
var builder = new ExceptionTranslatorBuilder()
.Map<TestInternalException>(() => new TestUnknownException());
.Map<TestInternalException>(ex => new TestUnknownException(ex));

Assert.ThrowsAny<ArgumentException>(() => builder.Map<TestInternalException>(() => new TestUnknownException()));
Assert.ThrowsAny<ArgumentException>(() => builder.Map<TestInternalException>(ex => new TestUnknownException(ex)));
}
}
3 changes: 1 addition & 2 deletions CR.Exceptions/CrException.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using CR.Exceptions.Extensions;
using System.Collections.Immutable;
using System.Collections.Immutable;

namespace CR.Exceptions;

Expand Down
15 changes: 0 additions & 15 deletions CR.Exceptions/Extensions/FuncExtensions.cs

This file was deleted.

2 changes: 1 addition & 1 deletion CR.Exceptions/Extensions/ImmutableArrayExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System.Collections.Immutable;
using System.Runtime.CompilerServices;

namespace CR.Exceptions.Extensions;
namespace CR.Exceptions;

internal static class ImmutableArrayExtensions
{
Expand Down
14 changes: 9 additions & 5 deletions CR.Exceptions/Mapping/ExceptionFactory.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
using CR.Exceptions.Extensions;
using System.Collections.Frozen;
using System.Collections.Frozen;
using System.Diagnostics.CodeAnalysis;

namespace CR.Exceptions.Mapping;
namespace CR.Exceptions;

public class ExceptionFactory : Map<string, Func<CrException>>
{
internal ExceptionFactory(FrozenDictionary<string, Func<CrException>> dictionary) : base(dictionary) { }

public CrException Create(string code)
=> GetValue(code).ToResult();
=> ExecuteFactory(GetValue(code));

public bool TryCreate(string code, [MaybeNullWhen(false)] out CrException exception)
=> (exception = TryGetValue(code, out var factory) ? factory.ToResult() : null) != null;
=> (exception = TryGetValue(code, out var factory) ? ExecuteFactory(factory) : null) != null;

private static CrException ExecuteFactory(Func<CrException> factory)
{
return factory() ?? throw new InvalidOperationException($"{nameof(factory)} '{factory.Method.Name}' returned null.");
}
}
2 changes: 1 addition & 1 deletion CR.Exceptions/Mapping/ExceptionFactoryBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace CR.Exceptions.Mapping;
namespace CR.Exceptions;

public class ExceptionFactoryBuilder : MapBuilder<string, Func<CrException>>
{
Expand Down
24 changes: 15 additions & 9 deletions CR.Exceptions/Mapping/ExceptionTranslator.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
using CR.Exceptions.Extensions;
using System.Collections.Frozen;
using System.Collections.Frozen;
using System.Diagnostics.CodeAnalysis;

namespace CR.Exceptions.Mapping;
namespace CR.Exceptions;

public class ExceptionTranslator : TypeMap<Func<CrException>>
public class ExceptionTranslator : TypeMap<Func<Exception, CrException>>
{
internal ExceptionTranslator(FrozenDictionary<Type, Func<CrException>> dictionary) : base(dictionary) { }
internal ExceptionTranslator(FrozenDictionary<Type, Func<Exception, CrException>> dictionary) : base(dictionary) { }

public CrException Translate(CrException exception)
=> GetByHierarchy(exception.GetType()).ToResult();
public CrException Translate(Exception exception)
=> ExecuteTranslator(exception, GetByHierarchy(exception.GetType()));

public bool TryTranslate(CrException exception, [MaybeNullWhen(false)] out CrException translated)
=> (translated = TryGetByHierarchy(exception.GetType(), out var translator) ? translator.ToResult() : null) != null;
public bool TryTranslate(Exception exception, [MaybeNullWhen(false)] out CrException translated)
=> (translated = TryGetByHierarchy(exception.GetType(), out var translator) ? ExecuteTranslator(exception, translator) : null) != null;

private static CrException ExecuteTranslator(Exception innerException, Func<Exception, CrException> translator)
{
return
translator(innerException) ??
throw new InvalidOperationException($"{nameof(translator)} '{translator.Method.Name}' returned null.");
}
}
6 changes: 3 additions & 3 deletions CR.Exceptions/Mapping/ExceptionTranslatorBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
namespace CR.Exceptions.Mapping;
namespace CR.Exceptions;

public class ExceptionTranslatorBuilder : MapBuilder<Type, Func<CrException>>
public class ExceptionTranslatorBuilder : MapBuilder<Type, Func<Exception, CrException>>
{
public ExceptionTranslatorBuilder Map<TException>(Func<CrException> translator) where TException : CrException
public ExceptionTranslatorBuilder Map<TException>(Func<Exception, CrException> translator) where TException : Exception
{
AddPair(typeof(TException), translator);
return this;
Expand Down
4 changes: 2 additions & 2 deletions CR.Exceptions/Mapping/Map.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System.Collections.Frozen;
using System.Diagnostics.CodeAnalysis;

namespace CR.Exceptions.Mapping;
namespace CR.Exceptions;

public abstract class Map<TKey, TValue> where TKey : notnull
{
Expand All @@ -20,6 +20,6 @@ protected TValue GetValue(TKey key)
protected bool TryGetValue(TKey key, [MaybeNullWhen(false)] out TValue value)
=> _dictionary.TryGetValue(key, out value);

protected KeyNotFoundException CreateKeyNotFoundException(TKey key)
protected KeyNotFoundException CreateKeyNotFoundException(TKey? key)
=> new($"Key '{key?.ToString() ?? "null"}' in map is not found.");
}
2 changes: 1 addition & 1 deletion CR.Exceptions/Mapping/MapBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Collections.Frozen;

namespace CR.Exceptions.Mapping;
namespace CR.Exceptions;

public abstract class MapBuilder<TKey, TValue> where TKey : notnull
{
Expand Down
2 changes: 1 addition & 1 deletion CR.Exceptions/Mapping/TypeMap.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System.Collections.Frozen;
using System.Diagnostics.CodeAnalysis;

namespace CR.Exceptions.Mapping;
namespace CR.Exceptions;

public abstract class TypeMap<TValue> : Map<Type, TValue>
{
Expand Down
13 changes: 4 additions & 9 deletions CR.Exceptions/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,8 @@ Example:
```csharp
public sealed class UserNotFoundException : NotFoundException
{
public UserNotFoundException()
: base(
[
new CrError(
"IdentityUserNotFound",
"User was not found.")
])
public UserNotFoundException()
: base([new CrError("IdentityUserNotFound","User was not found.")])
{
}
}
Expand Down Expand Up @@ -124,14 +119,14 @@ Registration:
```csharp
ExceptionTranslator translator = new ExceptionTranslatorBuilder()
.Map<KeycloakUserNotFoundException>(
static () => new UserNotFoundException())
static innerException => new UserNotFoundException(innerException))
.Build();
```

Usage:

```csharp
catch (KeycloakUserNotFoundException ex)
catch (Exception ex)
{
throw translator.Translate(ex);
}
Expand Down
8 changes: 8 additions & 0 deletions Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<Project>

<PropertyGroup>
<Company>apptade</Company>
<Authors>apptade</Authors>
Expand All @@ -12,4 +13,11 @@
<PackageLicenseExpression>Apache-2.0</PackageLicenseExpression>
<NuGetPackagePrefix>CrCore</NuGetPackagePrefix>
</PropertyGroup>

<PropertyGroup>
<AnalysisLevel>latest</AnalysisLevel>
<AnalysisMode>Recommended</AnalysisMode>
<EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild>
</PropertyGroup>

</Project>
Loading