Commit a2c93299edd31b31f6a0fa51a41a9b83110ca527

Authored by Nathan Broadbent
1 parent 41381ee2
Exists in master and in 1 other branch production

Added error handling for issue trackers, display error message if something goes wrong

app/controllers/errs_controller.rb
@@ -39,7 +39,11 @@ class ErrsController < ApplicationController @@ -39,7 +39,11 @@ class ErrsController < ApplicationController
39 set_tracker_params 39 set_tracker_params
40 40
41 if @app.issue_tracker 41 if @app.issue_tracker
42 - @app.issue_tracker.create_issue @problem, current_user 42 + begin
  43 + @app.issue_tracker.create_issue @problem, current_user
  44 + rescue IssueTrackers::IssueTrackerError => ex
  45 + flash[:error] = ex.message
  46 + end
43 else 47 else
44 flash[:error] = "This app has no issue tracker setup." 48 flash[:error] = "This app has no issue tracker setup."
45 end 49 end
app/models/issue_trackers/github_issues_tracker.rb
@@ -21,8 +21,12 @@ class IssueTrackers::GithubIssuesTracker < IssueTracker @@ -21,8 +21,12 @@ class IssueTrackers::GithubIssuesTracker < IssueTracker
21 21
22 def create_issue(problem, reported_by = nil) 22 def create_issue(problem, reported_by = nil)
23 client = Octokit::Client.new(:login => username, :password => password) 23 client = Octokit::Client.new(:login => username, :password => password)
24 - issue = client.create_issue(project_id, issue_title(problem), body_template.result(binding).unpack('C*').pack('U*'), options = {})  
25 - problem.update_attribute :issue_link, issue.issue.html_url 24 + begin
  25 + issue = client.create_issue(project_id, issue_title(problem), body_template.result(binding).unpack('C*').pack('U*'), options = {})
  26 + problem.update_attribute :issue_link, issue.issue.html_url
  27 + rescue Octokit::Unauthorized
  28 + raise IssueTrackers::AuthenticationError, "Could not authenticate with Github. Please check your username and password."
  29 + end
26 end 30 end
27 31
28 def body_template 32 def body_template
config/initializers/issue_trackers.rb
@@ -2,5 +2,6 @@ @@ -2,5 +2,6 @@
2 include IssueTrackers 2 include IssueTrackers
3 3
4 # Require all issue tracker apis in lib/issue_tracker_apis 4 # Require all issue tracker apis in lib/issue_tracker_apis
5 -Dir.glob(Rails.root.join('lib/issue_tracker_apis/*.rb')).each {|t| require t }  
6 - 5 +Dir.glob(Rails.root.join('lib/issue_trackers/apis/*.rb')).each {|t| require t }
  6 +# Require issue tracker error classes
  7 +require Rails.root.join('lib/issue_trackers/errors')
lib/issue_tracker_apis/mingle.rb
@@ -1,15 +0,0 @@ @@ -1,15 +0,0 @@
1 -module Mingle  
2 - class Card < ActiveResource::Base  
3 - # site template ~> "https://username:password@mingle.example.com/api/v1/projects/:project_id/"  
4 - self.format = :xml  
5 - end  
6 - def self.set_site(site)  
7 - # ActiveResource seems to clone and freeze the @site variable  
8 - # after the first use. It seems that the only way to change @site  
9 - # is to drop the subclass, and then reload it.  
10 - Mingle.send(:remove_const, :Card)  
11 - load File.join(Rails.root,'lib','issue_tracker_apis','mingle.rb')  
12 - Mingle::Card.site = site  
13 - end  
14 -end  
15 -  
lib/issue_trackers/apis/mingle.rb 0 → 100644
@@ -0,0 +1,15 @@ @@ -0,0 +1,15 @@
  1 +module Mingle
  2 + class Card < ActiveResource::Base
  3 + # site template ~> "https://username:password@mingle.example.com/api/v1/projects/:project_id/"
  4 + self.format = :xml
  5 + end
  6 + def self.set_site(site)
  7 + # ActiveResource seems to clone and freeze the @site variable
  8 + # after the first use. It seems that the only way to change @site
  9 + # is to drop the subclass, and then reload it.
  10 + Mingle.send(:remove_const, :Card)
  11 + load File.join(Rails.root,'lib','issue_trackers', 'apis','mingle.rb')
  12 + Mingle::Card.site = site
  13 + end
  14 +end
  15 +
lib/issue_trackers/errors.rb 0 → 100644
@@ -0,0 +1,4 @@ @@ -0,0 +1,4 @@
  1 +module IssueTrackers
  2 + class IssueTrackerError < StandardError; end
  3 + class AuthenticationError < IssueTrackerError; end
  4 +end