spree/spree — pull request #5592 by HoyaBoya (2014-11-10 22:10:41+00:00)
Title: Food For Thought - Introduce Active Model Better Errors to API

I'm kinda of half expecting this PR to be rejected but here we go....

Right now, API errors are really hard to digest from a client perspective because we are forced to bind to translation messages. This was first encountered with apply_promo_codes and addressed by storing the translation code on the model:

https://github.com/spree/spree/commit/feb143b0477d2850facd59044c43d0e01276bb37

I want to propose a more global change to start using the Spree translation symbols as error codes along with the existing Active Model error messages. This is done via the active_model-better_errors gem. I thought this was a better approach than monkeying ActiveModel::Errors, and based on my understanding of the code, there's no other way where it keeps track of the translation symbols once a message is added.

https://github.com/rails/rails/blob/master/activemodel/lib/active_model/errors.rb#L299-L307

So we introduce this new gem, and then we display error_codes in the various RABL locations where we want API friendly error messages vs human friendly error messages.

I'm open to ideas here, but the current API feels a tad brittle, especially as I try to handle inventory insufficient issues during our API driven checkout process.