elixir-plug / plug

Compose web applications with functions
https://hex.pm/packages/plug
Other
2.81k stars 578 forks source link

Add parameter for function on how to handle invalid strings when validating for UTF-8 #1159 #1202

Closed HansGlimmerfors closed 6 months ago

HansGlimmerfors commented 6 months ago

#1159 asked for the possibility for more custom behavior when failing validations of UTF-8 strings. This PR adds a new function Plug.Conn.Utils.validate_utf8/3 as an alternative to Plug.Conn.Utils.validate_utf8!/3. The new function has a parameter for a function on how to handle a failure (with the error message supplied as input to the function). The changes are backward compatible, but updating Plug.Conn.Query to use the new function makes the signature of Plug.Conn.Query.decode/5 a bit verbose and clunky. When the try-rescue will be removed from Plug.Conn.Query.decode_www_pair/4, there will no longer be any need for both a parameter for an exception and a function, but it would be a breaking change in the future to remove the exception parameter in favor of only the function.

josevalim commented 6 months ago

Thank you @HansGlimmerfors! There is a simpler approach, so I went for it in 5f244f4412f6d0dabe83cb25f5b6a8a7d369d2a0, thank you!