How to return custom errors in a after_transaction call?
Let's say I have a change with the following implementation:
As you can see, my change just ignores everything and returns
{:error, :something}
. In other words, I want to create a change that in some conditions, I want to return an error with some custom data.
The problem is that instead of Ash returning {:error, :something}
directly, it returns:
So it actually converts :something
into a string and returns it as a UnknownError
.
Is there some way to actually return :something
directly or create a custom error that will have it as one of the fields?6 Replies
Yes, if you create a custom Ash error with
def_ash_error
All of the ash errors are built this way, so you could copy this one for example: https://github.com/ash-project/ash/blob/main/lib/ash/error/changes/invalid_attribute.ex
GitHub
ash/lib/ash/error/changes/invalid_attribute.ex at main · ash-projec...
A declarative and extensible framework for building Elixir applications. - ash/lib/ash/error/changes/invalid_attribute.ex at main · ash-project/ash
If what you return is an ash error, then it won't be wrapped in an unknown error
Thanks @Zach Daniel , so, doing your suggestion I came up with this error:
This seems to work great, but the error always arrive inside a
Ash.Error.Unknown
error in the errors
field.
Is there some way to make my custom error the top error returned?Oh, no you cant
All errors will always come back in an error class
I.e “unknown” or “invalid” or “forbidden”
Those will always be the top level errors
You can read more about it in the errors guide in the docs
Got it, thanks!