Services error handling

Events happening in the community are now at Drupal community events on www.drupal.org.
Steven's picture

There doesn't seem to be an elegant way of dealing with errors. services_method_call() invokes services_error($message), which in turns invokes a server-specific method to output an error in the server-native format. However, this server-native error lives in the same space as legitimate results.

It seems with this system that it is the service server's responsibility to determine whether the data it is being passed is an error message that it generated previously, or not. If this is the way things are supposed to work, this means that there is a chance of confusion between legitimate data and an error structure. Right now, e.g. json_server.module looks in the data for the presence of an '#error' key to determine error status.

Oddly enough, this seems to follow a bad example set by core's xmlrpcs.inc, which similarly checks if the returned value is an object that has its ->is_error key set. In this case, it's still a stupid idea, but at least there you know you're only working with the XMLRPC server and thus which key name to avoid.

In the case of services, there are an infinite number of server types available, each with its own hook_server_error() and thus each with its own method of indicating errors. This means method implementors are working in a data minefield where adding arbitrary keys to your return value can trigger certain server back ends to interpret the result as errors.

(PS: What's with all the hash marks? Every single hook_services property is defined with '#name' rather than 'name'. This convention is used in Form API to allow properties ('#name') to share a namespace with form element names ('name'). In services.module, there is no such namespace sharing, and hash marks are used unnecessarily. The JSON server even includes these hash marks in its output. Icky.)

Comments

I would also like to return errors

windsurf's picture

I would also like to be able to return errors. For example, consider this method callback:

//the condition here isn't the best example, just to show how i'd like to be able to throw errors:

function foo_members_service_get_all($groupType) {
	if ($groupType == 1){
		return array('Burt', 'Ernie');
	}else if ($groupType == 2){
		return array('Homer', 'Bart');
	}else{
		//throw new Exception("Invalid group type");
		services_error("Invalid group type");
	}
}


Throwing an Exception is actually not correct and calling services_error doesn't seem to do anything. I'd like #error to contain "Invalid group type". (I'm not sure if all the servers have the "#error" key but it's one of the root items in the object returned by the JSON server, just to clarify)

Well, return

Hugo Wetterberg's picture

Well,

return services_error("Invalid group type");

Should work fine

works!

windsurf's picture

Awesome that worked perfectly, thanks!

Services

Group organizers

Group categories

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds: