support better errorhandling for codeharbor
This commit is contained in:
2
Gemfile
2
Gemfile
@ -37,7 +37,7 @@ gem 'rest-client'
|
|||||||
gem 'rubyzip'
|
gem 'rubyzip'
|
||||||
gem 'mnemosyne-ruby'
|
gem 'mnemosyne-ruby'
|
||||||
gem 'faraday'
|
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 'whenever', require: false
|
||||||
gem 'rails-timeago'
|
gem 'rails-timeago'
|
||||||
|
|
||||||
|
19
Gemfile.lock
19
Gemfile.lock
@ -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
|
GIT
|
||||||
remote: https://github.com/gosukiwi/tubesock
|
remote: https://github.com/gosukiwi/tubesock
|
||||||
revision: 86a5ca4f7d3c3a7b9a727ad91df3b9b4912eda39
|
revision: 86a5ca4f7d3c3a7b9a727ad91df3b9b4912eda39
|
||||||
@ -17,6 +7,15 @@ GIT
|
|||||||
rack (>= 1.5.0)
|
rack (>= 1.5.0)
|
||||||
websocket (>= 1.1.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
|
GEM
|
||||||
remote: https://rubygems.org/
|
remote: https://rubygems.org/
|
||||||
specs:
|
specs:
|
||||||
|
@ -107,26 +107,7 @@ class ExercisesController < ApplicationController
|
|||||||
end
|
end
|
||||||
|
|
||||||
def export_external_check
|
def export_external_check
|
||||||
@codeharbor_link = current_user.codeharbor_link
|
codeharbor_check = ExerciseService::CheckExternal.call(uuid: @exercise.uuid, 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
|
|
||||||
|
|
||||||
render json: {
|
render json: {
|
||||||
message: codeharbor_check[:message],
|
message: codeharbor_check[:message],
|
||||||
@ -196,8 +177,12 @@ class ExercisesController < ApplicationController
|
|||||||
exercise.save!
|
exercise.save!
|
||||||
return render json: {}, status: 201
|
return render json: {}, status: 201
|
||||||
end
|
end
|
||||||
logger.info(exercise.errors.full_messages)
|
# logger.info(exercise.errors.full_messages)
|
||||||
render json: {}, status: 400
|
# 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
|
end
|
||||||
|
|
||||||
def user_from_api_key
|
def user_from_api_key
|
||||||
|
34
app/services/exercise_service/check_external.rb
Normal file
34
app/services/exercise_service/check_external.rb
Normal 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
|
@ -10,11 +10,7 @@ module ExerciseService
|
|||||||
def execute
|
def execute
|
||||||
body = @zip.string
|
body = @zip.string
|
||||||
begin
|
begin
|
||||||
conn = Faraday.new(url: @codeharbor_link.push_url) do |faraday|
|
response = connection.post do |request|
|
||||||
faraday.adapter Faraday.default_adapter
|
|
||||||
end
|
|
||||||
|
|
||||||
response = conn.post do |request|
|
|
||||||
request.headers['Content-Type'] = 'application/zip'
|
request.headers['Content-Type'] = 'application/zip'
|
||||||
request.headers['Content-Length'] = body.length.to_s
|
request.headers['Content-Length'] = body.length.to_s
|
||||||
request.headers['Authorization'] = 'Bearer ' + @codeharbor_link.api_key
|
request.headers['Authorization'] = 'Bearer ' + @codeharbor_link.api_key
|
||||||
@ -26,5 +22,13 @@ module ExerciseService
|
|||||||
return e.message
|
return e.message
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def connection
|
||||||
|
Faraday.new(url: @codeharbor_link.push_url) do |faraday|
|
||||||
|
faraday.adapter Faraday.default_adapter
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -337,6 +337,9 @@ en:
|
|||||||
create_new: Create new
|
create_new: Create new
|
||||||
close: Close
|
close: Close
|
||||||
abort: Abort
|
abort: Abort
|
||||||
|
export_errors:
|
||||||
|
invalid: Invalid exercise
|
||||||
|
internal_error: An internal error occurred on Codeharbor while importing the exercise.
|
||||||
file_form:
|
file_form:
|
||||||
hints:
|
hints:
|
||||||
feedback_message: This message is used as a hint for failing tests.
|
feedback_message: This message is used as a hint for failing tests.
|
||||||
|
Reference in New Issue
Block a user