Error handling for views

Error handling for views

When you query a view, Couchbase Server might return errors when it is generating a result set. For instance, the server may only be able to retrieve results from two of three server nodes in response to a view query. Couchbase Server includes any successfully created results as a JSON object; any errors that the server encountered are a part of the JSON object. Couchbase SDKs include helper methods you can use to handle any detected errors. For instance in the Ruby SDK:

view = blog.recent_posts(:include_docs => true)
logger =

view.on_error do |from, reason|
  logger.warn("#{view.inspect} received the error '#{reason}' from #{from}")

posts = view.each do |doc|
  # do something
  # with doc object

We start by querying our view and assigning the result set to the variable view . We then use the on_error method to intercept any error objects that are streaming from Couchbase Server in response to the view query. Within the on_error loop we can do something useful with each error object; in this case we log the content from the error object to standard output.

Note: Error objects in a result set appear at the end of the response object. Therefore, the result set may have several objects that are successfully retrieved results. The error objects occur after the retrieved results.

If you are using the REST API or Couchbase Admin Console to query views, see Error control for more information about the functional equivalent of the on_error method and what conditions cause errors.