Skip to content

Commit 7f90734

Browse files
authored
Merge pull request TwilioDevEd#35 from TwilioDevEd/chat-error-handling-improvements
Error handling improvements for Chat
2 parents c092307 + ff0624e commit 7f90734

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

webroot/chat/index.js

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,23 @@ $(function() {
4646
$.getJSON('/token.php', {
4747
device: 'browser'
4848
}, function(data) {
49-
// Alert the user they have been assigned a random username
50-
username = data.identity;
51-
print('You have been assigned a random username of: '
52-
+ '<span class="me">' + username + '</span>', true);
49+
5350

5451
// Initialize the Chat client
5552
Twilio.Chat.Client.create(data.token).then(client => {
53+
console.log('Created chat client');
5654
chatClient = client;
5755
chatClient.getSubscribedChannels().then(createOrJoinGeneralChannel);
56+
57+
// Alert the user they have been assigned a random username
58+
username = data.identity;
59+
print('You have been assigned a random username of: '
60+
+ '<span class="me">' + username + '</span>', true);
61+
62+
}).catch(error => {
63+
console.error(error);
64+
print('There was an error creating the chat client:<br/>' + error, true);
65+
print('Please check your .env file.', false);
5866
});
5967
});
6068

@@ -103,7 +111,12 @@ $(function() {
103111
// Send a new message to the general channel
104112
var $input = $('#chat-input');
105113
$input.on('keydown', function(e) {
114+
106115
if (e.keyCode == 13) {
116+
if (generalChannel === undefined) {
117+
print('The Chat Service is not configured. Please check your .env file.', false);
118+
return;
119+
}
107120
generalChannel.sendMessage($input.val())
108121
$input.val('');
109122
}

0 commit comments

Comments
 (0)