Send exception for UNIQUE constraint to UI in .NET core - sql

When a unique constraint exception occurs, how do I message the UI in .NET Core; I want to return JSON, not MVC/Razor approach.

I think you have been looking at my article Catching Bad Data in Entity Framework. In there I describe a way to catch a SQL error and turn it into a validation error for EF Core.
UPDATE
In answer to your follow on questions I can point you to the code associated with the book I am writing, Entity Framework Core in Action. In this I built a method called SaveChangesSqlCheck, which contains the code to check for sql errors. You would use this method instead of calling SaveChanges, and it will return a ValidationResult.
My book has associated git repository where I have unit tests for just about everything I show in the book. Below are links to a unit test to see how you call SaveChangesSqlCheck, and then the SaveChangesSqlCheck code itself.
Unit test: https://github.com/JonPSmith/EfCoreInAction/blob/Chapter10/Test/UnitTests/DataLayer/Ch10_CatchSqlError.cs#L59-L89
The SaveChangesSqlCheck code: https://github.com/JonPSmith/EfCoreInAction/blob/Chapter10/DataLayer/EfCode/SaveChangesSqlCheck.cs
The method that catches the Unique error: https://github.com/JonPSmith/EfCoreInAction/blob/Chapter10/DataLayer/EfCode/SqlErrorFormatters.cs
Note: you need to format the unique constraint name in a special way - see https://github.com/JonPSmith/EfCoreInAction/blob/Chapter10/Test/Chapter10Listings/EfCode/Configuration/MyUniqueConfig.cs for an example.
If you plan to use the code shown in the article then all you need to do is use JsonConvert.SerializeObject(errors) to turn that into json. I have included some code so you can see what the json output would look like.
var error = new ValidationResult("error message");
var jsonList = JsonConvert.SerializeObject(error,
new JsonSerializerSettings { Formatting = Newtonsoft.Json.Formatting.Indented });
The json output of this would be
{
"MemberNames": [],
"ErrorMessage": "error message"
}
I hope that helps.

Related

How can I use Format Arguments in BusinessException?

I have noticed the abp localization provide a Format Arguments mechanism to help generate realtime local string by this way, and I want to know how can I do the same thing in calling a BusinessException while all its overloads are not suitable for this purpose.
Please see the documentation: https://docs.abp.io/en/abp/latest/Exception-Handling#exception-localization
It is possible to set an exception code and data related to the exception. Then ABP automatically localizes the exception message by also using the data arguments you've provided.
Example exception:
throw new BusinessException("App:010046")
.WithData("UserName", "john");
And the related localization entry in the json file:
"App:010046": "Username should be unique. '{UserName}' is already taken!"
It is not using {0}, {1}... but using parameter names instead.

Entity Framework: .Add & .SaveChanges() is not adding entity to database

When I am adding an entry
var objectEntity = new ObjectStaging();
objectEntity .CreatedBy = ClaimsPrincipal.Current.Identity.Name;
objectEntity .ModifiedBy = ClaimsPrincipal.Current.Identity.Name;
objectEntity .Set(ObjectInfo);
database.ObjectStaging.Add(objectEntity);
database.SaveChanges();
For some reason, my objectEntity is not being added to my database given the following code. When using the debugger, it moves through database.ObjectStaging.Add(objectEntity); but does not continue after calling SaveChanges(). Is there a way to know why it is failing to add the entry to the database?
Exception Thrown:
Exception thrown:
'System.Data.Entity.Validation.DbEntityValidationException' in
VCC.BrokerPortal.DAO.dll Validation failed for one or more entities.
See 'EntityValidationErrors' property for more details.
Is there a way to know what specifically did not validate?
Also, I tested the state of the entity using the following lines of code,
database.ObjectStaging.Add(objectEntity);
var state= brokerPortalDB.Entry(objectEntity).State;
state currently shows the value of 'added'.
Thank you!
I ended up looking into the exception thrown and it appears it was related to one of the properties not having proper validation. In this instance I had a [MaxLength] attribute assigned to a field and I was trying to add a value longer than that length allowed.

In Identity for Entity Framework Core 3.x, how do I find users by Email (minding normalization) when Email uniqueness is not enforced?

userManager.FindByEmailAsync(myEmail) throws an exception if there are multiple users with the same email.
I could use:
await context.ApplicationUsers
.FirstOrDefaultAsync(x => x.NormalizedEmail == myEmail.ToUpperInvariant());
That seems to work okay. But I'm not sure if ToUpperInvariant is the right way to check, because System.Text also has Normalize(). It won't matter right now since we are using SQL Server with a case-insensitive configuration, but I don't want things to break if we ever change that.
Am I normalizing in a way that is consistent with how Entity Framework does it? I tried to find the source code, but what I found doesn't use the NormalizedEmail field, so it's likely old.
The normalization is not done by the EF Core, but the UserManager class (using ILookupNormalizer service injected via constructor or set via KeyNormalizer property).
UserManager.FindByEmailAsync method does the normalization for you before calling the store method. The problem is that EF Core store method implementation uses SingleOrDefaultAsync which throws if there are duplicate normalized emails in the database.
To fix that, you could use UserManager.NormalizeEmail method to do the normalization, and then use FirstOrDefaultAsync query as in your sample:
var normalizedEmail = userManager.NormalizeEmail(myEmail);
var firstDuplicate = await userManager.Users
.FirstOrDefaultAsync(x => x.NormalizedEmail == normalizedEmail);

Spock & Spock Reports: how "catch" and customize the error message for AssertionError?

I am working with:
Spock Core
Spock Reports
Spock Spring
Spring MVC Testing
and I have the following code:
#FailsWith(java.lang.AssertionError.class)
def "findAll() Not Expected"(){
given:
url = PersonaUrlHelper.FINDALL;
when:
resultActions = mockMvc.perform(get(url)).andDo(print())
then:
resultActions.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_XML))
}
Here the code fails (how is expected) because the method being tested really returns (MediaType.APPLICATION_JSON) instead of (MediaType.APPLICATION_XML).
So that the reason of #FailsWith(java.lang.AssertionError.class).
Even if I use #FailsWith(value=java.lang.AssertionError.class, reason="JSON returned ...") I am not able to see the reason through Spock Reports
Question One: how I can see the reason on Spock Reports?.
I know Spock offers the thrown() method, therefore I am able to do:
then:
def e = thrown(IllegalArgumentException)
e.message == "Some expected error message"
println e.message
Sadly thrown does not work for AssertionError.
If I use thrown(AssertionError) the test method does not pass, unique way is through #FailsWith but I am not able to get the error message from AssertionError
Question Two how is possible get the Error Message from AssertionError?
I know I am able to do something like
then: "Something to show on Spock Reports"
But just curious if the question two can be resolved..
regarding Question one:
if you look at FailsWithExtension#visitFeatureAnnotation you can see that only value from the #FailsWith is evaluated, reason is not touched at all. What you could do is introduce you own type of annotation (custom one, e.g. same as #FailsWith) and override AbstractAnnotationDrivenExtension#visitFeatureAnnotation. There you have access to reason parameter.
regarding Question two:
please look at this link: http://spock-framework.3207229.n2.nabble.com/Validate-exception-message-with-FailsWith-td7573288.html
additionally maybe you could override AbstractAnnotationDrivenExtension#visitSpec and add custom listener (overriding AbstractRunListener). Then you have access to AbstractRunListener#error method whose documentation says:
Called for every error that occurs during a spec run. May be called multiple times for the same method, for example if both
* the expect-block and the cleanup-block of a feature method fail.
Didn't test for Question two, but it may work. I've used sth similar.
Enjoy,
Tommy

IQueryable serialization (web api)

Why isn't this working?:
var surveys = db.Surveys.Where(s => s.Author.UserId == user.UserId);
return from survey in surveys
select new
{
surveyId = survey.SurveyId,
title = survey.Title
};
And this, with a minor change, is?:
var surveys = db.Surveys.Where(s => s.Author == user);
return from survey in surveys
select new
{
surveyId = survey.SurveyId,
title = survey.Title
};
It throws a serialization error
The 'ObjectContent`1' type failed to serialize the response body for content type
'application/xml; charset=utf-8'. (...)
I'm fine with solving it that way, but I have the same error here (below), and can't solve it the same way:
var surveys = db.Surveys.Where(s => s.AnswerableBy(user));
I ran into this issue recently, in my case the problem was that I had circular references created by the Entity Framework - Model First (Company -> Employee -> Company).
I resolved it by simply creating a view model object that had only the properties I needed.
As per the section 'Handling Circular Object References' here on the Microsoft ASP.NET Web API website, add the following lines of code to the Register method of your WebAPIConfig.cs file (should be in the App_Start folder of your project).
var json = config.Formatters.JsonFormatter;
json.SerializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.Objects;
config.Formatters.Remove(config.Formatters.XmlFormatter);
There are separate instructions on that page for dealing with XML parsing.
The exception you are seeing is a general exception, which can be caused by any number of factors. Check the InnerException property of the serialization exception to find out what exactly caused the serialization to fail.
You are using an anonymous object. Make it strongly typed and the serialization will work.
At the end this has to do with Linq to Entities vs Linq to Objects.
Some LINQ queries can not be translated into SQL (like using a method: someCollection.Where(p => p.SomeMethod() == ...)). So they must be translated to memory first (someCollection.ToList().Where...)
I was having the same problem. In my case the error was caused by the relation between tables created in my Dbml file. Once I removed the relation from the DBML file, it worked. I think Database relations can't be serialized.