Handling Missing Properties

Submitted by Peter on Wed, 07/19/2017 - 12:59

I have been quite active in the Amazon Alexa Skills Kit forum as of late and it appears that there have been issues with the Alexa service returning all the usual properties in the JSON response. For example 1 of those properties that vanished is deviceId.

Of course in an ideal world all required properties should be included in the response but like anything, things sometimes do not go to plan. This has some angry remarks on the developer forums. This got me thinking. When programming on other platforms it is drummed into us, Never Trust Data From External Sources. Usually, this means user input but it also extends to data from a database, but why shouldn't this also include the properties in an Alexa request.

With that in mind it begs the question why do we completely trust that a property exists. If  your skill makes use of a property it should be common practice to gracefully fail I the property does not exist (or better yet use alternatives, for example instead of deviceId will userId work instead?)

Checking that a property exists is a fairly trivial task, for example, you could try:

 

if(!this.event.context.device.hasOwnProperty('deviceId')){

      //Device ID does not exist end gracefully or try using something else

}

By doing this you will improve the reliability of your skill. Not only will this make it a better experience for your users but this should help stop an influx of negative reviews if and when the skill fails.