dima767/inspektr — Issue #13: AuditActionContext assertNotNulls could yield more helpful IllegalArgumentExceptions
Author: apetro
Created: 2011-11-10 15:01:06+00:00
State: open

Saw this in providing some help with CAS today:

AuditActionContext constructor IllegalArgumentExceptions-out when arguments, e.g. resourceOperatedUpon, are null

assertNotNull(resourceOperatedUpon, "resourceOperatedUpon cannot be null");

The resulting IllegalArgumentException would be more helpful if it included the rest of the arguments to the constructor, to make it easier for the adopter encountering this exception to figure out what context gave rise to it.

Not sure whether and how to accomplish this. Catch the IllegalArgumentException and wrap in an IllegalArgumentException with the fuller context?

public AuditActionContext(final String principal, final String resourceOperatedUpon, final String actionPerformed, final String applicationCode, final Date whenActionWasPerformed, final String clientIpAddress, final String serverIpAddress) {

try {
assertNotNull(principal, "principal cannot be null");
assertNotNull(resourceOperatedUpon, "resourceOperatedUpon cannot be null");
assertNotNull(actionPerformed, "actionPerformed cannot be null.");
assertNotNull(applicationCode, "applicationCode cannot be null.");
assertNotNull(whenActionWasPerformed, "whenActionPerformed cannot be null.");
assertNotNull(clientIpAddress, "clientIpAddress cannot be null.");
assertNotNull(serverIpAddress, "serverIpAddress cannot be null.");
} catch (IllegalArgumentException illegalArgException) {

throw new IllegalArgumentException("One or more arguments to AuditActionContext constructor were invalid: " +
"principal: " + principal +
"resourceOperatedUpon: " + resourceOperatedUpon +
"actionPerformed: " + actionPerformed +
"applicationCode: " + applicationCode +
"whenActionWasPerformed: " + whenActionWasPerformed +
"clientIpAddress: " + clientIpAddress +
"serverIpAddress: " + serverIpAddress,
illegalArgException );

}
...
}

Enhance the message in each assertNotNull ?

Something else?