404 on missing record.

Hi Guys,

I’ve been playing with the concept of throwing a 404 not found when people are requesting records which could not be found. An example of this might be a handler method like this:

public void function delete(event, rc, prc) {
// We need to collect the object we’re looking to delete, let’s do this using a virtual service layer.
rc.service = ServiceService.findWhere({service_id=rc.service_id});

// Check to ensure that this service exists.
if (isNull(rc.service)) {
// A service was not found matching that ID.
// Route them to the page not found event.
runEvent(‘main.pageNotFound’);
} else {
// We found the service record so we can delete it.
ServiceService.delete(rc.service);

// Put a flash message to note that the service has been deleted.
flash.put(“Message”, “The service has been deleted.”);

// Put the user back to their list of service.
setNextEvent(‘services’);
}
}

We perform a simple check to see if a record was found matching the criteria - then forwarding the user to the missing page event if a record is not found.

This works great, but I find myself writing this same logic in handler after handler, which feels a little inefficient. I wonder if there is a better way of doing it? Perhaps the service layer could throw a specific error when a record isn’t found, that could be caught by an exception handler that routes the user to the 404?

Or perhaps some form of clever magic with an interceptor?

What think yee genii of the coldbox google group?

Robert

Couldn't you move it all into the service layer? So the controller
would be more along the lines of:

public void function delete(event, rc, prc)
{
  if ( ServiceService.deleteService( rc.service_id ) )
  {
    flash.put("Message", "The service has been deleted.");
    setNextEvent('services');
  }
else
  {
    runEvent('main.pageNotFound');
  }
}