support better errorhandling for codeharbor
This commit is contained in:
@@ -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
|
||||
|
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
|
||||
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
|
||||
|
Reference in New Issue
Block a user