support better errorhandling for codeharbor

This commit is contained in:
Karol
2019-10-25 16:25:57 +02:00
parent c0a0b44c4d
commit 3912caab1c
6 changed files with 63 additions and 38 deletions

View File

@ -37,7 +37,7 @@ gem 'rest-client'
gem 'rubyzip'
gem 'mnemosyne-ruby'
gem 'faraday'
gem 'proforma', git: 'git://github.com/openHPI/proforma.git' # use version not master
gem 'proforma', path: '../proforma' #, git: 'git://github.com/openHPI/proforma.git' # use version not master
gem 'whenever', require: false
gem 'rails-timeago'

View File

@ -1,13 +1,3 @@
GIT
remote: git://github.com/openHPI/proforma.git
revision: 54a5bdf2bafebb548998c945ee68d099d85108df
specs:
proforma (0.1.0)
activemodel (~> 5.2.3)
activesupport (~> 5.2.3)
nokogiri (~> 1.10.2)
rubyzip (>= 1.2.2, < 2.1.0)
GIT
remote: https://github.com/gosukiwi/tubesock
revision: 86a5ca4f7d3c3a7b9a727ad91df3b9b4912eda39
@ -17,6 +7,15 @@ GIT
rack (>= 1.5.0)
websocket (>= 1.1.0)
PATH
remote: ../proforma
specs:
proforma (0.1.0)
activemodel (~> 5.2.3)
activesupport (~> 5.2.3)
nokogiri (~> 1.10.2)
rubyzip (>= 1.2.2, < 2.1.0)
GEM
remote: https://rubygems.org/
specs:

View File

@ -107,26 +107,7 @@ class ExercisesController < ApplicationController
end
def export_external_check
@codeharbor_link = current_user.codeharbor_link
conn = Faraday.new(url: @codeharbor_link.check_uuid_url) do |faraday|
faraday.options[:open_timeout] = 5
faraday.options[:timeout] = 5
faraday.adapter Faraday.default_adapter
end
codeharbor_check = begin
response = conn.post do |req|
req.headers['Content-Type'] = 'application/json'
req.headers['Authorization'] = 'Bearer ' + @codeharbor_link.api_key
req.body = {uuid: @exercise.uuid}.to_json
end
response_hash = JSON.parse(response.body, symbolize_names: true)
{error: false}.merge(response_hash.slice(:message, :exercise_found, :update_right))
rescue Faraday::Error => e
{error: true, message: t('exercises.export_codeharbor.error', message: e.message)}
end
codeharbor_check = ExerciseService::CheckExternal.call(uuid: @exercise.uuid, codeharbor_link: current_user.codeharbor_link)
render json: {
message: codeharbor_check[:message],
@ -196,8 +177,12 @@ class ExercisesController < ApplicationController
exercise.save!
return render json: {}, status: 201
end
logger.info(exercise.errors.full_messages)
render json: {}, status: 400
# logger.info(exercise.errors.full_messages)
# render json: {}, status: 400
rescue Proforma::ProformaError
render json: t('exercises.export_codeharbor.export_errors.invalid'), status: 400
rescue StandardError
render json: t('exercises.export_codeharbor.export_errors.internal_error'), status: 500
end
def user_from_api_key

View File

@ -0,0 +1,34 @@
# frozen_string_literal: true
module ExerciseService
class CheckExternal < ServiceBase
def initialize(uuid:, codeharbor_link:)
@uuid = uuid
@codeharbor_link = codeharbor_link
end
def execute
response = connection.post do |req|
req.headers['Content-Type'] = 'application/json'
req.headers['Authorization'] = 'Bearer ' + @codeharbor_link.api_key
req.body = {uuid: @uuid}.to_json
end
response_hash = JSON.parse(response.body, symbolize_names: true)
{error: false}.merge(response_hash.slice(:message, :exercise_found, :update_right))
rescue Faraday::Error => e
{error: true, message: t('exercises.export_exercise.error', message: e.message)}
end
private
def connection
Faraday.new(url: @codeharbor_link.check_uuid_url) do |faraday|
faraday.options[:open_timeout] = 5
faraday.options[:timeout] = 5
faraday.adapter Faraday.default_adapter
end
end
end
end

View File

@ -10,11 +10,7 @@ module ExerciseService
def execute
body = @zip.string
begin
conn = Faraday.new(url: @codeharbor_link.push_url) do |faraday|
faraday.adapter Faraday.default_adapter
end
response = conn.post do |request|
response = connection.post do |request|
request.headers['Content-Type'] = 'application/zip'
request.headers['Content-Length'] = body.length.to_s
request.headers['Authorization'] = 'Bearer ' + @codeharbor_link.api_key
@ -26,5 +22,13 @@ module ExerciseService
return e.message
end
end
private
def connection
Faraday.new(url: @codeharbor_link.push_url) do |faraday|
faraday.adapter Faraday.default_adapter
end
end
end
end

View File

@ -337,6 +337,9 @@ en:
create_new: Create new
close: Close
abort: Abort
export_errors:
invalid: Invalid exercise
internal_error: An internal error occurred on Codeharbor while importing the exercise.
file_form:
hints:
feedback_message: This message is used as a hint for failing tests.