Skip to content

Commit 7a0b816

Browse files
mk-fgprogval
authored andcommitted
Handle (log/fallback) connection errors when fetching well-known/matrix/client URLs
1 parent 6cb502d commit 7a0b816

File tree

4 files changed

+50
-38
lines changed

4 files changed

+50
-38
lines changed

lib/matrix_client/client.ex

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -449,14 +449,14 @@ defmodule M51.MatrixClient.Client do
449449

450450
wellknown_url = "https://" <> hostname <> "/.well-known/matrix/client"
451451

452-
case httpoison.get!(wellknown_url) do
453-
%HTTPoison.Response{status_code: 200, body: body} ->
452+
case httpoison.get(wellknown_url) do
453+
{:ok, %HTTPoison.Response{status_code: 200, body: body}} ->
454454
data = Jason.decode!(body)
455455
base_url = data["m.homeserver"]["base_url"]
456456
Logger.debug("Well-known request for #{wellknown_url} yielded #{base_url}")
457457
base_url
458458

459-
%HTTPoison.Response{status_code: 404} ->
459+
{:ok, %HTTPoison.Response{status_code: 404}} ->
460460
base_url = "https://" <> hostname
461461

462462
Logger.debug(
@@ -466,7 +466,7 @@ defmodule M51.MatrixClient.Client do
466466

467467
base_url
468468

469-
res ->
469+
{:ok, res} ->
470470
# The next call will probably fail, but this spares error handling in this one.
471471
base_url = "https://" <> hostname
472472

@@ -476,6 +476,18 @@ defmodule M51.MatrixClient.Client do
476476
)
477477

478478
base_url
479+
480+
{:error, %HTTPoison.Error{reason: err}} ->
481+
# Treat this in the same way as HTTP error codes above - perm error and fallback
482+
base_url = "https://" <> hostname
483+
484+
Logger.warn(
485+
"Well-known request for #{wellknown_url} failed" <>
486+
" with connection error [#{err}]. Falling back to #{base_url}"
487+
)
488+
489+
base_url
490+
479491
end
480492
end
481493

test/format/common_test.exs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -117,21 +117,21 @@ defmodule M51.FormatTest do
117117

118118
test "Matrix link to IRC" do
119119
MockHTTPoison
120-
|> expect(:get!, 4, fn url ->
120+
|> expect(:get, 4, fn url ->
121121
assert url == "https://example.org/.well-known/matrix/client"
122122

123-
%HTTPoison.Response{
123+
{:ok, %HTTPoison.Response{
124124
status_code: 200,
125125
body: ~s({"m.homeserver": {"base_url": "https://api.example.org"}})
126-
}
126+
}}
127127
end)
128-
|> expect(:get!, 1, fn url ->
128+
|> expect(:get, 1, fn url ->
129129
assert url == "https://homeserver.org/.well-known/matrix/client"
130130

131-
%HTTPoison.Response{
131+
{:ok, %HTTPoison.Response{
132132
status_code: 200,
133133
body: ~s({"m.homeserver": {"base_url": "https://api.homeserver.org"}})
134-
}
134+
}}
135135
end)
136136

137137
assert M51.Format.matrix2irc(~s(<a href="https://example.org">foo</a>)) ==
@@ -178,13 +178,13 @@ defmodule M51.FormatTest do
178178

179179
test "Matrix link to IRC (404 on well-known)" do
180180
MockHTTPoison
181-
|> expect(:get!, 1, fn url ->
181+
|> expect(:get, 1, fn url ->
182182
assert url == "https://example.org/.well-known/matrix/client"
183183

184-
%HTTPoison.Response{
184+
{:ok, %HTTPoison.Response{
185185
status_code: 404,
186186
body: ~s(this is not JSON)
187-
}
187+
}}
188188
end)
189189

190190
assert M51.Format.matrix2irc(~s(<img src="mxc://example.org/foo" />)) ==

test/matrix_client/client_test.exs

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ defmodule M51.MatrixClient.ClientTest do
3232

3333
def expect_login(mock_httpoison) do
3434
mock_httpoison
35-
|> expect(:get!, fn url ->
35+
|> expect(:get, fn url ->
3636
assert url == "https://matrix.example.org/.well-known/matrix/client"
37-
%HTTPoison.Response{status_code: 404, body: "Error 404"}
37+
{:ok, %HTTPoison.Response{status_code: 404, body: "Error 404"}}
3838
end)
3939
|> expect(:get!, fn url ->
4040
assert url == "https://matrix.example.org/_matrix/client/r0/login"
@@ -81,15 +81,15 @@ defmodule M51.MatrixClient.ClientTest do
8181

8282
test "connection to non-homeserver", %{sup_pid: sup_pid} do
8383
MockHTTPoison
84-
|> expect(:get!, fn url ->
84+
|> expect(:get, fn url ->
8585
assert url == "https://example.org/.well-known/matrix/client"
8686

87-
%HTTPoison.Response{
87+
{:ok, %HTTPoison.Response{
8888
status_code: 404,
8989
body: """
9090
Error 404
9191
"""
92-
}
92+
}}
9393
end)
9494
|> expect(:get!, fn url ->
9595
assert url == "https://example.org/_matrix/client/r0/login"
@@ -119,15 +119,15 @@ defmodule M51.MatrixClient.ClientTest do
119119

120120
test "connection without well-known", %{sup_pid: sup_pid} do
121121
MockHTTPoison
122-
|> expect(:get!, fn url ->
122+
|> expect(:get, fn url ->
123123
assert url == "https://matrix.example.org/.well-known/matrix/client"
124124

125-
%HTTPoison.Response{
125+
{:ok, %HTTPoison.Response{
126126
status_code: 404,
127127
body: """
128128
Error 404
129129
"""
130-
}
130+
}}
131131
end)
132132
|> expect(:get!, fn url ->
133133
assert url == "https://matrix.example.org/_matrix/client/r0/login"
@@ -192,8 +192,8 @@ defmodule M51.MatrixClient.ClientTest do
192192

193193
test "connection with well-known", %{sup_pid: sup_pid} do
194194
MockHTTPoison
195-
|> expect(:get!, fn _url ->
196-
%HTTPoison.Response{
195+
|> expect(:get, fn _url ->
196+
{:ok, %HTTPoison.Response{
197197
status_code: 200,
198198
body: """
199199
{
@@ -202,7 +202,7 @@ defmodule M51.MatrixClient.ClientTest do
202202
}
203203
}
204204
"""
205-
}
205+
}}
206206
end)
207207
|> expect(:get!, fn url ->
208208
assert url == "https://matrix.example.com/_matrix/client/r0/login"
@@ -270,15 +270,15 @@ defmodule M51.MatrixClient.ClientTest do
270270

271271
test "connection without password flow", %{sup_pid: sup_pid} do
272272
MockHTTPoison
273-
|> expect(:get!, fn url ->
273+
|> expect(:get, fn url ->
274274
assert url == "https://matrix.example.org/.well-known/matrix/client"
275275

276-
%HTTPoison.Response{
276+
{:ok, %HTTPoison.Response{
277277
status_code: 404,
278278
body: """
279279
Error 404
280280
"""
281-
}
281+
}}
282282
end)
283283
|> expect(:get!, fn url ->
284284
assert url == "https://matrix.example.org/_matrix/client/r0/login"
@@ -314,15 +314,15 @@ defmodule M51.MatrixClient.ClientTest do
314314

315315
test "connection with invalid password", %{sup_pid: sup_pid} do
316316
MockHTTPoison
317-
|> expect(:get!, fn url ->
317+
|> expect(:get, fn url ->
318318
assert url == "https://matrix.example.org/.well-known/matrix/client"
319319

320-
%HTTPoison.Response{
320+
{:ok, %HTTPoison.Response{
321321
status_code: 404,
322322
body: """
323323
Error 404
324324
"""
325-
}
325+
}}
326326
end)
327327
|> expect(:get!, fn url ->
328328
assert url == "https://matrix.example.org/_matrix/client/r0/login"
@@ -374,9 +374,9 @@ defmodule M51.MatrixClient.ClientTest do
374374

375375
test "registration", %{sup_pid: sup_pid} do
376376
MockHTTPoison
377-
|> expect(:get!, fn url ->
377+
|> expect(:get, fn url ->
378378
assert url == "https://matrix.example.org/.well-known/matrix/client"
379-
%HTTPoison.Response{status_code: 404, body: "Error 404"}
379+
{:ok, %HTTPoison.Response{status_code: 404, body: "Error 404"}}
380380
end)
381381
|> expect(:post!, fn url, body ->
382382
assert url == "https://matrix.example.org/_matrix/client/r0/register"

test/matrix_client/poller_test.exs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1146,23 +1146,23 @@ defmodule M51.MatrixClient.PollerTest do
11461146
test "messages (is_backlog=#{is_backlog})" do
11471147
if unquote(is_backlog) do
11481148
MockHTTPoison
1149-
|> expect(:get!, 0, fn url ->
1149+
|> expect(:get, 0, fn url ->
11501150
assert url == "https://matrix.org/.well-known/matrix/client"
11511151

1152-
%HTTPoison.Response{
1152+
{:ok, %HTTPoison.Response{
11531153
status_code: 200,
11541154
body: ~s({"m.homeserver": {"base_url": "https://matrix-client.matrix.org"}})
1155-
}
1155+
}}
11561156
end)
11571157
else
11581158
MockHTTPoison
1159-
|> expect(:get!, 5, fn url ->
1159+
|> expect(:get, 5, fn url ->
11601160
assert url == "https://matrix.org/.well-known/matrix/client"
11611161

1162-
%HTTPoison.Response{
1162+
{:ok, %HTTPoison.Response{
11631163
status_code: 200,
11641164
body: ~s({"m.homeserver": {"base_url": "https://matrix-client.matrix.org"}})
1165-
}
1165+
}}
11661166
end)
11671167
end
11681168

0 commit comments

Comments
 (0)