diff options
author | Hubert Chathi <hubert@uhoreg.ca> | 2020-05-09 11:03:08 -0400 |
---|---|---|
committer | Hubert Chathi <hubert@uhoreg.ca> | 2020-05-09 11:03:08 -0400 |
commit | 654920f4419edef75757e14a9f9aa6c6a0f3ba41 (patch) | |
tree | 1f89c2bd726f0d0e50b7c2ca936e94884a9d2875 | |
parent | release 0.2.2 (diff) |
parse errors and reduce code duplication
20 files changed, 143 insertions, 75 deletions
diff --git a/lib/polyjuice/client/endpoint.ex b/lib/polyjuice/client/endpoint.ex index 1b6b7b5..f6c8247 100644 --- a/lib/polyjuice/client/endpoint.ex +++ b/lib/polyjuice/client/endpoint.ex @@ -68,4 +68,38 @@ defmodule Polyjuice.Client.Endpoint do ) :: any def transform_http_result(endpoint_args, status_code, headers, body) end + + defprotocol BodyParser do + @fallback_to_any true + @spec parse(endpoint_args :: __MODULE__.t(), body :: any) :: {:ok, any} | any + def parse(endpoint_args, body) + end + + defimpl BodyParser, for: Any do + def parse(_endpoint_args, body), do: {:ok, body} + end + + @spec parse_response( + endpoint_args :: map, + status_code :: integer(), + headers :: [{String.t(), String.t()}, ...], + body :: String.t() + ) :: {:ok, any} | any + def parse_response(%{} = endpoint_args, status_code, headers, body) + when is_integer(status_code) and is_list(headers) and is_binary(body) do + # FIXME: check if content type is "application/json" + with {:ok, json} <- Poison.decode(body) do + case status_code do + 200 -> + Polyjuice.Client.Endpoint.BodyParser.parse(endpoint_args, json) + + _ -> + {:error, status_code, json} + end + else + _ -> + {:error, if(status_code == 200, do: 500, else: status_code), + %{"errcode" => "CA_UHOREG_POLYJUICE_BAD_RESPONSE", "body" => body}} + end + end end diff --git a/lib/polyjuice/client/endpoint/get_rooms_messages.ex b/lib/polyjuice/client/endpoint/get_rooms_messages.ex index bceea3f..3a5d2df 100644 --- a/lib/polyjuice/client/endpoint/get_rooms_messages.ex +++ b/lib/polyjuice/client/endpoint/get_rooms_messages.ex @@ -78,14 +78,8 @@ defmodule Polyjuice.Client.Endpoint.GetRoomsMessages do } end - def transform_http_result(_req, status_code, _resp_headers, body) do - case status_code do - 200 -> - {:ok, Poison.decode!(body)} - - _ -> - {:error, status_code, body} - end + def transform_http_result(req, status_code, resp_headers, body) do + Polyjuice.Client.Endpoint.parse_response(req, status_code, resp_headers, body) end end end diff --git a/lib/polyjuice/client/endpoint/get_sync.ex b/lib/polyjuice/client/endpoint/get_sync.ex index c30e680..511fb21 100644 --- a/lib/polyjuice/client/endpoint/get_sync.ex +++ b/lib/polyjuice/client/endpoint/get_sync.ex @@ -84,14 +84,8 @@ defmodule Polyjuice.Client.Endpoint.GetSync do } end - def transform_http_result(_req, status_code, _resp_headers, body) do - case status_code do - 200 -> - {:ok, Poison.decode!(body)} - - _ -> - {:error, status_code, body} - end + def transform_http_result(req, status_code, resp_headers, body) do + Polyjuice.Client.Endpoint.parse_response(req, status_code, resp_headers, body) end end end diff --git a/lib/polyjuice/client/endpoint/post_join.ex b/lib/polyjuice/client/endpoint/post_join.ex index bdc8c0a..6689f59 100644 --- a/lib/polyjuice/client/endpoint/post_join.ex +++ b/lib/polyjuice/client/endpoint/post_join.ex @@ -76,14 +76,14 @@ defmodule Polyjuice.Client.Endpoint.PostJoin do } end - def transform_http_result(_req, status_code, _resp_headers, body) do - case status_code do - 200 -> - {:ok, body |> Poison.decode!() |> Map.get("room_id")} + def transform_http_result(req, status_code, resp_headers, body) do + Polyjuice.Client.Endpoint.parse_response(req, status_code, resp_headers, body) + end + end - _ -> - {:error, status_code, body} - end + defimpl Polyjuice.Client.Endpoint.BodyParser do + def parse(_req, parsed) do + {:ok, Map.get(parsed, "room_id")} end end end diff --git a/lib/polyjuice/client/endpoint/post_login.ex b/lib/polyjuice/client/endpoint/post_login.ex index e374b41..23fa5c3 100644 --- a/lib/polyjuice/client/endpoint/post_login.ex +++ b/lib/polyjuice/client/endpoint/post_login.ex @@ -84,14 +84,8 @@ defmodule Polyjuice.Client.Endpoint.PostLogin do } end - def transform_http_result(_req, status_code, _resp_headers, body) do - case status_code do - 200 -> - {:ok, Poison.decode!(body)} - - _ -> - {:error, status_code, body} - end + def transform_http_result(req, status_code, resp_headers, body) do + Polyjuice.Client.Endpoint.parse_response(req, status_code, resp_headers, body) end end end diff --git a/lib/polyjuice/client/endpoint/post_logout.ex b/lib/polyjuice/client/endpoint/post_logout.ex index e29c28b..d0e4e1a 100644 --- a/lib/polyjuice/client/endpoint/post_logout.ex +++ b/lib/polyjuice/client/endpoint/post_logout.ex @@ -45,14 +45,14 @@ defmodule Polyjuice.Client.Endpoint.PostLogout do } end - def transform_http_result(_req, status_code, _resp_headers, body) do - case status_code do - 200 -> - {:ok} + def transform_http_result(req, status_code, resp_headers, body) do + Polyjuice.Client.Endpoint.parse_response(req, status_code, resp_headers, body) + end + end - _ -> - {:error, status_code, body} - end + defimpl Polyjuice.Client.Endpoint.BodyParser do + def parse(_req, _body) do + {:ok} end end end diff --git a/lib/polyjuice/client/endpoint/post_rooms_receipt.ex b/lib/polyjuice/client/endpoint/post_rooms_receipt.ex index 45cff8e..5b92803 100644 --- a/lib/polyjuice/client/endpoint/post_rooms_receipt.ex +++ b/lib/polyjuice/client/endpoint/post_rooms_receipt.ex @@ -63,14 +63,14 @@ defmodule Polyjuice.Client.Endpoint.PostRoomsReceipt do } end - def transform_http_result(_req, status_code, _resp_headers, body) do - case status_code do - 200 -> - {:ok} + def transform_http_result(req, status_code, resp_headers, body) do + Polyjuice.Client.Endpoint.parse_response(req, status_code, resp_headers, body) + end + end - _ -> - {:error, status_code, body} - end + defimpl Polyjuice.Client.Endpoint.BodyParser do + def parse(_req, _body) do + {:ok} end end end diff --git a/lib/polyjuice/client/endpoint/post_user_filter.ex b/lib/polyjuice/client/endpoint/post_user_filter.ex index 39b0f4a..cf57437 100644 --- a/lib/polyjuice/client/endpoint/post_user_filter.ex +++ b/lib/polyjuice/client/endpoint/post_user_filter.ex @@ -57,14 +57,14 @@ defmodule Polyjuice.Client.Endpoint.PostUserFilter do } end - def transform_http_result(_req, status_code, _resp_headers, body) do - case status_code do - 200 -> - {:ok, body |> Poison.decode!() |> Map.get("filter_id")} + def transform_http_result(req, status_code, resp_headers, body) do + Polyjuice.Client.Endpoint.parse_response(req, status_code, resp_headers, body) + end + end - _ -> - {:error, status_code, body} - end + defimpl Polyjuice.Client.Endpoint.BodyParser do + def parse(_req, parsed) do + {:ok, Map.get(parsed, "filter_id")} end end end diff --git a/lib/polyjuice/client/endpoint/put_rooms_send.ex b/lib/polyjuice/client/endpoint/put_rooms_send.ex index 717624d..549bc2b 100644 --- a/lib/polyjuice/client/endpoint/put_rooms_send.ex +++ b/lib/polyjuice/client/endpoint/put_rooms_send.ex @@ -65,14 +65,14 @@ defmodule Polyjuice.Client.Endpoint.PutRoomsSend do } end - def transform_http_result(_req, status_code, _resp_headers, body) do - case status_code do - 200 -> - {:ok, body |> Poison.decode!() |> Map.get("event_id")} + def transform_http_result(req, status_code, resp_headers, body) do + Polyjuice.Client.Endpoint.parse_response(req, status_code, resp_headers, body) + end + end - _ -> - {:error, status_code, body} - end + defimpl Polyjuice.Client.Endpoint.BodyParser do + def parse(_req, parsed) do + {:ok, Map.get(parsed, "event_id")} end end end diff --git a/lib/polyjuice/client/endpoint/put_rooms_state.ex b/lib/polyjuice/client/endpoint/put_rooms_state.ex index 98481ed..51e9d64 100644 --- a/lib/polyjuice/client/endpoint/put_rooms_state.ex +++ b/lib/polyjuice/client/endpoint/put_rooms_state.ex @@ -65,14 +65,14 @@ defmodule Polyjuice.Client.Endpoint.PutRoomsState do } end - def transform_http_result(_req, status_code, _resp_headers, body) do - case status_code do - 200 -> - {:ok, body |> Poison.decode!() |> Map.get("event_id")} + def transform_http_result(req, status_code, resp_headers, body) do + Polyjuice.Client.Endpoint.parse_response(req, status_code, resp_headers, body) + end + end - _ -> - {:error, status_code, body} - end + defimpl Polyjuice.Client.Endpoint.BodyParser do + def parse(_req, parsed) do + {:ok, Map.get(parsed, "event_id")} end end end diff --git a/test/polyjuice/client/endpoint/get_rooms_messages_test.exs b/test/polyjuice/client/endpoint/get_rooms_messages_test.exs index f38a8ac..1b27f09 100644 --- a/test/polyjuice/client/endpoint/get_rooms_messages_test.exs +++ b/test/polyjuice/client/endpoint/get_rooms_messages_test.exs @@ -46,7 +46,9 @@ defmodule Polyjuice.Client.Endpoint.GetRoomsMessagesTest do 500, [], "Aaah!" - ) == {:error, 500, "Aaah!"} + ) == + {:error, 500, + %{"body" => "Aaah!", "errcode" => "CA_UHOREG_POLYJUICE_BAD_RESPONSE"}} end with endpoint = %Polyjuice.Client.Endpoint.GetRoomsMessages{ diff --git a/test/polyjuice/client/endpoint/get_sync_test.exs b/test/polyjuice/client/endpoint/get_sync_test.exs index babc0d3..939e0e2 100644 --- a/test/polyjuice/client/endpoint/get_sync_test.exs +++ b/test/polyjuice/client/endpoint/get_sync_test.exs @@ -41,7 +41,9 @@ defmodule Polyjuice.Client.Endpoint.GetSyncTest do 500, [], "Aaah!" - ) == {:error, 500, "Aaah!"} + ) == + {:error, 500, + %{"body" => "Aaah!", "errcode" => "CA_UHOREG_POLYJUICE_BAD_RESPONSE"}} end with endpoint = %Polyjuice.Client.Endpoint.GetSync{ diff --git a/test/polyjuice/client/endpoint/post_join_test.exs b/test/polyjuice/client/endpoint/post_join_test.exs index 969c74f..5d6ed65 100644 --- a/test/polyjuice/client/endpoint/post_join_test.exs +++ b/test/polyjuice/client/endpoint/post_join_test.exs @@ -67,7 +67,9 @@ defmodule Polyjuice.Client.Endpoint.PostJoinTest do 500, [], "Aaah!" - ) == {:error, 500, "Aaah!"} + ) == + {:error, 500, + %{"body" => "Aaah!", "errcode" => "CA_UHOREG_POLYJUICE_BAD_RESPONSE"}} end with endpoint = %Polyjuice.Client.Endpoint.PostJoin{ diff --git a/test/polyjuice/client/endpoint/post_login_test.exs b/test/polyjuice/client/endpoint/post_login_test.exs index 4e1e6c3..0255562 100644 --- a/test/polyjuice/client/endpoint/post_login_test.exs +++ b/test/polyjuice/client/endpoint/post_login_test.exs @@ -74,6 +74,7 @@ defmodule Polyjuice.Client.Endpoint.PostLoginTest do 500, [], "Aaah!" - ) == {:error, 500, "Aaah!"} + ) == + {:error, 500, %{"body" => "Aaah!", "errcode" => "CA_UHOREG_POLYJUICE_BAD_RESPONSE"}} end end diff --git a/test/polyjuice/client/endpoint/post_logout_test.exs b/test/polyjuice/client/endpoint/post_logout_test.exs index 185e415..b548d9f 100644 --- a/test/polyjuice/client/endpoint/post_logout_test.exs +++ b/test/polyjuice/client/endpoint/post_logout_test.exs @@ -43,6 +43,7 @@ defmodule Polyjuice.Client.Endpoint.PostLogoutTest do 500, [], "Aaah!" - ) == {:error, 500, "Aaah!"} + ) == + {:error, 500, %{"body" => "Aaah!", "errcode" => "CA_UHOREG_POLYJUICE_BAD_RESPONSE"}} end end diff --git a/test/polyjuice/client/endpoint/post_rooms_receipt_test.exs b/test/polyjuice/client/endpoint/post_rooms_receipt_test.exs index d97bc2c..cebe89f 100644 --- a/test/polyjuice/client/endpoint/post_rooms_receipt_test.exs +++ b/test/polyjuice/client/endpoint/post_rooms_receipt_test.exs @@ -47,6 +47,7 @@ defmodule Polyjuice.Client.Endpoint.PostRoomsReceiptTest do 500, [], "Aaah!" - ) == {:error, 500, "Aaah!"} + ) == + {:error, 500, %{"body" => "Aaah!", "errcode" => "CA_UHOREG_POLYJUICE_BAD_RESPONSE"}} end end diff --git a/test/polyjuice/client/endpoint/post_user_filter_test.exs b/test/polyjuice/client/endpoint/post_user_filter_test.exs index efff197..6d001cc 100644 --- a/test/polyjuice/client/endpoint/post_user_filter_test.exs +++ b/test/polyjuice/client/endpoint/post_user_filter_test.exs @@ -50,6 +50,7 @@ defmodule Polyjuice.Client.Endpoint.PostUserfilterTest do 500, [], "Aaah!" - ) == {:error, 500, "Aaah!"} + ) == + {:error, 500, %{"body" => "Aaah!", "errcode" => "CA_UHOREG_POLYJUICE_BAD_RESPONSE"}} end end diff --git a/test/polyjuice/client/endpoint/put_rooms_send_test.exs b/test/polyjuice/client/endpoint/put_rooms_send_test.exs index 904934c..a05018e 100644 --- a/test/polyjuice/client/endpoint/put_rooms_send_test.exs +++ b/test/polyjuice/client/endpoint/put_rooms_send_test.exs @@ -51,6 +51,7 @@ defmodule Polyjuice.Client.Endpoint.PutRoomsSendTest do 500, [], "Aaah!" - ) == {:error, 500, "Aaah!"} + ) == + {:error, 500, %{"body" => "Aaah!", "errcode" => "CA_UHOREG_POLYJUICE_BAD_RESPONSE"}} end end diff --git a/test/polyjuice/client/endpoint/put_rooms_state_test.exs b/test/polyjuice/client/endpoint/put_rooms_state_test.exs index 4be69d3..8f2f5aa 100644 --- a/test/polyjuice/client/endpoint/put_rooms_state_test.exs +++ b/test/polyjuice/client/endpoint/put_rooms_state_test.exs @@ -50,6 +50,7 @@ defmodule Polyjuice.Client.Endpoint.PutRoomsStateTest do 500, [], "Aaah!" - ) == {:error, 500, "Aaah!"} + ) == + {:error, 500, %{"body" => "Aaah!", "errcode" => "CA_UHOREG_POLYJUICE_BAD_RESPONSE"}} end end diff --git a/test/polyjuice/client/endpoint_test.exs b/test/polyjuice/client/endpoint_test.exs new file mode 100644 index 0000000..5c19a5f --- /dev/null +++ b/test/polyjuice/client/endpoint_test.exs @@ -0,0 +1,40 @@ +# Copyright 2020 Hubert Chathi <hubert@uhoreg.ca> +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +defmodule Polyjuice.Client.EndpointTest do + use ExUnit.Case + + test "parse response" do + # just test the error handling -- successful responses will be checked by + # the various endpoint tests + + # the server returned a Matrix error + assert Polyjuice.Client.Endpoint.parse_response( + %{}, + 404, + [], + ~s({"errcode": "M_NOT_FOUND", "error": "not found"}) + ) == {:error, 404, %{"errcode" => "M_NOT_FOUND", "error" => "not found"}} + + # the server claims to have succeeded, but the body is not JSON + assert Polyjuice.Client.Endpoint.parse_response(%{}, 200, [], "not JSON") == + {:error, 500, + %{"errcode" => "CA_UHOREG_POLYJUICE_BAD_RESPONSE", "body" => "not JSON"}} + + # the server returned a non-Matrix error + assert Polyjuice.Client.Endpoint.parse_response(%{}, 404, [], "Not found") == + {:error, 404, + %{"errcode" => "CA_UHOREG_POLYJUICE_BAD_RESPONSE", "body" => "Not found"}} + end +end |