Skip to content

Commit

Permalink
refactor: remove exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
nadirbad committed Dec 27, 2024
1 parent 5a2a2c2 commit 4aad452
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 42 deletions.
20 changes: 0 additions & 20 deletions src/Application/Common/Exceptions/UnsupportedColourException.cs

This file was deleted.

16 changes: 7 additions & 9 deletions src/Application/Domain/ValueObjects/Colour.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
using VerticalSliceArchitecture.Application.Common;
using VerticalSliceArchitecture.Application.Common.Exceptions;
using System.Reflection.Metadata.Ecma335;

using ErrorOr;

using VerticalSliceArchitecture.Application.Common;

namespace VerticalSliceArchitecture.Application.Domain.ValueObjects;

Expand All @@ -18,11 +21,11 @@ private Colour(string code)
Code = code;
}

public static Colour From(string code)
public static ErrorOr<Colour> From(string code)
{
var colour = new Colour { Code = code };

return !SupportedColours.Contains(colour) ? throw new UnsupportedColourException(code) : colour;
return !SupportedColours.Contains(colour) ? ColourErrors.UnsupportedColour(code) : colour;
}

public static Colour White => new("#FFFFFF");
Expand All @@ -48,11 +51,6 @@ public static implicit operator string(Colour colour)
return colour.ToString();
}

public static explicit operator Colour(string code)
{
return From(code);
}

public override string ToString()
{
return Code;
Expand Down
11 changes: 11 additions & 0 deletions src/Application/Domain/ValueObjects/ColourErrors.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using ErrorOr;

namespace VerticalSliceArchitecture.Application.Domain.ValueObjects;

public static class ColourErrors
{
public static Error UnsupportedColour(string code) =>
Error.Validation(
code: "ColourErrors.UnsupportedColour",
description: $"The colour code '{code}' is not supported.");
}
22 changes: 9 additions & 13 deletions tests/Application.UnitTests/ValueObjects/ColourTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using VerticalSliceArchitecture.Application.Common.Exceptions;
using VerticalSliceArchitecture.Application.Domain.ValueObjects;
using VerticalSliceArchitecture.Application.Domain.ValueObjects;

namespace VerticalSliceArchitecture.Application.UnitTests.ValueObjects;

Expand All @@ -12,7 +11,8 @@ public void ShouldReturnCorrectColourCode()

var colour = Colour.From(code);

colour.Code.Should().Be(code);
colour.IsError.Should().BeFalse();
colour.Value.Code.Should().Be(code);
}

[Fact]
Expand All @@ -32,17 +32,13 @@ public void ShouldPerformImplicitConversionToColourCodeString()
}

[Fact]
public void ShouldPerformExplicitConversionGivenSupportedColourCode()
public void ShouldReturnErrorGivenNotSupportedColourCode()
{
var colour = (Colour)"#FFFFFF";
// Arrange/Act
var colour = Colour.From("##FF33CC");

colour.Should().Be(Colour.White);
}

[Fact]
public void ShouldThrowUnsupportedColourExceptionGivenNotSupportedColourCode()
{
FluentActions.Invoking(() => Colour.From("##FF33CC"))
.Should().Throw<UnsupportedColourException>();
// Assert
colour.IsError.Should().BeTrue();
colour.FirstError.Code.Should().Be("ColourErrors.UnsupportedColour");
}
}

0 comments on commit 4aad452

Please sign in to comment.