@@ -13,6 +13,37 @@ defmodule MishkaDeveloperTools.Helper.UUID do
13
13
@ type t :: << _ :: 288 >>
14
14
@ type raw :: << _ :: 128 >>
15
15
16
+ defmodule HelperEcto.CastError do
17
+ @ moduledoc """
18
+ Raised when a changeset can't cast a value.
19
+ """
20
+ defexception [ :message , :type , :value ]
21
+
22
+ def exception ( opts ) do
23
+ type = Keyword . fetch! ( opts , :type )
24
+ value = Keyword . fetch! ( opts , :value )
25
+ msg = opts [ :message ] || "cannot cast #{ inspect ( value ) } to #{ format ( type ) } "
26
+ % __MODULE__ { message: msg , type: type , value: value }
27
+ end
28
+
29
+ @ doc """
30
+ Format type for error messaging and logs.
31
+ """
32
+ def format ( { composite , type } ) when composite in [ :array , :map ] do
33
+ "{#{ inspect ( composite ) } , #{ format ( type ) } }"
34
+ end
35
+
36
+ def format ( { :parameterized , type , params } ) do
37
+ if function_exported? ( type , :format , 1 ) do
38
+ apply ( type , :format , [ params ] )
39
+ else
40
+ "##{ inspect ( type ) } <#{ inspect ( params ) } >"
41
+ end
42
+ end
43
+
44
+ def format ( type ) , do: inspect ( type )
45
+ end
46
+
16
47
@ spec cast ( t | raw | any ) :: { :ok , t } | :error
17
48
def cast ( uuid )
18
49
@@ -36,7 +67,7 @@ defmodule MishkaDeveloperTools.Helper.UUID do
36
67
def cast! ( uuid ) do
37
68
case cast ( uuid ) do
38
69
{ :ok , hex_uuid } -> hex_uuid
39
- :error -> raise Ecto .CastError, type: __MODULE__ , value: uuid
70
+ :error -> raise HelperEcto .CastError, type: __MODULE__ , value: uuid
40
71
end
41
72
end
42
73
0 commit comments