Skip to content

Commit dd59217

Browse files
authored
Handle unknown HTTP status response (#19)
As some responses don't have the matching status against the OpenAPI spec, we need to set a default value to get the exact error message instead of stacktrace like below: ** (MatchError) no match of right hand side value: nil (open_api_typesense 0.6.1) lib/open_api_typesense/client.ex:172: OpenApiTypesense.Client.parse_resp/2
1 parent fe3d03c commit dd59217

1 file changed

Lines changed: 10 additions & 7 deletions

File tree

lib/open_api_typesense/client.ex

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ defmodule OpenApiTypesense.Client do
1717
{:ok, any()}
1818
| {:error, ApiResponse.t()}
1919
| {:error, String.t()}
20+
| {:error, list()}
2021
| :error
2122

2223
@doc since: "0.2.0"
@@ -166,14 +167,16 @@ defmodule OpenApiTypesense.Client do
166167
# end
167168
# end
168169

169-
defp parse_resp(%Req.Response{} = resp, opts_resp) do
170-
{code, values} =
171-
opts_resp
172-
|> Enum.find(fn {code, _values} ->
173-
code === resp.status
174-
end)
170+
# Some resources are missing 4xx descriptions, hence we will set a default
171+
# instead so we can see the actual error message instead of stacktrace.
172+
# See https://github.com/typesense/typesense-api-spec/pull/84
173+
defp parse_resp(%Req.Response{} = resp, status_type) do
174+
{status, type} =
175+
status_type
176+
|> Enum.find(fn {status, _type} -> status == resp.status end) ||
177+
{resp.status, :map}
175178

176-
parse_values(code, values, resp.body)
179+
parse_values(status, type, resp.body)
177180
end
178181

179182
defp parse_resp(error, _opts_resp) do

0 commit comments

Comments
 (0)