Skip to content

Commit 01405ce

Browse files
authored
Merge pull request #12 from honamic/NotFoundBusinessException
added NotFoundBusinessException
2 parents 8e35dc8 + c34b586 commit 01405ce

File tree

8 files changed

+46
-14
lines changed

8 files changed

+46
-14
lines changed

.github/workflows/build.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
name: .NET Build
22

3-
on: [push, pull_request]
3+
on:
4+
push:
5+
branches: [main]
6+
pull_request:
7+
branches: [main]
48

59
jobs:
610
build:

src/Core/Applications.Abstractions/Results/ResultMessage.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
public class ResultMessage
44
{
5-
public ResultMessage(ResultMessageType type, string message, string field = null, string code = null)
5+
public ResultMessage(ResultMessageType type, string message, string? field= null, string? code = null)
66
{
77
Type = type;
88
Message = message;
@@ -12,9 +12,9 @@ public ResultMessage(ResultMessageType type, string message, string field = null
1212

1313
public string Message { get; set; }
1414

15-
public string Field { get; set; }
15+
public string? Field { get; set; }
1616

17-
public string Code { get; set; }
17+
public string? Code { get; set; }
1818

1919
public ResultMessageType Type { get; set; }
2020
}

src/Core/Applications.Abstractions/Results/ResultMessagesExtensions.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ public static void AppendInfo(this Result result, string message)
1212
result.Messages.Add(new ResultMessage(ResultMessageType.Info, message));
1313
}
1414

15-
public static void AppendError(this Result result, string message, string field, string code)
15+
public static void AppendError(this Result result, string message, string? field, string? code)
1616
{
17-
result.Messages.Add(new ResultMessage(ResultMessageType.Error, message, field, code));
17+
result.Messages.Add(new ResultMessage(ResultMessageType.Error, message, code:code , field: field));
1818
}
1919

20-
public static void AppendError(this Result result, string message, string field)
20+
public static void AppendError(this Result result, string message, string? field)
2121
{
22-
result.AppendError(message, field, null);
22+
result.AppendError(message, field: field, null);
2323
}
2424

2525
public static void AppendError(this Result result, string message)
@@ -88,4 +88,9 @@ public static void SetStatusAsInvalidDomainState(this Result result, string erro
8888
result.Status = ResultStatus.InvalidDomainState;
8989
result.AppendError(errorMessage);
9090
}
91+
92+
public static void SetStatusAsValidationError(this Result result)
93+
{
94+
result.Status = ResultStatus.ValidationError;
95+
}
9196
}

src/Core/Applications/CommandHandlerDecorators/ExceptionCommandHandlerDecorator.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ private TResponse CreateResultWithError(Type type, Exception ex)
5252
result.SetStatusAsUnauthorized();
5353
result.AppendError(ex.Message);
5454
break;
55+
case NotFoundBusinessException notFoundEx:
56+
result.Status = ResultStatus.NotFound;
57+
result.AppendError(notFoundEx.GetMessage(), null, notFoundEx.GetCode());
58+
break;
5559
case BusinessException businessException:
5660
result.Status = ResultStatus.ValidationError;
5761
var code = businessException.GetCode();

src/Core/Applications/CommandHandlerDecorators/ResultOrientedCommandHandlerDecorator.cs

Whitespace-only changes.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace Honamic.Framework.Domain;
2+
3+
public class NotFoundBusinessException : BusinessException
4+
{
5+
public NotFoundBusinessException(string? message = null, string? code = null)
6+
: base(message ?? "Item not found.", code)
7+
{
8+
}
9+
}

src/Facade/Default/Honamic.Framework.Facade.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
</ItemGroup>
1515

1616
<ItemGroup>
17+
<ProjectReference Include="..\..\Core\Domain.Abstractions\Honamic.Framework.Domain.Abstractions.csproj" />
1718
<ProjectReference Include="..\..\Utilities\Default\Honamic.Framework.Utilities.csproj" />
1819
<ProjectReference Include="..\Abstractions\Honamic.Framework.Facade.Abstractions.csproj" />
1920
</ItemGroup>

src/Facade/Default/Interceptors/ExceptionHandlingInterceptor.cs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using Castle.DynamicProxy;
22
using Honamic.Framework.Applications.Exceptions;
33
using Honamic.Framework.Applications.Results;
4+
using Honamic.Framework.Domain;
45
using Microsoft.Extensions.Logging;
56
using System.Diagnostics;
67
using System.Reflection;
@@ -36,7 +37,7 @@ public void Intercept(IInvocation invocation)
3637
{
3738
if (invocation.Proxy is IBaseFacade baseFacade)
3839
{
39-
baseFacade.Logger.LogError(ex, "Unhandled exception");
40+
baseFacade.Logger.LogError(ex, "Unhandled exception");
4041
}
4142

4243
var result = GetNewResult(invocation, ex);
@@ -111,16 +112,24 @@ public void Intercept(IInvocation invocation)
111112
switch (ex)
112113
{
113114
case UnauthenticatedException:
114-
rawResult?.SetStatusAsUnauthenticated();
115-
rawResult?.AppendError(ex.Message, "Exception");
115+
rawResult?.SetStatusAsUnauthenticated(ex.Message);
116116
break;
117117
case UnauthorizedException:
118-
rawResult?.SetStatusAsUnauthorized();
119-
rawResult?.AppendError(ex.Message, "Exception");
118+
rawResult?.SetStatusAsUnauthorized(ex.Message);
119+
break;
120+
case NotFoundBusinessException notFoundEx:
121+
rawResult?.SetStatusAsNotFound();
122+
rawResult?.AppendError(notFoundEx.GetMessage(), null, notFoundEx.GetCode());
123+
break;
124+
case BusinessException businessException:
125+
rawResult?.SetStatusAsValidationError();
126+
var code = businessException.GetCode();
127+
var message = businessException.GetMessage();
128+
rawResult?.AppendError(message, null, code);
120129
break;
121130
default:
122131
rawResult?.SetStatusAsUnhandledExceptionWithSorryError();
123-
rawResult?.AppendError(ex.ToString(), "Exception");
132+
rawResult?.AppendError(ex.Message, "Exception");
124133
break;
125134
}
126135

0 commit comments

Comments
 (0)