google conn error graceful handling

closes FOO-1388
flag=none

TEST PLAN:
  1) make google drive fail with timeouts
  2) try to list docs on assignments_controller
  3) do not get a 500, just fail to load them.

Change-Id: I7a70f643e3745a796b3a41baf8d832c275aebc83
Reviewed-on: https://gerrit.instructure.com/c/canvas-lms/+/255718
Tested-by: Service Cloud Jenkins <svc.cloudjenkins@instructure.com>
Reviewed-by: Simon Williams <simon@instructure.com>
QA-Review: Simon Williams <simon@instructure.com>
Product-Review: Simon Williams <simon@instructure.com>
This commit is contained in:
Ethan Vizitei 2020-12-21 13:29:08 -06:00
parent 53b11cc927
commit 9bfbf0d396
2 changed files with 25 additions and 6 deletions

View File

@ -347,10 +347,18 @@ class AssignmentsController < ApplicationController
# prevent masquerading users from accessing google docs
if assignment.allow_google_docs_submission? && @real_current_user.blank?
docs = {}
status_code = :ok
begin
docs = google_drive_connection.list_with_extension_filter(assignment.allowed_extensions)
rescue GoogleDrive::NoTokenError, Google::APIClient::AuthorizationError => e
rescue GoogleDrive::NoTokenError,
Google::APIClient::AuthorizationError => e
Canvas::Errors.capture_exception(:oauth, e, :warn)
docs = { errors: { base: t("Auth failure in connecting to Google Drive.") } }
status_code = :unauthorized
rescue GoogleDrive::ConnectionException => e
Canvas::Errors.capture_exception(:oauth, e, :warn)
docs = { errors: { base: t("Unable to connect to Google Drive.") } }
status_code = :gateway_timeout
rescue ArgumentError => e
Canvas::Errors.capture_exception(:oauth, e)
rescue => e
@ -358,7 +366,7 @@ class AssignmentsController < ApplicationController
raise e
end
respond_to do |format|
format.json { render json: docs.to_hash }
format.json { render json: docs.to_hash, status: status_code }
end
else
error_object = {errors:

View File

@ -1762,17 +1762,28 @@ describe AssignmentsController do
end
describe "GET list_google_docs" do
it "passes errors through to Canvas::Errors" do
let(:connection){ double() }
let(:params){ {course_id: @course.id, id: @assignment.id} }
before(:each) do
user_session(@teacher)
connection = double()
allow(connection).to receive(:list_with_extension_filter).and_raise(ArgumentError)
allow(controller).to receive(:google_drive_connection).and_return(connection)
allow_any_instance_of(Assignment).to receive(:allow_google_docs_submission?).and_return(true)
end
it "passes errors through to Canvas::Errors" do
allow(connection).to receive(:list_with_extension_filter).and_raise(ArgumentError)
expect(Canvas::Errors).to receive(:capture_exception)
params = {course_id: @course.id, id: @assignment.id}
get 'list_google_docs', params: params, format: 'json'
expect(response.code).to eq("200")
end
it "gives appropriate error code to connection errors" do
allow(connection).to receive(:list_with_extension_filter).and_raise(GoogleDrive::ConnectionException)
get 'list_google_docs', params: params, format: 'json'
expect(response.code).to eq("504")
expect(response.body).to include("Unable to connect to Google Drive")
end
end
end