Skip to content

Commit

Permalink
refactor: ♻️ refactor problem details exception handler
Browse files Browse the repository at this point in the history
  • Loading branch information
mehdihadeli committed Sep 10, 2024
1 parent a13b01f commit f3922be
Show file tree
Hide file tree
Showing 22 changed files with 190 additions and 165 deletions.
2 changes: 1 addition & 1 deletion .config/dotnet-tools.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"rollForward": false
},
"dotnet-ef": {
"version": "8.0.7",
"version": "8.0.8",
"commands": [
"dotnet-ef"
],
Expand Down
165 changes: 89 additions & 76 deletions .github/release-drafter.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,46 +15,49 @@ template: |
$CHANGES
**Full Changelog**: https://github.com/$OWNER/$REPOSITORY/compare/$PREVIOUS_TAG...v$RESOLVED_VERSION
categories:
- title: 🚀 Features
labels:
- feature
- title: ♻️ Enhancement
labels:
- enhancement
- refactor
- title: 🐛 Bug Fixes
labels:
- fix
- bug
- title: 👷 CI
labels:
- ci
- title: ⚠️ Breaking Changes
labels:
- breaking-changes
- title: ⛔️ Deprecated
labels:
- deprecated
- title: 🗑 Removed
labels:
- removed
- title: 🔐 Security
labels:
- security
- title: 📄 Documentation
labels:
- docs
- documentation
- title: 🧩 Dependency Updates
labels:
- deps
- dependencies
- title: 🧰 Maintenance
label: 'chore'
- title: 🧺 Miscellaneous #Everything except ABAP
label: misc
- title: 🚩 Other changes
## putting no labels pr to `Other Changes` category with no label - https://github.com/release-drafter/release-drafter/issues/139#issuecomment-480473934
- title: 🚀 Features
labels:
- feature
- title: ♻️ Enhancement
labels:
- enhancement
- refactor
- title: 🐛 Bug Fixes
labels:
- fix
- bug
- title: 👷 CI
labels:
- ci
- title: 🧪 Test
labels:
- test
- title: ⚠️ Breaking Changes
labels:
- breaking-changes
- title: ⛔️ Deprecated
labels:
- deprecated
- title: 🗑 Removed
labels:
- removed
- title: 🔐 Security
labels:
- security
- title: 📄 Documentation
labels:
- docs
- documentation
- title: 🧩 Dependency Updates
labels:
- deps
- dependencies
- title: 🧰 Maintenance
label: 'chore'
- title: 🧺 Miscellaneous #Everything except ABAP
label: misc
- title: 🚩 Other changes
## putting no labels pr to `Other Changes` category with no label - https://github.com/release-drafter/release-drafter/issues/139#issuecomment-480473934


# https://www.trywilco.com/post/wilco-ci-cd-github-heroku
Expand All @@ -64,52 +67,62 @@ categories:
# Using regex for defining rules - https://regexr.com/
# https://stackoverflow.com/questions/58899999/regexp-to-match-conventional-commit-syntax
autolabeler:
- label: 'chore'
branch:
- '(chore)(\([a-z ]+\))?\/.'
title:
- '^(chore)(\([a-z ]+\))?: .'
- label: 'bug'
branch:
- '(fix)(\([a-z ]+\))?\/.'
title:
- '^(fix)(\([a-z ]+\))?: .'
- label: 'feature'
branch:
- '(feat)(\([a-z ]+\))?\/.'
title:
- '^(feat)(\([a-z ]+\))?: .'
- label: 'ci/cd'
branch:
- '(ci)(\([a-z ]+\))?\/.'
title:
- '^(ci)(\([a-z ]+\))?: .'
- label: 'minor'
branch:
- '(feat)(\([a-z ]+\))?\/.'
title:
- '^(feat)(\([a-z ]+\))?: .'
- label: 'patch'
branch:
- '(fix)(\([a-z ]+\))?\/.'
- '(ci)(\([a-z ]+\))?\/.'
title:
- '^(fix)(\([a-z ]+\))?: .'
- '^(ci)(\([a-z ]+\))?: .'
- label: 'chore'
branch:
- '(chore)(\([a-z ]+\))?\/.'
title:
- '^(chore)(\([a-z ]+\))?: .'
- label: 'bug'
branch:
- '(fix)(\([a-z ]+\))?\/.'
title:
- '^(fix)(\([a-z ]+\))?: .'
- label: 'refactor'
branch:
- '(refactor)(\([a-z ]+\))?\/.'
title:
- '^(refactor)(\([a-z ]+\))?: .'
- label: 'test'
branch:
- '(test)(\([a-z ]+\))?\/.'
title:
- '^(test)(\([a-z ]+\))?: .'
- label: 'feature'
branch:
- '(feat)(\([a-z ]+\))?\/.'
title:
- '^(feat)(\([a-z ]+\))?: .'
- label: 'ci/cd'
branch:
- '(ci)(\([a-z ]+\))?\/.'
title:
- '^(ci)(\([a-z ]+\))?: .'
- label: 'minor'
branch:
- '(feat)(\([a-z ]+\))?\/.'
title:
- '^(feat)(\([a-z ]+\))?: .'
- label: 'patch'
branch:
- '(fix)(\([a-z ]+\))?\/.'
- '(ci)(\([a-z ]+\))?\/.'
title:
- '^(fix)(\([a-z ]+\))?: .'
- '^(ci)(\([a-z ]+\))?: .'

change-template: '- $TITLE @$AUTHOR (#$NUMBER)'
change-title-escapes: '\<*_&' # You can add # and @ to disable mentions, and add ` to disable code blocks.
version-resolver:
major:
labels:
- breaking-changes
- breaking-changes
minor:
labels:
- minor
- minor
patch:
labels:
- patch
- patch
default: patch

exclude-labels:
- skip-changelog
- skip-changelog
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"description": "food-delivery-microservices",
"scripts": {
"prepare": "husky && dotnet tool restore",
"install-dev-cert-bash": "curl -sSL https://aka.ms/getvsdbgsh | bash /dev/stdin -v vs2019 -l ~/vsdbg"
"install-dev-cert-bash": "curl -sSL https://aka.ms/getvsdbgsh | bash /dev/stdin -v vs2019 -l ~/vsdbg",
"upgrade-packages": "dotnet outdated food-delivery-microservices.sln"
},
"repository": {
"type": "git",
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ namespace BuildingBlocks.Core.Web.Extensions;

public static class HostEnvironmentExtensions
{
public static bool IsTest(this IHostEnvironment env) => env.IsEnvironment("Test");
public static bool IsTest(this IHostEnvironment env) => env.IsEnvironment(Environments.Test);

public static bool IsDependencyTest(this IHostEnvironment env) => env.IsEnvironment("DependencyTest");
public static bool IsDependencyTest(this IHostEnvironment env) => env.IsEnvironment(Environments.DependencyTest);

public static bool IsDocker(this IHostEnvironment env) => env.IsEnvironment("Docker");
public static bool IsDocker(this IHostEnvironment env) => env.IsEnvironment(Environments.Docker);
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,16 @@
<PackageReference Include="AspNetCore.HealthChecks.Rabbitmq" />
<PackageReference Include="AspNetCore.HealthChecks.NpgSql" />
<PackageReference Include="AspNetCore.HealthChecks.EventStore" />
<PackageReference Include="prometheus-net.AspNetCore" />
<PackageReference Include="prometheus-net.AspNetCore.Grpc" />
<PackageReference Include="prometheus-net.AspNetCore.HealthChecks" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Diagnostics.HealthChecks" />
<PackageReference Include="Microsoft.Extensions.Diagnostics.HealthChecks.EntityFrameworkCore" />
<PackageReference Include="prometheus-net.AspNetCore" />
<PackageReference Include="prometheus-net.AspNetCore.Grpc" />
<PackageReference Include="prometheus-net.AspNetCore.HealthChecks" />
<PackageReference Include="Microsoft.Extensions.Diagnostics.HealthChecks.ResourceUtilization" />

</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ namespace BuildingBlocks.HealthCheck;
// https://github.com/prometheus-net/prometheus-net
public static class DependencyInjectionExtensions
{
private static readonly string[] _defaultTags = ["live", "ready",];

public static WebApplicationBuilder AddCustomHealthCheck(
this WebApplicationBuilder builder,
Action<IHealthChecksBuilder>? healthChecksBuilder = null,
Expand All @@ -38,10 +40,24 @@ public static WebApplicationBuilder AddCustomHealthCheck(

var healCheckBuilder = builder
.Services.AddHealthChecks()
.AddDiskStorageHealthCheck(_ => { }, tags: new[] { "live", "ready" })
.AddPingHealthCheck(_ => { }, tags: new[] { "live", "ready" })
.AddPrivateMemoryHealthCheck(512 * 1024 * 1024, tags: new[] { "live", "ready" })
.AddDnsResolveHealthCheck(_ => { }, tags: new[] { "live", "ready" })
.AddDiskStorageHealthCheck(_ => { }, tags: _defaultTags)
.AddPingHealthCheck(_ => { }, tags: _defaultTags)
.AddPrivateMemoryHealthCheck(512 * 1024 * 1024, tags: _defaultTags)
.AddDnsResolveHealthCheck(_ => { }, tags: _defaultTags)
.AddResourceUtilizationHealthCheck(o =>
{
o.CpuThresholds = new ResourceUsageThresholds
{
DegradedUtilizationPercentage = 80,
UnhealthyUtilizationPercentage = 90,
};
o.MemoryThresholds = new ResourceUsageThresholds
{
DegradedUtilizationPercentage = 80,
UnhealthyUtilizationPercentage = 90,
};
o.SamplingWindow = TimeSpan.FromSeconds(5);
})
.ForwardToPrometheus();

healthChecksBuilder?.Invoke(healCheckBuilder);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using BuildingBlocks.Core.Messaging;
using BuildingBlocks.Core.Reflection;
using BuildingBlocks.Core.Reflection.Extensions;
using BuildingBlocks.Core.Web.Extensions;
using Humanizer;
using MassTransit;
using Microsoft.AspNetCore.Builder;
Expand Down Expand Up @@ -49,7 +50,7 @@ params Assembly[] scanAssemblies
? scanAssemblies
: ReflectionUtilities.GetReferencedAssemblies(Assembly.GetCallingAssembly()).ToArray();

if (!builder.Environment.IsEnvironment("test"))
if (!builder.Environment.IsTest())
{
builder.Services.AddMassTransit(ConfiguratorAction);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using BuildingBlocks.Core.Persistence.EfCore;
using BuildingBlocks.Core.Web;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
Expand All @@ -14,7 +15,7 @@ public TDbContext CreateDbContext(string[] args)
{
Console.WriteLine($"BaseDirectory: {AppContext.BaseDirectory}");

var environmentName = env ?? Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "test";
var environmentName = env ?? Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? Environments.Test;

var builder = new ConfigurationBuilder()
.SetBasePath(AppContext.BaseDirectory ?? "")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@ public static async Task<TRequest> HandleValidationAsync<TRequest>(
{
var validationResult = await validator.ValidateAsync(request, cancellationToken).ConfigureAwait(false);
if (!validationResult.IsValid)
throw new ValidationException(validationResult.ToValidationResultModel().Message);
{
throw new ValidationException(
validationResult.ToValidationResultModel().Errors?.FirstOrDefault()?.Message
?? validationResult.ToValidationResultModel().Message
);
}

return request;
}
Expand All @@ -29,7 +34,12 @@ public static TRequest HandleValidation<TRequest>(this IValidator<TRequest> vali
{
var validationResult = validator.Validate(request);
if (!validationResult.IsValid)
throw new ValidationException(validationResult.ToValidationResultModel().Message);
{
throw new ValidationException(
validationResult.ToValidationResultModel().Errors?.FirstOrDefault()?.Message
?? validationResult.ToValidationResultModel().Message
);
}

return request;
}
Expand Down
Loading

0 comments on commit f3922be

Please sign in to comment.