Home > Articles > Home & Office Computing > The Web/Virtual Worlds/Social Networking

  • Print
  • + Share This
This chapter is from the book

Error Handling

JavaScript errors from apps seem to be an all-too-common occurrence, but they don't have to be. Most developers don't expect errors to occur, and so they don't test or prepare for them as a result, but errors do happen. Your best defense against them is to admit that yes, they do occur, and yes, you need to be ready for them.

OpenSocial offers a few functions you can use to deal with errors. In the following example we check for errors before we parse the response for data. If an error is found, the response data isn't parsed.

/**
 * Check if the response had an error; if it did, log it and if
 * it was an INTERNAL_ERROR attempt to retry the request
 * @param {opensocial.DataResponse} data
 */
function requestHadError(data){

   // Return true if data is null or undefined
   if(!data) return true;

   // Check the opensocial.DataResponse for the global error flag
   if(data.hadError()){
      // Find the specific opensocial.ResponseItem that had the error
      var ri = data.get(Tic-Tac-Toe.RequestKeys.VIEWER);
      if(ri && ri.hadError()){

         // Output the error message
         log(ri.getErrorMessage());

         // Check the error code; an INTERNAL_ERROR can simply mean
         // network congestion or MySpace server instability

         if(opensocial.ResponseItem.Error.INTERNAL_ERROR === ri.getErrorCode()){

            // Retry the request a certain number of times; make
            // sure you don't create an infinite loop here!

            if(retries > 0){
               retries--;
               window.setTimeout(getInitialData, 1000);
            }
         }
      }
      return true;
   }
   return false;
}

First, we check if the response is null or undefined, and then check if the global error flag was set in the DataResponse object by calling the object's hadError() function. This flag is set if any of the requests had an error. Each ResponseItem has an error flag as well, so we'll need to check each ResponseItem in order to find out which request had the error, if the global flag was set. We do this by calling the ResponseItem's hadError() function. In this case we had only one request, so there is only one ResponseItem, but in the event of multiple ResponseItem objects we would check each one in turn to determine its error state.

Similar to an opensocial.Enum object that has two types of data, one for display and one for logical comparisons, each opensocial.ResponseItem that encounters an error also has two types of data. The error message, accessed by getErrorMessage(), is an arbitrary string that should describe the error and help you debug what happened. The error code, accessed by getErrorCode(), matches up to one of the codes found in the opensocial.ResponseItem.Error enum. Common causes for each type of error code may be found in Table 2.4.

Table 2.4. Common Error Code Causes

Common Error Codes

Causes

opensocial.ResponseItem.Error.BAD_REQUEST

The most common cause of this error is that some part of your request didn't make sense to the container. For example, you passed in a negative value that should be positive, you didn't supply a required field, or you passed in an invalid ID for a particular request.

opensocial.ResponseItem.Error.FORBIDDEN

This error is returned only when the server responds with a status code of 403 and is not commonly seen.

opensocial.ResponseItem.Error.INTERNAL_ERROR

This is a catchall error that typically occurs when something unknown happens on the MySpace side of the request. As such, it should be intermittent. It can also occur in opensocial.requestPermission (a topic we'll cover in the section on requesting permissions in Chapter 3) if a permission was requested but no new permission was granted, or if the server returned an unknown status code.

opensocial.ResponseItem.Error.NOT_IMPLEMENTED

This error is returned if you've requested some OpenSocial functionality that MySpace doesn't support. If you receive this error, either the entire function you're calling isn't available, or some parameter in a request isn't supported. An example of the latter would be that you requested an opensocial.Person field that isn't supported.

opensocial.ResponseItem.Error.UNAUTHORIZED

This error most commonly happens when you've requested data to which you don't have access. It may be possible to use opensocial.requestPermission to ask the user to grant your app access to the data. Again, see Chapter 3 for details.

You'll notice that we also attempted to retry the request in the event of an INTERNAL_ERROR. This is because an INTERNAL_ERROR can sometimes occur because of network congestion or other temporary problems. Your request might succeed if you wait a second or so and try again.

Handling errors offers two benefits:

  • There are no embarrassing JavaScript errors when you try to retrieve objects that don't exist.
  • There's a possibility that you can recover from your error by retrying the request.
  • + Share This
  • 🔖 Save To Your Account