Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return resource.errors.full_messages in addition to resource.errors #44

Closed
jasonswett opened this issue Oct 14, 2014 · 19 comments
Closed

Comments

@jasonswett
Copy link
Contributor

In RegistrationsController#create, might it be a good idea to return resource.errors.full_messages instead of resource.errors? The full_messages version seems to me to be easier to consume on the client side.

One reservation I do have is that maybe devise_token_auth isn't always used in client-server-architected apps. "Traditional" apps would of course still probably want resource.errors as opposed to resource.errors.full_messages. So if we were to make a change, we would probably only want to make it for JSON requests.

I forked the project and changed resource.errors to resource.errors.full_messages and it works well for me.

What do you think?

@lynndylanhurley
Copy link
Owner

I think the current error-handling system is mostly haphazard. I've been wanting to formalize the errors, but I keep getting distracted. I'll make this a priority for the next release.

I do have some ideas on this, I'll post back to this issue ASAP.

@jasonswett
Copy link
Contributor Author

I'm willing to help in whatever way makes sense. Let me know if you think any of that work is delegable to me.

@lynndylanhurley
Copy link
Owner

Sorry for the delay. I think the best way to handle this will be to return the full error object, including messages and full_messages.

Just to be clear:

  • messages look like this: {:password_confirmation=>["doesn't match Password"]}
  • full_messages look like this: ["Password confirmation doesn't match Password"]

The messages hash is useful for matching by field name, so the error can be easily appended above the input that is causing the issue.

For example:

<fieldset ng-class="{error: errors.full_messages.length}">
  <div>
    <label>Email</label>
    <p class="errors" ng-show="errors.messages['email']">Email {{ errors.messages['email'] }}</p>
    <input type="text" name="email" ng-model="form.email" />
  </div>
</fieldset>

The full_messages array is useful for throwing alerts/modals, or showing an error summary at the top of forms.

So I think it's probably best to standardize all the errors so that they look like this:

{
  data: {
    errors: {
      messages: {
        // messages hash contents
      },
      full_messages: [
        // full_messages array contents
      ]
    }
  }
}

Do you agree? Is this something that you would like to take on?

@jasonswett
Copy link
Contributor Author

Totally agree, and yes.

Is there any certain process I should be following? Create a branch, implement the feature, issue pull request?

@lynndylanhurley
Copy link
Owner

I would prefer this process:

  1. Create feature branch
  2. Write test cases
  3. Make tests pass
  4. Issue pull request

You can run the test suite using the following steps:

  1. clone this repo
  2. bundle install
  3. rake db:migrate
  4. RAILS_ENV=test rake db:migrate
  5. guard

This will open the guard test-runner. Guard will re-run each test suite when changes are made to its corresponding files.

I should probably add a CONTRIBUTING.md or something :)

@jasonswett
Copy link
Contributor Author

Okay, I'm with you. I was including steps 2 and 3 in "implement the feature", I just didn't explicitly say it. And I already cloned your repo and ran your tests a long time ago. :)

I'm actually on vacation right now but let me take a closer look at this later in the week and give you a rough feel for timeline. Thanks for the opportunity to help.

@jasonswett
Copy link
Contributor Author

I checked my schedule: I can get this in place by the end of next week (the week ending 10/24).

@lynndylanhurley
Copy link
Owner

Awesome, thanks @jasonswett! Let me know if you run into any trouble.

@jasonswett jasonswett changed the title Use resource.errors.full_messages instead of resource.errors Return resource.errors.full_messages in addition to resource.errors Oct 23, 2014
@jasonswett
Copy link
Contributor Author

Hmm, after getting into the implementation a little bit I'm realizing that splitting

{
  data: {
    errors: [
      // whatever
    ]
  }
}

into

{
  data: {
    errors: {
      messages: {
        // messages hash contents
      },
      full_messages: [
        // full_messages array contents
      ]
    }
  }
}

might carry some unwanted baggage. The issues to me are:

  • It seems to me not-backwards-compatible. People would have to know to change any instance of response.errors to response.errors.messages.
  • response.errors.messages seems kind of non-standard compared to response.errors
  • There are a number of places in the controller (I counted 6) that would have to change in order to match this, and that might result in some awkwardness

What would you think about this structure instead?

{
  data: {
    errors: {
      // messages hash contents
      full_messages: [
        // full_messages array contents
      ]
    },
  }
}

@lynndylanhurley
Copy link
Owner

@jasonswett - I agree! Good work 👍

@jasonswett
Copy link
Contributor Author

Okay, the change is in place. I'm new to open source contribution so I don't know if I did all the steps right. Here's a link to my pull request: #50

@lynndylanhurley
Copy link
Owner

Cool, thanks @jasonswett! I'll review ASAP.

@lynndylanhurley
Copy link
Owner

Merged! Check out:

  • ng-token-auth 0.0.24-beta2
  • devise_token_auth 0.1.30.beta1

Please test and confirm that everything works!

@jasonswett
Copy link
Contributor Author

Will do. Thanks!

@lynndylanhurley
Copy link
Owner

@jasonswett - you may want to hold off testing until I fix a bug that was introduced regarding #51. I'll let you know when everything has stabilized.

@jasonswett
Copy link
Contributor Author

10-4

@lynndylanhurley
Copy link
Owner

Ok @jasonswett - the problem should be fixed in 0.1.30.beta3.

@lynndylanhurley
Copy link
Owner

@jasonswett - this seems to be working ok. I'm closing this, but please re-open if you find any issues.

Thanks again for the PR 😺

@jasonswett
Copy link
Contributor Author

@lynndylanhurley You're welcome for the PR! Sorry for my slowness here - been a bit underwater lately.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants