josephwoodward / GlobalExceptionHandlerDotNet

Exception handling as a convention in the ASP.NET Core request pipeline
MIT License
272 stars 32 forks source link

can not handle api constructor exception #44

Open jeff-nasseri opened 3 years ago

jeff-nasseri commented 3 years ago

dotnet version : .net 5 os : windows

when i raise new custome exception in my ctor of webapi in my project ,app.UseGlobalExceptionHandler can not handle exception here is my ctor code : CurrentLoggedUser = _unitOfWork.GetRepository<AuthXUser>().SingleOrDefault(a => a.UUID == _userUUID) ?? throw new CanNotFoundUserWithCurrentUUID(_userUUID);

here is my error handling code :

app.UseGlobalExceptionHandler(x => { x.ContentType = "application/json"; x.ResponseBody(s => JsonConvert.SerializeObject(new { Description = "An error occured whilst processing your request and has not developet in global exception", ErrorMessage = s.Message })); x.Map<CanNotFoundUserWithCurrentUUID>().ToStatusCode(StatusCodes.Status404NotFound) .WithBody((ex, context) => JsonConvert.SerializeObject(new ContainerErrorModel { Message = $"can not found user by uuid {ex.UUID}", ContainerName = "Profile", Code = 0 })); });