File tree Expand file tree Collapse file tree 4 files changed +42
-8
lines changed
app/services/exercise_service
spec/services/exercise_service Expand file tree Collapse file tree 4 files changed +42
-8
lines changed Original file line number Diff line number Diff line change @@ -17,21 +17,29 @@ def execute
1717 request . headers [ 'Authorization' ] = "Bearer #{ @codeharbor_link . api_key } "
1818 request . body = body
1919 end
20+ return nil if response . success?
21+ return I18n . t ( 'exercises.export_codeharbor.not_authorized' ) if response . status == 401
2022
21- if response . success?
22- nil
23- else
24- response . status == 401 ? I18n . t ( 'exercises.export_codeharbor.not_authorized' ) : response . body
25- end
23+ handle_error ( message : response . body )
24+ rescue Faraday ::ServerError => e
25+ handle_error ( error : e , message : I18n . t ( 'exercises.export_codeharbor.server_error' ) )
2626 rescue StandardError => e
27- e . message
27+ handle_error ( error : e )
2828 end
2929 end
3030
3131 private
3232
33+ def handle_error ( message : nil , error : nil )
34+ Sentry . capture_exception ( error ) if error . present?
35+ ERB ::Util . html_escape ( message || error . to_s )
36+ end
37+
3338 def connection
3439 Faraday . new ( url : @codeharbor_link . push_url ) do |faraday |
40+ faraday . options [ :open_timeout ] = 5
41+ faraday . options [ :timeout ] = 5
42+
3543 faraday . adapter Faraday . default_adapter
3644 end
3745 end
Original file line number Diff line number Diff line change 103103 export_failed : ' Export ist fehlgeschlagen.<br>ID: %{id}<br>Title: %{title}<br><br>Error: %{error}'
104104 label : Zu CodeHarbor exportieren
105105 not_authorized : Die Autorisierung mit CodeHarbor konnte nicht hergestellt werden. Ist der API-Schlüssel korrekt?
106+ server_error : Verbindung zu CodeHarbor fehlgeschlagen. Gegenseite nicht erreichbar.
106107 successfully_exported : ' Aufgabe wurde erfolgreich exportiert.<br>ID: %{id}<br>Title: %{title}'
107108 external_users :
108109 statistics :
Original file line number Diff line number Diff line change 103103 export_failed : ' Export has failed.<br>ID: %{id}<br>Title: %{title}<br><br>Error: %{error}'
104104 label : Export to CodeHarbor
105105 not_authorized : Authorization with could not be established with CodeHarbor. Is the API Key correct?
106+ server_error : Connection to CodeHarbor failed. Remote host unreachable.
106107 successfully_exported : ' Exercise has been successfully exported.<br>ID: %{id}<br>Title: %{title}'
107108 external_users :
108109 statistics :
Original file line number Diff line number Diff line change 4949
5050 context 'when response status is 500' do
5151 let ( :status ) { 500 }
52- let ( :response ) { 'an error occured ' }
52+ 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
61+
62+ context 'when faraday throws an error' do
63+ let ( :connection ) { instance_double ( Faraday ::Connection ) }
64+ let ( :error ) { Faraday ::ServerError }
65+
66+ before do
67+ allow ( Faraday ) . to receive ( :new ) . and_return ( connection )
68+ allow ( connection ) . to receive ( :post ) . and_raise ( error )
69+ end
70+
71+ it { is_expected . to eql I18n . t ( 'exercises.export_codeharbor.server_error' ) }
72+
73+ context 'when another error occurs' do
74+ let ( :error ) { 'another error' }
75+
76+ it { is_expected . to eql 'another error' }
77+ end
78+ end
5579 end
5680
5781 context 'when response status is 401' do
You can’t perform that action at this time.
0 commit comments