Huxelpux

Huxelpux

I want to show a localized error message when a postgres database constraint for a relationship is violated (especially, during the destroy action of a parent record that still has child records that belong to it).

The documentation for the violation_message suggests that it shall be used for that purpose. It is “A message to show if there is a conflict with this relationship in the database on destroy.”

However, if I specify a violation_message for the belongs_to relationship, upon trying to destroy the record I only get an InvalidAttribute error on field :id with the standard message “would leave records behind”. Having an inverse has_many relationship with a violation_message does not change this, either.

So how can I specify a relationship-specific, localized violation_message (using gettext) that is then used in the error (or elsewhere)? Or are error messages not intended to be localized at all? How to specify and use relationship-specific, localized error messages for an application, then?

Showing Posts 1 to 5

zachdaniel

zachdaniel

Creator of Ash

Localization happens at the “edges”. So you would do it in your live views, or in an error handler for your GraphQL etc.

With that said, if the violation_message for a relationship is not being used then that is a bug. Could you create a reproduction and open an issue?

Huxelpux

Huxelpux OP

Thanks for the lightspeed response. I’ll try to create a reproduction (might take some time …)

Your statement that localization happens at the “edges” seems very reasonable. However, I’m not sure how it should be done in this case.

Suppose that I want to have a generic delete functionality in my application. If somebody tries to delete a parent record (say, a Post) with some child records (Comments) belonging to it, the message “Deleting this post would leave comments behind.” should be shown to the user.

Of course, this message should only be shown if there is a belongs_to relationship with the corresponding foreign key constraint specified in the domain model.

Now, how do I construct the message (and localize it) if somebody tries to delete a Post with Comments still attached? I only get an InvalidAttribute error with field :id, the message “would leave records behind” and private_vars (which I’m probably not supposed to use - they are private, after all). If I dare to use the private_vars, I see that a :foreign constraint is involved, and I get the constraint_name, which could be anything.

Should I resort to Introspection to get the necessary information to construct my error message? That seems to be rather awkward. Shouldn’t the error contain the information about the violation of a constraint in the domain, so that I can use it at the “edges”?

zachdaniel

zachdaniel

Creator of Ash

Right, so it makes perfect sense to construct error messages internally that allow you to uniquely identify errors, but the actual localization of that would happen at the edges.
With that said, adding more information like the resource, relationship etc. to errors would only be a positive thing. Something we have to incrementally improve.

zachdaniel

zachdaniel

Creator of Ash

Any things like that, even small things, are worth issues on GH. Even if its just “improve X error information to include resource & relationship”.

Huxelpux

Huxelpux OP

OK - thank you again. I’ll see what I can do regarding GH issues. :slight_smile:

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
apz
I’m new to elixir and just tried to install the elixirLS extension for VScode(ium) and it is throwing some errors that I would like help ...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews