Skip to content

Commit

Permalink
Catch and optionally announce exceptions during formatting
Browse files Browse the repository at this point in the history
Fix #8
  • Loading branch information
lee-dohm committed Mar 7, 2015
1 parent a324b3d commit 7147b9e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Add the package `hubot-github-event-announcer` as a dependency in your Hubot `pa

```json
"dependencies": {
"hubot-github-event-announcer": "0.6.0"
"hubot-github-event-announcer": "0.7.0"
}
```

Expand All @@ -50,6 +50,7 @@ The GitHub Event Announcer responds to the URL `http://hubot.example.com/hubot/g

It also can be configured using the following environment values:

* `HUBOT_GITHUB_EVENT_ANNOUNCE_EXCEPTIONS` — If present, announces exceptions that occur during formatting
* `HUBOT_GITHUB_EVENT_DEFAULT_ROOM` — If no room is specified in the hook, the announcer will send events to this room

## Copyright
Expand Down
29 changes: 20 additions & 9 deletions src/github-event-announcer.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
# None
#
# Configuration:
# HUBOT_GITHUB_EVENT_DEFAULT_ROOM - Room name of the default room to announce events in.
# HUBOT_GITHUB_EVENT_SECRET - Secret that matches the value stored in the GitHub hook definition.
# HUBOT_GITHUB_EVENT_ANNOUNCE_EXCEPTIONS - If present, announces exceptions that occur during formatting
# HUBOT_GITHUB_EVENT_DEFAULT_ROOM - Room name of the default room to announce events in
# HUBOT_GITHUB_EVENT_SECRET - Secret that matches the value stored in the GitHub hook definition
#
# Commands:
# None
Expand Down Expand Up @@ -39,13 +40,23 @@ module.exports = (robot) ->
# * `room` Room {String} to announce the event to.
# * `message` Message {String} to use to announce the event.
announceEvent = (event, callback) ->
formatter = formatters[event.type] ? formatters.unhandled
message = formatter(event)
if message
callback(event.room, message)
else
event.robot.logger.info "Formatter for '#{event.type}' refused to format:
JSON.stringify(event, null, 2)"
try
formatter = formatters[event.type] ? formatters.unhandled
message = formatter(event)
if message
callback(event.room, message)
else
event.robot.logger.info "Formatter for #{event.type} event refused to format:
#{JSON.stringify(event, null, 2)}"
catch err
if process.env.HUBOT_GITHUB_EVENT_ANNOUNCE_EXCEPTIONS
event.robot.messageRoom event.room, """
Exception occurred while formatting #{event.type} event
#{JSON.stringify(err, null, 2)}
"""

event.robot.emit 'error', err, "Exception occurred while formatting #{event.type} event"

# Public: Receives the GitHub event webhook request.
#
Expand Down

0 comments on commit 7147b9e

Please sign in to comment.