first time include all file
This commit is contained in:
@@ -0,0 +1,73 @@
|
||||
using CarParkValidationAPI.Entities;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Net;
|
||||
using System.Text.Json;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace CarParkValidationAPI.Helpers
|
||||
{
|
||||
public class ErrorHandlerMiddleware
|
||||
{
|
||||
private readonly RequestDelegate _next;
|
||||
private readonly ILogger<ErrorHandlerMiddleware> _logger;
|
||||
private readonly IHostEnvironment _env;
|
||||
|
||||
public ErrorHandlerMiddleware(RequestDelegate next,
|
||||
ILogger<ErrorHandlerMiddleware> logger, IHostEnvironment env)
|
||||
{
|
||||
_next = next;
|
||||
_logger = logger;
|
||||
_env = env;
|
||||
}
|
||||
public async Task Invoke(HttpContext context)
|
||||
{
|
||||
try
|
||||
{
|
||||
await _next(context);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
var response = context.Response;
|
||||
response.ContentType = "application/json";
|
||||
_logger.LogError(ex, ex.Message);
|
||||
/*
|
||||
switch (ex)
|
||||
{
|
||||
// case Exception e:
|
||||
// // custom application error
|
||||
// response.StatusCode = (int)HttpStatusCode.BadRequest;
|
||||
// break;
|
||||
case KeyNotFoundException:
|
||||
// not found error
|
||||
response.StatusCode = (int)HttpStatusCode.NotFound;
|
||||
break;
|
||||
default:
|
||||
// unhandled error
|
||||
response.StatusCode = (int)HttpStatusCode.InternalServerError;
|
||||
break;
|
||||
} */
|
||||
response.StatusCode = ex switch
|
||||
{
|
||||
// case Exception e:
|
||||
// // custom application error
|
||||
// response.StatusCode = (int)HttpStatusCode.BadRequest;
|
||||
// break;
|
||||
KeyNotFoundException => (int)HttpStatusCode.NotFound,// not found error
|
||||
_ => (int)HttpStatusCode.InternalServerError,// unhandled error
|
||||
};
|
||||
//response.StatusCode = (int)HttpStatusCode.InternalServerError;
|
||||
var res = _env.IsDevelopment() ?
|
||||
new ApiException(response.StatusCode, ex.Message, ex.StackTrace?.ToString())
|
||||
: new ApiException(response.StatusCode, "Internal Server Error");
|
||||
|
||||
var options = new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase };
|
||||
var json = JsonSerializer.Serialize(res, options);
|
||||
await response.WriteAsync(json);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
using System;
|
||||
|
||||
namespace CarParkValidationAPI.Helpers;
|
||||
|
||||
public class Helper
|
||||
{
|
||||
public static string DateToStr(DateTime? date)
|
||||
{
|
||||
string result = "";
|
||||
if (date != null)
|
||||
result = date?.ToString("yyyy-MM-dd");
|
||||
return result;
|
||||
}
|
||||
|
||||
public static DateTime StrToDate(string date)
|
||||
{
|
||||
DateTime result = DateTime.Today;
|
||||
if (DateTime.TryParse(date, out var dt))
|
||||
{
|
||||
result = dt;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user