From b5a0dec4f2e0528f99ff1fde9e207c0c992af3bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eric=20Meadows-J=C3=B6nsson?= Date: Wed, 15 May 2024 11:43:06 +0200 Subject: [PATCH] Return 400 for UTF-8 encoding errors This is usually an input error from the user and should return a 400 error. --- lib/phoenix_ecto/plug.ex | 11 +++++++++++ mix.exs | 1 + mix.lock | 2 ++ 3 files changed, 14 insertions(+) diff --git a/lib/phoenix_ecto/plug.ex b/lib/phoenix_ecto/plug.ex index 9f1dcdd..daff2c1 100644 --- a/lib/phoenix_ecto/plug.ex +++ b/lib/phoenix_ecto/plug.ex @@ -57,3 +57,14 @@ unless Phoenix.Ecto.StorageNotCreatedError in excluded_exceptions do def storage_up(repo), do: repo.__adapter__().storage_up(repo.config()) end end + +if Code.ensure_loaded?(Postgrex.Error) do + unless Postgrex.Error in excluded_exceptions do + defimpl Plug.Exception, for: Postgrex.Error do + def status(%{postgres: %{code: :character_not_in_repertoire}}), do: 400 + def status(_), do: 500 + + def actions(_), do: [] + end + end +end diff --git a/mix.exs b/mix.exs index 268e9ee..40fea87 100644 --- a/mix.exs +++ b/mix.exs @@ -54,6 +54,7 @@ defmodule PhoenixEcto.Mixfile do {:phoenix_html, "~> 2.14.2 or ~> 3.0 or ~> 4.1", optional: true}, {:ecto, "~> 3.5"}, {:plug, "~> 1.9"}, + {:postgrex, "~> 0.17", optional: true}, {:ex_doc, ">= 0.0.0", only: :docs} ] end diff --git a/mix.lock b/mix.lock index be17eb6..b8d546b 100644 --- a/mix.lock +++ b/mix.lock @@ -1,4 +1,5 @@ %{ + "db_connection": {:hex, :db_connection, "2.6.0", "77d835c472b5b67fc4f29556dee74bf511bbafecdcaf98c27d27fa5918152086", [:mix], [{:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "c2f992d15725e721ec7fbc1189d4ecdb8afef76648c746a8e1cad35e3b8a35f3"}, "decimal": {:hex, :decimal, "2.1.1", "5611dca5d4b2c3dd497dec8f68751f1f1a54755e8ed2a966c2633cf885973ad6", [:mix], [], "hexpm", "53cfe5f497ed0e7771ae1a475575603d77425099ba5faef9394932b35020ffcc"}, "earmark_parser": {:hex, :earmark_parser, "1.4.39", "424642f8335b05bb9eb611aa1564c148a8ee35c9c8a8bba6e129d51a3e3c6769", [:mix], [], "hexpm", "06553a88d1f1846da9ef066b87b57c6f605552cfbe40d20bd8d59cc6bde41944"}, "ecto": {:hex, :ecto, "3.10.3", "eb2ae2eecd210b4eb8bece1217b297ad4ff824b4384c0e3fdd28aaf96edd6135", [:mix], [{:decimal, "~> 1.6 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "44bec74e2364d491d70f7e42cd0d690922659d329f6465e89feb8a34e8cd3433"}, @@ -11,5 +12,6 @@ "phoenix_html": {:hex, :phoenix_html, "3.3.1", "4788757e804a30baac6b3fc9695bf5562465dd3f1da8eb8460ad5b404d9a2178", [:mix], [{:plug, "~> 1.5", [hex: :plug, repo: "hexpm", optional: true]}], "hexpm", "bed1906edd4906a15fd7b412b85b05e521e1f67c9a85418c55999277e553d0d3"}, "plug": {:hex, :plug, "1.14.2", "cff7d4ec45b4ae176a227acd94a7ab536d9b37b942c8e8fa6dfc0fff98ff4d80", [:mix], [{:mime, "~> 1.0 or ~> 2.0", [hex: :mime, repo: "hexpm", optional: false]}, {:plug_crypto, "~> 1.1.1 or ~> 1.2", [hex: :plug_crypto, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4.3 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "842fc50187e13cf4ac3b253d47d9474ed6c296a8732752835ce4a86acdf68d13"}, "plug_crypto": {:hex, :plug_crypto, "1.2.5", "918772575e48e81e455818229bf719d4ab4181fcbf7f85b68a35620f78d89ced", [:mix], [], "hexpm", "26549a1d6345e2172eb1c233866756ae44a9609bd33ee6f99147ab3fd87fd842"}, + "postgrex": {:hex, :postgrex, "0.17.5", "0483d054938a8dc069b21bdd636bf56c487404c241ce6c319c1f43588246b281", [:mix], [{:db_connection, "~> 2.1", [hex: :db_connection, repo: "hexpm", optional: false]}, {:decimal, "~> 1.5 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:table, "~> 0.1.0", [hex: :table, repo: "hexpm", optional: true]}], "hexpm", "50b8b11afbb2c4095a3ba675b4f055c416d0f3d7de6633a595fc131a828a67eb"}, "telemetry": {:hex, :telemetry, "1.2.1", "68fdfe8d8f05a8428483a97d7aab2f268aaff24b49e0f599faa091f1d4e7f61c", [:rebar3], [], "hexpm", "dad9ce9d8effc621708f99eac538ef1cbe05d6a874dd741de2e689c47feafed5"}, }