File tree Expand file tree Collapse file tree 4 files changed +43
-14
lines changed
app/services/task_service
spec/services/task_service Expand file tree Collapse file tree 4 files changed +43
-14
lines changed Original file line number Diff line number Diff line change @@ -9,21 +9,24 @@ def initialize(zip:, account_link:)
99 end
1010
1111 def execute
12- body = @zip . string
13- begin
14- response = self . class . connection . post ( @account_link . push_url ) { |request | request_parameters ( request , body ) }
15- if response . success?
16- nil
17- else
18- response . status == 401 ? I18n . t ( 'tasks.export_external_confirm.not_authorized' , account_link : @account_link . name ) : response . body
19- end
20- rescue StandardError => e
21- e
22- end
12+ response = self . class . connection . post ( @account_link . push_url ) { |request | request_parameters ( request , @zip . string ) }
13+ return nil if response . success?
14+ return I18n . t ( 'tasks.export_external_confirm.not_authorized' , account_link : @account_link . name ) if response . status == 401
15+
16+ handle_error ( message : response . body )
17+ rescue Faraday ::ServerError => e
18+ handle_error ( error : e , message : I18n . t ( 'tasks.export_external_confirm.server_error' , account_link : @account_link . name ) )
19+ rescue StandardError => e
20+ handle_error ( error : e )
2321 end
2422
2523 private
2624
25+ def handle_error ( message : nil , error : nil )
26+ Sentry . capture_exception ( error ) if error . present?
27+ ERB ::Util . html_escape ( message || error . to_s )
28+ end
29+
2730 def request_parameters ( request , body )
2831 request . tap do |req |
2932 req . headers [ 'Content-Type' ] = 'application/zip'
Original file line number Diff line number Diff line change 77 duplicate :
88 error_alert : Die Aufgabe konnte nicht dupliziert werden.
99 export_external_confirm :
10- error : ' Der Export der Aufgabe (%{title}) ist fehlgeschlagen. <br> Fehler: %{error}'
10+ error : ' Der Export der Aufgabe (%{title}) ist fehlgeschlagen. <br><br> Fehler: %{error}'
1111 not_authorized : Die Autorisierung mit "%{account_link}" konnte nicht hergestellt werden. Ist der API-Schlüssel korrekt?
12+ server_error : Verbindung zu %{account_link} fehlgeschlagen. Gegenseite nicht erreichbar.
1213 success : Aufgabe (%{title}) erfolgreich exportiert.
1314 import :
1415 internal_error : Beim Import dieser Aufgabe ist auf CodeHarbor ein interner Fehler aufgetreten.
Original file line number Diff line number Diff line change 77 duplicate :
88 error_alert : Task could not be duplicated
99 export_external_confirm :
10- error : ' Export of task (%{title}) failed. <br> Error: %{error}'
10+ error : ' Export of task (%{title}) failed. <br><br> Error: %{error}'
1111 not_authorized : Authorization with could not be established with "%{account_link}". Is the API Key correct?
12+ server_error : Connection to %{account_link} failed. Remote host unreachable.
1213 success : Task (%{title}) successfully exported.
1314 import :
1415 internal_error : An internal error occurred on CodeHarbor while importing the exercise.
Original file line number Diff line number Diff line change 5151 let ( :status ) { 500 }
5252 let ( :response ) { 'an error occurred' }
5353
54- it { is_expected . to be response }
54+ it { is_expected . to eql response }
55+
56+ context 'when response contains problematic characters' do
57+ let ( :response ) { 'an <error> occurred' }
58+
59+ it { is_expected . to eql 'an <error> occurred' }
60+ end
5561 end
5662
5763 context 'when response status is 401' do
6066
6167 it { is_expected . to eq response }
6268 end
69+
70+ context 'when faraday throws an error' do
71+ let ( :connection ) { instance_double ( Faraday ::Connection ) }
72+ let ( :error ) { Faraday ::ServerError }
73+
74+ before do
75+ allow ( Faraday ) . to receive ( :new ) . and_return ( connection )
76+ allow ( connection ) . to receive ( :post ) . and_raise ( error )
77+ end
78+
79+ it { is_expected . to eql I18n . t ( 'tasks.export_external_confirm.server_error' , account_link : account_link . name ) }
80+
81+ context 'when another error occurs' do
82+ let ( :error ) { 'another error' }
83+
84+ it { is_expected . to eql 'another error' }
85+ end
86+ end
6387 end
6488
6589 context 'when an error occurs' do
You can’t perform that action at this time.
0 commit comments