Example of using RepositoryValidationException

Topics: General Discussion Forum
Jun 6, 2007 at 2:40 PM
Where to find an example of using RepositoryValidationException?
Developer
Jun 6, 2007 at 3:12 PM
Any SqlException that have a State value of 1 will map to this custom exception.
Jun 6, 2007 at 5:39 PM

charlyfriend wrote:
Any SqlException that have a State value of 1 will map to this custom exception.


So it's kind of custom validation logic on the sql server side?
What about sql message? Should the SQL Error message provide information which field failed validation in any particular format?
Developer
Jun 6, 2007 at 5:56 PM
On the sql server side you basically throw the error (handled or unhandled) and on the BE side you handle it (inside HandleSqlException) and map it to a "RepositoryValidationException" or a "ConcurrencyViolationException".
Now you may provide any useful information you want in the SQL Error mesage from a handled exception inside sql server or add it from "HandleSqlException" function at the time fo the custom NET exception.
Regarding any internal detailed information, always take care about som kind of exception shilelding so you don't end up sending this data to the end user so to avoid disclosing sensitive information. Just logging/auditing this data should be fine, along with some custom general purpose error message sent to the caller.
Jun 6, 2007 at 6:32 PM
Edited Jun 6, 2007 at 6:44 PM
Generally speaking, it is fine. But Data Accessguidance package provide the praticular solution for that.
There is an exception handler in any repository class:

private void HandleSqlException(SqlException ex, IDbToBusinessEntityNameMapper mapper)
{
if (ex.Number == ErrorCodes.SqlUserRaisedError)
{
switch (ex.State)
{
case ErrorCodes.ValidationError:
string[] messageParts = ex.Errors0.Message.Split(':');
throw new RepositoryValidationException(
mapper.MapDbParameterToBusinessEntityProperty(messageParts0),
messageParts1, ex);

case ErrorCodes.ConcurrencyViolationError:
throw new ConcurrencyViolationException(ex.Message, ex);

}
}

throw new RepositoryFailureException(ex);
}

It's fine with ConcurrencyViolationException because there is no additional processing for it (besides, there is an example how raise this exception in any Update stored procedure).
But for ValidationError the handler expects an error message with specific formatting. This scenario requires at least an example in the autogenerated stored procedures (or in the hands-on lab) or ideally to be documented. And I'm not sure if it is handled properly when the error message does not contain ":".
Developer
Jun 6, 2007 at 10:57 PM
The "RethrowError" sp will build the SqlError thrown to to caller. SO the @ErrorMessage variable will be populated with the format provided by the specified "SELECT" statement. From there and the SQL documentation about the State values and error messages you should find the appropiate format.
Jun 8, 2007 at 2:28 PM

charlyfriend wrote:
The "RethrowError" sp will build the SqlError thrown to to caller. SO the @ErrorMessage variable will be populated with the format provided by the specified "SELECT" statement. From there and the SQL documentation about the State values and error messages you should find the appropiate format.


That makes sense. "RehtrowError" sp generates the error message
SELECT @ErrorMessage =
N'Error %d, Level %d, State %d, Procedure %s, Line %d, ' +
'Message: '+ ERROR_MESSAGE();

and string[] messageParts = ex.Errors0.Message.Split(':'); will be fine.

However, I doubt this code will work properly
throw new RepositoryValidationException(
mapper.MapDbParameterToBusinessEntityProperty(messageParts[0]),messageParts[1], ex);

because messageParts[0] is ALWAYS "Message".

I suppose that the error message must contain a string <invalid patrameter name>:<error message> instead to let the exception to be processed properly. And my quiestion in this topic is how achive this? As I told there is no example of it neither in documentation nor in the autogenarated code/hand-on lab.
Jul 30, 2007 at 7:04 PM
I guess I'm having similar problems with the error reporting.
From the storedproc RethrowError to HandleSqlException in the repository.

If there is any information on how the filters in HandleSqlException on error number (ex.Number) and error state (ex.State) are determined that would be helpful.
I have situations where errors that probably should not be handled as RepositoryValidationException or ConcurrencyViolationException are caught there nonetheless.
Such as when I violate a unique constraint on a table.
It tries to handle it as a RepositoryValidationException and that handling fails in MapDbParameterToBusinessEntityProperty.

Thanks.