Closed kipcole9 closed 2 years ago
The Ecto type behaviour allows for providing additional information although I'm not sure what Ecto version that was added. I'll work on an implementation.
@callback cast(term) :: {:ok, term} | :error | {:error, keyword()}
Published ex_money_sql version 1.6.0 with the following changelog entry:
t:Money.Ecto.Composite.Type
and t:Money.Ecto.Map.Type
now return the exception module when there is an error in cast/1
. For example:iex> Money.Ecto.Composite.Type.cast("") ==
{:error,
[
exception: Money.InvalidAmountError,
message: "Amount cannot be converted to a number: \"\""
]}
The expected exceptions are:
Money.InvalidAmountError
Money.UnknownCurrencyError
Money.ParseError
Thanks to @DaTrader for the enhancement request.
Fabulous!
I have a problem with the parsing error messages being of variable and potentially sizeable length (the error messages are citing the input value). Also, I have a problem when the user starts typing the amount by first stating the currency so for as long as there is no number next to it, the error shows: Amount cannot be converted to a number: "".
I need to replace the former with a generic, fixed length message such as "Invalid amount", and the latter with no error message at all. As I now see it, the only way to do this is to intercept these in the LiveView generated MyApp.ErrorHelpers.error_tag/2 function, which is fine for me. But, the amount of information I get there is not enough. The details provided are the Money type tuple and the validation: :cast. as show below:
{"Amount cannot be converted to a number: \"\"", [type: {:parameterized, Money.Ecto.Composite.Type, []}, validation: :cast]} Can you please also provide the error module atom itself so I can tell the errors one from another?