Error Handling

If the bot API returns an error, the SDK wraps the error into a ChatbotResponseException object. This example shows how to retrieve the error details using try and catch.

// Build the request to end the session.
BotEndSessionRequest botEndSessionRequest = BotRequest
    .withEndSession(EndSessionReason.USERREQUEST).build();

// Create an invalid session ID to return an error.
RuntimeSessionId invalidSessionId = new RuntimeSessionId("invalid session id");

// Send the request to end the session with error handling.
try{
    BotResponse endSessionResponse = client
        .endChatSession(config, invalidSessionId , botEndSessionRequest);
}catch (Exception e){

    // The error is wrapped in RuntimeException and ExecutionException.
    // Therefore, call getCause() twice to find the root cause.
    ChatbotResponseException chatbotResponseException =
        ((ChatbotResponseException)e.getCause().getCause());

    int httpStatusCode = chatbotResponseException.getStatus();

    System.out.println("Error Http Status Code : " + httpStatusCode);

    Error errorResponse = chatbotResponseException.getErrorResponse();
    System.out.println("Error Payload : " + errorResponse);
}