File tree 4 files changed +41
-8
lines changed
app/services/exercise_service
spec/services/exercise_service
4 files changed +41
-8
lines changed Original file line number Diff line number Diff line change @@ -17,15 +17,22 @@ def execute
17
17
request . headers [ 'Authorization' ] = "Bearer #{ @codeharbor_link . api_key } "
18
18
request . body = body
19
19
end
20
+ return nil if response . success?
21
+ return I18n . t ( 'exercises.export_codeharbor.not_authorized' ) if response . status == 401
20
22
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' ) )
26
26
rescue StandardError => e
27
- e . message
27
+ handle_error ( error : e )
28
28
end
29
29
end
30
+
31
+ private
32
+
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
30
37
end
31
38
end
Original file line number Diff line number Diff line change 103
103
export_failed : ' Export ist fehlgeschlagen.<br>ID: %{id}<br>Title: %{title}<br><br>Error: %{error}'
104
104
label : Zu CodeHarbor exportieren
105
105
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.
106
107
successfully_exported : ' Aufgabe wurde erfolgreich exportiert.<br>ID: %{id}<br>Title: %{title}'
107
108
external_users :
108
109
statistics :
Original file line number Diff line number Diff line change 103
103
export_failed : ' Export has failed.<br>ID: %{id}<br>Title: %{title}<br><br>Error: %{error}'
104
104
label : Export to CodeHarbor
105
105
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.
106
107
successfully_exported : ' Exercise has been successfully exported.<br>ID: %{id}<br>Title: %{title}'
107
108
external_users :
108
109
statistics :
Original file line number Diff line number Diff line change 49
49
50
50
context 'when response status is 500' do
51
51
let ( :status ) { 500 }
52
- let ( :response ) { 'an error occured ' }
52
+ let ( :response ) { 'an error occurred ' }
53
53
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
55
79
end
56
80
57
81
context 'when response status is 401' do
You can’t perform that action at this time.
0 commit comments