Skip to content

Instantly share code, notes, and snippets.

@lilmuckers
Created April 11, 2013 11:50
Show Gist options
  • Save lilmuckers/5362755 to your computer and use it in GitHub Desktop.
Save lilmuckers/5362755 to your computer and use it in GitHub Desktop.
Error handler that uses the github API to create an issue as a way of logging the error. This depends on the githubber npm library.
//set up the object with the api details
function GitHubError(api, owner, repo)
{
this.api = api
this.repo = {
owner: owner,
repo: repo
}
}
GitHubError.prototype.handle = function(handle)
{
//we're going to handle this error, handle it right good.
//get a list of all existing issues
this.api.issues.list(this.repo.owner, this.repo.repo, {}, function(err, data){
//this has errored... this is embarassing...
if(err){
//at least this error will get logged by forever
this._throwError('Error thrown in error reporting - list issues: '+handle.message)
}
//scan issues for one with a name that matches the error
var exists = false
for(var k in data){
var issue = data[k]
//if one exists - update it with another occurance report
if(issue.title == handle.message){
this._updateIssue(handle, issue)
exists = true
}
}
//if one does not exist, create it
if(!exists){
this._createIssue(handle)
}
}.bind(this));
}
GitHubError.prototype._createIssue = function(handle)
{
//build the data to create the issue with
var issue = {
title: handle.message,
body: "```\n"+handle.stack.toString()+"\n```"
}
//create the issue
this.api.issues.create(this.repo.owner, this.repo.repo, issue, function(err, data){
//oh dear, an error
if(err){
this._throwError('Error thrown in error reporting - create issue: '+handle.message)
}
//otherwise we're good and can just chillax
});
}
GitHubError.prototype._updateIssue = function(handle, issue)
{
//build the data
var comment = {
body: "```\n"+handle.stack.toString()+"\n```"
}
//we want to make sure the issue is open
if(issue.state != 'open'){
this.api.issues.update(this.repo.owner, this.repo.repo, issue.number, {state: 'open'}, function(err, data){
//woops
if(err){
this._throwError('Error thrown in error reporting - reopen issue: '+handle.message)
}
});
}
//create the comment
this.api.issues.comments.create(this.repo.owner, this.repo.repo, issue.number, comment, function(err, data){
//oh dear, an error
if(err){
this._throwError('Error thrown in error reporting - create comment: '+handle.message)
}
//otherwise we're good and can just chillax
});
}
GitHubError.prototype._throwError = function(message)
{
var error = new Error(message)
error.noCatch = true
throw error
}
module.exports = GitHubError
var GitHub = require('githubber'),
ErroHandler = require('error.js')
//instantiate the Githubber github objects
var ghObj = new GitHub.GitHub()
var github = new GitHub.GitHubAPI(ghObj, 'accessToken')
//instantiate the Commit object
var errorhandler = new ErrorHandler(github, 'lilmuckers', 'api-commits')
process.on('uncaughtException', function uncaughtException(err){
if(!err.noCatch){
errorhandler.handle(err)
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment