Skip to content
This repository was archived by the owner on Dec 11, 2024. It is now read-only.

Commit 7af4518

Browse files
authored
Merge pull request #53 from marcwittke/develop
Develop
2 parents 76f3084 + 69484ed commit 7af4518

File tree

4 files changed

+43
-24
lines changed

4 files changed

+43
-24
lines changed

src/abstractions/Backend.Fx/Exceptions/ClientException.cs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
using System;
44
using System.Collections.Generic;
55
using System.Linq;
6+
using JetBrains.Annotations;
7+
68

79
public class ClientException : Exception
810
{
@@ -48,21 +50,28 @@ public override string ToString()
4850

4951
public static class ClientExceptionEx
5052
{
51-
public static TEx AddError<TEx>(this TEx clientException, string errorMessage) where TEx : ClientException
53+
public static TEx AddError<TEx>(this TEx clientException, [LocalizationRequired] string errorMessage) where TEx : ClientException
5254
{
5355
clientException.Errors.Add(errorMessage);
5456
return clientException;
5557
}
5658

57-
public static TEx AddError<TEx>(this TEx clientException, string key, string errorMessage) where TEx : ClientException
59+
public static TEx AddError<TEx>(this TEx clientException, string key, [LocalizationRequired] string errorMessage) where TEx : ClientException
5860
{
5961
clientException.Errors.Add(key, errorMessage);
6062
return clientException;
6163
}
6264

63-
public static TEx AddErrors<TEx>(this TEx clientException, string key, IEnumerable<string> errorMessage) where TEx : ClientException
65+
public static TEx AddErrors<TEx>(this TEx clientException, [LocalizationRequired] IEnumerable<string> errorMessages) where TEx : ClientException
6466
{
65-
clientException.Errors.Add(key, errorMessage);
67+
clientException.Errors.Add(errorMessages);
68+
return clientException;
69+
}
70+
71+
72+
public static TEx AddErrors<TEx>(this TEx clientException, string key, [LocalizationRequired] IEnumerable<string> errorMessages) where TEx : ClientException
73+
{
74+
clientException.Errors.Add(key, errorMessages);
6675
return clientException;
6776
}
6877
}

src/abstractions/Backend.Fx/Exceptions/Errors.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@ public Errors Add(string errorMessage)
4242
return this;
4343
}
4444

45+
public Errors Add(IEnumerable<string> errorMessages)
46+
{
47+
Add(GenericErrorKey, errorMessages);
48+
return this;
49+
}
50+
4551
public Errors Add(string key, IEnumerable<string> errorMessages)
4652
{
4753
if (!_dictionaryImplementation.ContainsKey(key))

src/environments/Backend.Fx.AspNetCore/BackendFxProgram.cs

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,18 @@
55
using Backend.Fx.NetCore.Logging;
66
using Microsoft.AspNetCore;
77
using Microsoft.AspNetCore.Hosting;
8-
using Microsoft.Extensions.Configuration;
9-
using Microsoft.Extensions.DependencyInjection;
108
using Microsoft.Extensions.Logging;
119

1210
public class BackendFxProgram<TStartup> where TStartup : class
1311
{
14-
private static Logging.ILogger _logger = new DebugLogger(nameof(BackendFxProgram<TStartup>));
12+
private static Backend.Fx.Logging.ILogger _logger = new DebugLogger(nameof(BackendFxProgram<TStartup>));
1513

1614
public virtual void Main(string[] args)
1715
{
1816
IWebHost webHost;
1917
try
2018
{
21-
webHost = BuildWebHost(args);
19+
webHost = CreateWebHostBuilder(args).Build();
2220
}
2321
catch (Exception ex)
2422
{
@@ -36,13 +34,17 @@ public virtual void Main(string[] args)
3634
catch (Exception ex)
3735
{
3836
_logger.Fatal(ex, "Web host died unexpectedly");
39-
LogManager.Shutdown();
4037
throw;
4138
}
39+
finally
40+
{
41+
LogManager.Shutdown();
42+
}
4243
}
4344

44-
public virtual IWebHost BuildWebHost(string[] args)
45+
public IWebHostBuilder CreateWebHostBuilder(string[] args)
4546
{
47+
4648
try
4749
{
4850
_logger = LogManager.Create<BackendFxProgram<TStartup>>();
@@ -56,7 +58,6 @@ public virtual IWebHost BuildWebHost(string[] args)
5658

5759
try
5860
{
59-
IWebHost host;
6061
using (_logger.InfoDuration("Building web host"))
6162
{
6263
var webHostBuilder = WebHost.CreateDefaultBuilder(args)
@@ -71,21 +72,18 @@ public virtual IWebHost BuildWebHost(string[] args)
7172

7273
ConfigureWebHost(webHostBuilder);
7374

74-
host = webHostBuilder.Build();
75-
host.Services.GetRequiredService<IApplicationLifetime>().ApplicationStopped.Register(LogManager.Shutdown);
75+
return webHostBuilder;
7676
}
77-
78-
return host;
7977
}
8078
catch (Exception ex)
8179
{
82-
_logger.Fatal(ex, "Web host could not be built.");
80+
_logger.Fatal(ex, "Web host builder could not be created.");
8381
LogManager.Shutdown();
8482
throw;
8583
}
8684
}
8785

8886
protected virtual void ConfigureWebHost(IWebHostBuilder builder)
89-
{}
87+
{ }
9088
}
9189
}

src/environments/Backend.Fx.AspNetCore/ErrorHandling/JsonErrorHandlingMiddleware.cs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,12 @@ public class JsonErrorHandlingMiddleware : ErrorHandlingMiddleware
1717
{
1818
private readonly IHostingEnvironment _env;
1919
private static readonly ILogger Logger = LogManager.Create<JsonErrorHandlingMiddleware>();
20-
private readonly JsonSerializerSettings _jsonSerializerSettings = new JsonSerializerSettings
20+
21+
protected JsonSerializerSettings JsonSerializerSettings { get; } = new JsonSerializerSettings
2122
{
2223
ContractResolver = new DefaultContractResolver
2324
{
24-
NamingStrategy = new CamelCaseNamingStrategy { ProcessDictionaryKeys = true }
25+
NamingStrategy = new CamelCaseNamingStrategy {ProcessDictionaryKeys = true}
2526
},
2627
};
2728

@@ -53,10 +54,9 @@ protected override async Task HandleClientError(HttpContext context, int httpSta
5354
: new Errors().Add($"HTTP{httpStatusCode}: {message}");
5455

5556
context.Response.StatusCode = httpStatusCode;
56-
var errorsDictionaryForJson = errors.ToDictionary(kvp => kvp.Key == "" ? "_error" : kvp.Key, kvp => kvp.Value);
57-
string responseContent = JsonConvert.SerializeObject(errorsDictionaryForJson, _jsonSerializerSettings);
57+
string serializedErrors = SerializeErrors(errors);
5858
context.Response.ContentType = "application/json; charset=utf-8";
59-
await context.Response.WriteAsync(responseContent);
59+
await context.Response.WriteAsync(serializedErrors);
6060
}
6161

6262
protected override async Task HandleServerError(HttpContext context, Exception exception)
@@ -68,10 +68,16 @@ protected override async Task HandleServerError(HttpContext context, Exception e
6868
}
6969
context.Response.StatusCode = (int) HttpStatusCode.InternalServerError;
7070
var responseContent = _env.IsDevelopment()
71-
? JsonConvert.SerializeObject(new { message = exception.Message, stackTrace = exception.StackTrace }, _jsonSerializerSettings)
72-
: JsonConvert.SerializeObject(new { message = "An internal error occured" }, _jsonSerializerSettings);
71+
? JsonConvert.SerializeObject(new { message = exception.Message, stackTrace = exception.StackTrace }, JsonSerializerSettings)
72+
: JsonConvert.SerializeObject(new { message = "An internal error occured" }, JsonSerializerSettings);
7373
context.Response.ContentType = "application/json; charset=utf-8";
7474
await context.Response.WriteAsync(responseContent);
7575
}
76+
77+
protected virtual string SerializeErrors(Errors errors)
78+
{
79+
var errorsDictionaryForJson = errors.ToDictionary(kvp => kvp.Key == "" ? "_error" : kvp.Key, kvp => kvp.Value);
80+
return JsonConvert.SerializeObject(errorsDictionaryForJson, JsonSerializerSettings);
81+
}
7682
}
7783
}

0 commit comments

Comments
 (0)