“It also sends a clear message: this is the place where one should put their code!”
I have to agree with Stan (the previous commenter).
A schema file has a one-to-one relationship with a table that you insert into, so to me calling Song.insert() or Album.update() make a lot more sense than Audio.create_song().
Specifically, it doesn’t feel like it makes sense to me to have changesets in the context files, since a changeset must use only the fields from a schema. Using non-schema fields in that changeset will generate an Ecto Changeset error. Example: I have a registration context that will insert a user record, and I have a form that has a “I agree to the terms and conditions” checkbox, but my user table doesn’t store that boolean flag for some reason. I can’t use a user changeset utilized by the form that supports that checkbox because it will generate an Ecto Changeset error. I can, however, create a registration changeset that isn’t related to any schema then once I validate that the form passed validate, I can transform to a user changeset. So if I need to transform that form changeset before I can Repo.insert, what does that transformed changeset really represent? A user. And where should a user changeset that can only cast against user fields belong? On the user schema.
“Errors are designed to be handled by a special
That doesn’t seem like a positive to me. I think all errors should be custom handled by the engineers and should generate specific responses to improve the user experience. Otherwise where the failure happened is ambiguous to the end user, which can be confusing and frustrating.
I am trying to upload a CSV of records, then processing will happen on the records. If it 500s and I see “Internal Server Error”, I have to click back, then click refresh to see if it failed on the upload itself or if it failed on the processing (assuming that happened on the same process).
Now if I try uploading a CSV, and I land back on the upload page with an error flash message saying “Your upload was completed, but processing failed because of an issue on our end, but don’t worry, we notified our engineers and they will fix it shortly and re-process your CSV.” I probably have a bit more confidence in that team that put that flash message together versus the team that fell back to “Internal Server Error”.