diff --git a/app/controllers/api/v0/github_repos_controller.rb b/app/controllers/api/v0/github_repos_controller.rb index e864afa43..6afc1ab67 100644 --- a/app/controllers/api/v0/github_repos_controller.rb +++ b/app/controllers/api/v0/github_repos_controller.rb @@ -1,6 +1,6 @@ module Api module V0 - class GithubReposController < ApplicationController + class GithubReposController < ApiController def index client = create_octokit_client existing_user_repos = GithubRepo. @@ -10,6 +10,8 @@ module Api repo.selected = existing_user_repos.include?(repo.id) repo end + rescue Octokit::Unauthorized => e + render json: { error: "Github Unauthorized: #{e.message}", status: 401 }, status: :unauthorized end def update_or_create diff --git a/spec/requests/api/v0/github_repos_spec.rb b/spec/requests/api/v0/github_repos_spec.rb index fe1f593d8..f3d73f2f6 100644 --- a/spec/requests/api/v0/github_repos_spec.rb +++ b/spec/requests/api/v0/github_repos_spec.rb @@ -19,6 +19,13 @@ RSpec.describe "Api::V0::GithubRepos", type: :request do get "/api/github_repos" expect(response).to have_http_status(:ok) end + + it "returns 401 if github raises an unauthorized error" do + allow(Octokit::Client).to receive(:new).and_raise(Octokit::Unauthorized) + get "/api/github_repos" + expect(response).to have_http_status(:unauthorized) + expect(response.parsed_body["error"]).to include("Github Unauthorized") + end end describe "POST /api/v0/github_repos/update_or_create" do