ValidationException

ValidationException

Old forum URL: forums.lhotka.net/forums/t/6325.aspx


JoOfMetL posted on Wednesday, February 04, 2009


For centralized management errors. When a Save, but the object is not Valid a ValidationException is thrown.

In 90% of situations, just after, I try to catch this error, but sometimes I forget or I do not need to show the message to the user. My manager errors takes up the task. I log as error: Object is not valid. I would like to have a reference on the subject and the business rules are not respected. I can, of course, override the Save method, catch this exception and restart my own exception containing information I want, however, in order to share the code, do you think it is good for the community at CSLA ?

rsbaker0 replied on Wednesday, February 04, 2009

I'd like to echo the request for more information passed with this exception.

I can see that passing including the reference would by tricky, but the object class, id, and first broken business rule with error severity would be very helpful.

I don't have the exact current text in front of me, but instead of, for example:

"Object is not valid"

maybe the exception text could be something like:

"Object 'Project', id 'xyy' is not valid: Duration must be greater than zero."

JoOfMetL replied on Wednesday, February 04, 2009


Personally I would not change the message of the exception.

I think only ValidationExeption must have a reference to the object. Indeed, thanks to him we have all the information necessary to log the error message that we want. (See BrokenRules).

rsbaker0 replied on Wednesday, February 04, 2009

Well, I would agree as long as getting a reference back wouldn't be a problem, but validation exceptions can be thrown on the server-side of the portal also, making it complicated to get an object reference back to the client. I suppose there is some way to serialize the whole thing along with the exception, but I just don't know what problems that might pose.

JoOfMetL replied on Wednesday, February 04, 2009


I think the ValidationException is thrown on the client side : (Before DataPortal.Update).

public virtual T Save()
    {
      T result;
      if (this.IsChild)
        throw new NotSupportedException(Resources.NoSaveChildException);
      if (EditLevel > 0)
        throw new Validation.ValidationException(Resources.NoSaveEditingException);
      if (!IsValid && !IsDeleted)
        throw new Validation.ValidationException(Resources.NoSaveInvalidException);
      if (IsBusy)
        throw new Validation.ValidationException(Resources.BusyObjectsMayNotBeSaved);
      if (IsDirty)
        result = (T)DataPortal.Update(this);
      else
        result = (T)this;
      OnSaved(result, null, null);
      return result;
    }

The exception may also be thrown on the server-side?

rsbaker0 replied on Wednesday, February 04, 2009

Well, it's perfectly valid (er, at least I think it is and it works in my application) to call Save() on *another* object server-side in your DataPortal_Update() code.  That operation would just go over the local data portal rather than one of the remote portails.

Copyright (c) Marimer LLC