Skip to content

Commit 2a9f6c6

Browse files
committed
general code refactoring (client and server)
1 parent 7725387 commit 2a9f6c6

File tree

7 files changed

+52
-36
lines changed

7 files changed

+52
-36
lines changed

GhostUI/Controllers/AuthController.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public async Task<IActionResult> Login([FromBody]Credentials request)
2626
await _hubContext.Clients.All.SendAsync("UserLogin");
2727

2828
var token = Guid.NewGuid().ToString();
29-
var authUser = new AuthUser("success", token, request?.userName ?? "");
29+
var authUser = new AuthUser("success", token, request?.UserName ?? "");
3030

3131
return Ok(authUser);
3232
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using System.Net;
2+
using GhostUI.Models;
3+
using Microsoft.AspNetCore.Http;
4+
using Microsoft.AspNetCore.Hosting;
5+
using Microsoft.Extensions.Hosting;
6+
using Microsoft.AspNetCore.Builder;
7+
using Microsoft.AspNetCore.Diagnostics;
8+
9+
namespace GhostUI.Extensions
10+
{
11+
public static class ExceptionHandlerExtensions
12+
{
13+
public static IApplicationBuilder UseCustomExceptionHandler(this IApplicationBuilder app)
14+
{
15+
app.UseExceptionHandler(builder =>
16+
{
17+
builder.Run(async context =>
18+
{
19+
var error = context.Features.Get<IExceptionHandlerFeature>();
20+
var exDetails = new ExceptionDetails((int)HttpStatusCode.InternalServerError, error?.Error.Message ?? "");
21+
22+
context.Response.ContentType = "application/json";
23+
context.Response.StatusCode = exDetails.StatusCode;
24+
context.Response.Headers.Add("Access-Control-Allow-Origin", "*");
25+
context.Response.Headers.Add("Application-Error", exDetails.Message);
26+
context.Response.Headers.Add("Access-Control-Expose-Headers", "Application-Error");
27+
28+
await context.Response.WriteAsync(exDetails.ToString());
29+
});
30+
});
31+
32+
return app;
33+
}
34+
}
35+
}

GhostUI/Models/Credentials.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
{
33
public class Credentials : ICredentials
44
{
5-
public string? userName { get; set; }
6-
public string? password { get; set; }
7-
public bool rememberMe { get; set; }
5+
public string? UserName { get; set; }
6+
public string? Password { get; set; }
7+
public bool RememberMe { get; set; }
88
}
99
}

GhostUI/Models/ICredentials.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
{
33
public interface ICredentials
44
{
5-
string? userName { get; set; }
6-
string? password { get; set; }
7-
bool rememberMe { get; set; }
5+
string? UserName { get; set; }
6+
string? Password { get; set; }
7+
bool RememberMe { get; set; }
88
}
99
}

GhostUI/Models/IWeatherForecast.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
{
33
public interface IWeatherForecast
44
{
5-
int Id { get; }
6-
int TemperatureF { get; }
7-
int TemperatureC { get; set; }
8-
string? DateFormatted { get; set; }
9-
string? Summary { get; set; }
5+
int Id { get; }
6+
int TemperatureF { get; }
7+
int TemperatureC { get; set; }
8+
string? DateFormatted { get; set; }
9+
string? Summary { get; set; }
1010
}
1111
}

GhostUI/Models/WeatherForecast.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ namespace GhostUI.Models
44
{
55
public class WeatherForecast : IWeatherForecast
66
{
7-
public int TemperatureC { get; set; }
8-
public string? DateFormatted { get; set; }
9-
public string? Summary { get; set; }
7+
public int TemperatureC { get; set; }
8+
public string? DateFormatted { get; set; }
9+
public string? Summary { get; set; }
1010

1111
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
1212

GhostUI/Program.cs

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
1-
using System.Net;
21
using GhostUI.Hubs;
3-
using GhostUI.Models;
42
using GhostUI.Extensions;
53
using HealthChecks.UI.Client;
6-
using Microsoft.AspNetCore.Http;
74
using Microsoft.AspNetCore.Hosting;
85
using Microsoft.Extensions.Hosting;
96
using Microsoft.AspNetCore.Builder;
10-
using Microsoft.AspNetCore.Diagnostics;
117
using Microsoft.Extensions.DependencyInjection;
128
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
139
using Microsoft.AspNetCore.SpaServices.ReactDevelopmentServer;
@@ -65,23 +61,8 @@
6561
app.UseHsts();
6662
}
6763

68-
// Global exception handling
69-
app.UseExceptionHandler(builder =>
70-
{
71-
builder.Run(async context =>
72-
{
73-
var error = context.Features.Get<IExceptionHandlerFeature>();
74-
var exDetails = new ExceptionDetails((int)HttpStatusCode.InternalServerError, error?.Error.Message ?? "");
75-
76-
context.Response.ContentType = "application/json";
77-
context.Response.StatusCode = exDetails.StatusCode;
78-
context.Response.Headers.Add("Access-Control-Allow-Origin", "*");
79-
context.Response.Headers.Add("Application-Error", exDetails.Message);
80-
context.Response.Headers.Add("Access-Control-Expose-Headers", "Application-Error");
81-
82-
await context.Response.WriteAsync(exDetails.ToString());
83-
});
84-
});
64+
// Custom global exception handler
65+
app.UseCustomExceptionHandler();
8566

8667
app.UseCors(corsPolicyName);
8768

0 commit comments

Comments
 (0)