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.

1// Build the request to end the session.
2BotEndSessionRequest botEndSessionRequest = BotRequest
3    .withEndSession(EndSessionReason.USERREQUEST).build();
4
5// Create an invalid session ID to return an error.
6RuntimeSessionId invalidSessionId = new RuntimeSessionId("invalid session id");
7
8// Send the request to end the session with error handling.
9try{
10    BotResponse endSessionResponse = client
11        .endChatSession(config, invalidSessionId , botEndSessionRequest);
12}catch (Exception e){
13
14    // The error is wrapped in RuntimeException and ExecutionException.
15    // Therefore, call getCause() twice to find the root cause.
16    ChatbotResponseException chatbotResponseException =
17        ((ChatbotResponseException)e.getCause().getCause());
18
19    int httpStatusCode = chatbotResponseException.getStatus();
20
21    System.out.println("Error Http Status Code : " + httpStatusCode);
22
23    Error errorResponse = chatbotResponseException.getErrorResponse();
24    System.out.println("Error Payload : " + errorResponse);
25}