I am using RavenDB UniqueConstraints bundle, when a unique constraint violates i am getting exception client side , But its InvalidOperationException , exception message explains the reason
Raven.Database.Exceptions.OperationVetoedException: PUT vetoed by Raven.Bundles.UniqueConstraints.UniqueConstraintsPutTrigger because: Ensure unique constraint
Is there anyway to catch OperationVetoedException in a try catch block
Related
I'm wonder what is the purpose of exception parameter that we can pass to
HealthCheckResult.Unhealthy("Message", exception);
When this result is used in health-check endpoint only message and status are used and no info from exception?
An Exception representing the exception that was thrown when checking for status. Optional.
https://learn.microsoft.com/en-us/dotnet/api/microsoft.extensions.diagnostics.healthchecks.healthcheckresult.unhealthy?view=dotnet-plat-ext-3.0
Here's a good explanation on how to use it: https://blog.elmah.io/asp-net-core-2-2-health-checks-explained/
I try to catch/get the Type of the OleDbException to generate the correct error message.
At this moment I'm sure I catch whatever OleDbException, but I want to catch only duplicate value violations to generate the error message.
This is what I do for the moment.
ex As OleDb.OleDbException
MessageBox.Show(ex.Errors.Item(1).ToString(), "Error Message", MessageBoxButtons.OK)
The output is like:
"Violation of UNIQUE KEY constraint 'UC_MyTable'. Cannot insert
duplicate key in object 'dbo.MyTable'. The duplicate key value is
(PJO, Peter Johanson)."
Take a look at the .InnerException property of the exception.
See the MSDN Documentation about it.
You can also use the .ErrorCode property or just use the hash code generated by the message, which will be different for each error message:
ex.Message.GetHashCode
I am raising some application errors from PL/SQL procedures. I am setting some messages for instance,
RAISE_APPLICATION_ERROR(-20001, 'message');
Is there any way, I can display 'message' in exception section in catch block of java program from which I called this procedure?
If you're doing catch (Exception e), message should be available from e.getMessage().
If you're doing catch (SQLException e) (or whatever the exception type is for your data access package) message should still be available from e.getMessage(). Additionally, the -20001 should come through in e.getErrorCode(). Note that it may come through as the absolute value (20001 as opposed to -20001); you'll have to experiment.
I think this post will help you out: Error catching
catch (GenericJdbcException ge) {
IF (se.getErrorCode() == -20001)
If your not using Hibernate you might need to change the error type.
I have a view that has an INSTEAD OF INSERT trigger (in SQL Server 2005). When the user inserts into the view, they are in fact making inserts and updates to a number of tables. The view is sufficiently complex that it cannot have an index, so is unfortunately unconstrained.
The view is being inserted into from C# using code that would be problematic to change. This code catches primary and unique key violations using the following:
try
{
... // Insert into view
}
catch (SqlException ex)
{
if (ex.Number == 2627 || ex.Number == 2601) // Primary key exception, unique constraint violation
{
... // Report the duplicate entry to the user
}
else
{
throw;
}
}
So my question is: can I use RAISERROR within my trigger to create an exception with number 2627 or 2601?
No. You'll have to wait for THROW in the next release (maybe)
You can only throw errors that you have put into sys.messages (50000+), or with text that gives 50000. Or embed it in the text and change your c#. You can't throw errors less than 50000
If the view is so complex that you can't use DRI, then it is too complex. Also, you'll have concurrency issues: overlapping calls will break your "uniqueness" at some point when you roll your own.
I'm not sure if you can actually RAISE a genuine primary key violation. Though you can RAISE your own error with your own message and then catch that. This will also allow you to distinguish between a genuine primary key violation and your own custom violations.
Perhaps the crudest way to accomplish this would be...
SQL Code (in TRIGGER definition maybe)...
RAISERROR('Custom View Violation',16,1);
C#...
try
{
//execute SP / Insert etc...
}
catch (SqlException ex)
{
if (ex.Message.Split('\r')[0] == "Custom View Violation")
{
//deal with your exception
}
}
when deleting an entity in nhibernate i get an exception with this error message:
delete statement conflicted with column reference constraint ..etc
of course the exception is wrapped in long series of exceptions.
the error message is normal, but can i make nhibernate shows more polite error message to the user ??
in another words:
is there any conventions which with, i can customize the exception ??
I'm using Oracle 11g data base.
Yes, you can implement ISQLExceptionConverter to customize the exceptions thrown by NHibernate.
Here's a complete example.