Skip to content

Commit

Permalink
fix: Improve error logging by adding the error to the log. (#21)
Browse files Browse the repository at this point in the history
Authored-by: Kees C. Bakker <k.bakker@hostedsolutions>
  • Loading branch information
KeesCBakker committed Aug 3, 2023
1 parent b60ba80 commit 2c99805
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 16 deletions.
2 changes: 1 addition & 1 deletion docs/_pages/basic_usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ module.exports = function(robot) {
.then(function(api_response) {
// List is searched for the channel with the right name, and the notification_room is updated
const room = api_response.channels.find(channel => channel.name === default_channel_name);
if (room != null) { return notification_room = room.id; }}).catch(error => robot.logger.error(error.message));
if (room != null) { return notification_room = room.id; }}).catch(error => robot.logger.error(error, error.message));

// Any message that says "send updates here" will change the notification room
robot.hear(/send updates here/i, res => notification_room = res.message.rawMessage.channel.id);
Expand Down
12 changes: 6 additions & 6 deletions src/bot.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class SlackClient {
);
}
}).catch(error => {
return this.robot.logger.error(`Error setting topic in conversation ${conversationId}: ${error.message}`);
return this.robot.logger.error(error, `Error setting topic in conversation ${conversationId}: ${error.message}`);
});
}
send(envelope, message) {
Expand All @@ -84,11 +84,11 @@ class SlackClient {
if (typeof message !== "string") {
return this.web.chat.postMessage({ channel: room, text: message.text }).then(result => {
this.robot.logger.debug(`Successfully sent message to ${room}`)
}).catch(e => this.robot.logger.error(`SlackClient#send(message) error: ${e.message}`))
}).catch(e => this.robot.logger.error(e, `SlackClient#send(message) error: ${e.message}`))
} else {
return this.web.chat.postMessage({ channel: room, text: message }).then(result => {
this.robot.logger.debug(`Successfully sent message (string) to ${room}`)
}).catch(e => this.robot.logger.error(`SlackClient#send(string) error: ${e.message}`))
}).catch(e => this.robot.logger.error(e, `SlackClient#send(string) error: ${e.message}`))
}
}
loadUsers(callback) {
Expand Down Expand Up @@ -165,7 +165,7 @@ class SlackClient {
try {
await this.eventHandler(event);
} catch (error) {
this.robot.logger.error(`bot.js: eventWrapper: An error occurred while processing an event from SlackBot's SlackClient: ${error.message}.`);
this.robot.logger.error(error, `bot.js: eventWrapper: An error occurred while processing an event from SlackBot's SlackClient: ${error.message}.`);
}
}
}
Expand Down Expand Up @@ -365,7 +365,7 @@ class SlackBot extends Adapter {
* @param error - An error emitted
*/
error(error) {
this.robot.logger.error(`SlackBot error: ${JSON.stringify(error)}`);
this.robot.logger.error(error, `SlackBot error`);
// Assume that scripts can handle slowing themselves down, all other errors are bubbled up through Hubot
// NOTE: should rate limit errors also bubble up?
if (error.code !== -1) {
Expand Down Expand Up @@ -494,7 +494,7 @@ class SlackBot extends Adapter {
default:
this.robot.logger.debug(`Received generic message: ${message.event.type}`);
SlackTextMessage.makeSlackTextMessage(from, null, message?.body?.event.text, message?.body?.event, channel, this.robot.name, this.robot.alias, this.client, (error, message) => {
if (error) { return this.robot.logger.error(`Dropping message due to error ${error.message}`); }
if (error) { return this.robot.logger.error(error, `Dropping message due to error ${error.message}`); }
return this.receive(message);
});
break;
Expand Down
12 changes: 6 additions & 6 deletions src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ class SlackClient extends EventEmitter {
this.robot.logger.info('Connected to Slack after starting socket client.');
// this.emit('connected');
} catch (e) {
this.robot.logger.error(`Error connecting to Slack: ${e.message}`);
this.robot.logger.error(e, `Error connecting to Slack: ${e.message}`);
this.emit('error', e);
}
return startResponse;
Expand Down Expand Up @@ -139,7 +139,7 @@ class SlackClient extends EventEmitter {
return this.robot.logger.debug(`Conversation ${conversationId} is a DM or MPDM. These conversation types do not have topics.`);
}
} catch (e) {
this.robot.logger.error(`Error setting topic in conversation ${conversationId}: ${e.message}`);
this.robot.logger.error(e, `Error setting topic in conversation ${conversationId}: ${e.message}`);
}
}

Expand Down Expand Up @@ -202,12 +202,12 @@ class SlackClient extends EventEmitter {
if (typeof message !== "string") {
return this.web.chat.postMessage({channel: room, text: message.text}, Object.assign(message, options))
.catch(error => {
return this.robot.logger.error(`SlackClient#send() error: ${error.message}`);
return this.robot.logger.error(error, `SlackClient#send() error: ${error.message}`);
});
} else {
return this.web.chat.postMessage({channel: room, text: message.text}, options)
.catch(error => {
return this.robot.logger.error(`SlackClient#send() error: ${error.message}`);
return this.robot.logger.error(error, `SlackClient#send() error: ${error.message}`);
});
}
}
Expand Down Expand Up @@ -411,10 +411,10 @@ class SlackClient extends EventEmitter {
this.eventHandler(fetchedEvent);
}
catch (error) {
this.robot.logger.error(`An error occurred while processing an event: from Slack Client ${error.message}.`);
this.robot.logger.error(error, `An error occurred while processing an event: from Slack Client ${error.message}.`);
}
}).catch(error => {
return this.robot.logger.error(`Incoming message dropped due to error fetching info for a property: ${error.message}.`);
return this.robot.logger.error(error, `Incoming message dropped due to error fetching info for a property: ${error.message}.`);
});
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/message.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ class SlackTextMessage extends TextMessage {
this.text = text;
return cb();
}).catch(error => {
client.robot.logger.error(`An error occurred while building text: ${error.message}`);
client.robot.logger.error(error, `An error occurred while building text: ${error.message}`);
return cb(error);
});
}
Expand Down Expand Up @@ -225,7 +225,7 @@ class SlackTextMessage extends TextMessage {
mentions.push(new SlackMention(res.id, "user", res));
return `@${res.name}`;
}).catch(error => {
client.robot.logger.error(`Error getting user info ${id}: ${error.message}`);
client.robot.logger.error(error, `Error getting user info ${id}: ${error.message}`);
return `<@${id}>`;
});
}
Expand All @@ -247,7 +247,7 @@ class SlackTextMessage extends TextMessage {
return `\#${conversation.name}`;
} else { return `<\#${id}>`; }
}).catch(error => {
client.robot.logger.error(`Error getting conversation info ${id}: ${error.message}`);
client.robot.logger.error(error, `Error getting conversation info ${id}: ${error.message}`);
return `<\#${id}>`;
});
}
Expand Down

0 comments on commit 2c99805

Please sign in to comment.