Search code examples
javascriptaws-lambdaamazon-lex

Uncaught exception in AWS lambda javascript


I am trying to elicit an intent from Amazon LEX through lambda but I was given an error calling uncaught exception. Would appreciate the help, my code is as follows:

'use strict';

exports.handler = (event, context, callback) => {
  const sessionAttributes = event.sessionAttributes;
  const slots = event.currentIntent.slots;
  const intention = slots.Intention

  {
    let response = {
      sessionAttributes: event.sessionAttributes,
      dialogAction: {
        type: "ElicitIntent",
        message: {
          contentType: "PlainText",
          content: `Would you like to do A or B? `
        }

      }
    }
    callback(null, response);
  }
}

Solution

  • The "uncaught exception" error usually means that a response is not being provided for the specific type of input. In your case, the callback is not being reached.

    Simply remove the extra set of curly braces (I've commented them out):

    'use strict';
    
    exports.handler = (event, context, callback) => {
      const sessionAttributes = event.sessionAttributes;
      const slots = event.currentIntent.slots;
      const intention = slots.Intention
    
    //}                                             <--remove this
        let response = {
          sessionAttributes: event.sessionAttributes,
          dialogAction: {
            type: "ElicitIntent",
            message: {
              contentType: "PlainText",
              content: `Would you like to do A or B? `
            }
    
          }
        }
        callback(null, response);
    //}                                              <--and remove this
    }