kipcole9 / money_sql

Money functions for the serialization of a money data type in Elixir
Other
28 stars 18 forks source link

Add keyword list to error return from money_with_currency cast/1 #20

Closed kipcole9 closed 2 years ago

kipcole9 commented 2 years ago

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?

kipcole9 commented 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()}
kipcole9 commented 2 years ago

Published ex_money_sql version 1.6.0 with the following changelog entry:

Enhancements

iex> Money.Ecto.Composite.Type.cast("") ==
{:error,
 [
   exception: Money.InvalidAmountError,
   message: "Amount cannot be converted to a number: \"\""
 ]}

The expected exceptions are:

Thanks to @DaTrader for the enhancement request.

DaTrader commented 2 years ago

Fabulous!