<commit_before>import Ember from 'ember';

const { get, isPresent, isArray } = Ember;
// http://tools.ietf.org/html/rfc6749#section-5.2
const lookupTable = {
  'invalid_grant': 'The provided credentials are not valid.'
};

// @Cleanup: Might be worth only showing a single error, rather than all. -vevix
export default function errorMessages(obj) {
  let reason = 'An unknown error occurred';
  let errors = get(obj, 'errors') || get(obj, 'error');
  errors = errors === undefined ? get(obj, 'jqXHR.responseJSON.errors') : errors;
  if (isPresent(errors)) {
    reason = isArray(errors) ? errors.mapBy('title').compact().join('\n') : get(lookupTable, errors);
  }
  return reason;
}<commit_msg>client: update error-messages util to show the property name as well<commit_after>import Ember from 'ember';

const { get, isPresent, isArray } = Ember;
// http://tools.ietf.org/html/rfc6749#section-5.2
const lookupTable = {
  'invalid_grant': 'The provided credentials are not valid.'
};

function formatErrors(errors) {
  errors = errors.map((error) => {
    const param = get(error, 'source.parameter');
    const message = param === undefined ? get(error, 'title') : `${param} ${get(error, 'title')}`;
    return message;
  });
  return errors.compact().join('\n');
}

// @Cleanup: Might be worth only showing a single error, rather than all. -vevix
export default function errorMessages(obj) {
  let reason = 'An unknown error occurred';
  let errors = get(obj, 'errors') || get(obj, 'error');
  errors = errors === undefined ? get(obj, 'jqXHR.responseJSON.errors') : errors;
  if (isPresent(errors)) {
    reason = isArray(errors) ? formatErrors(errors) : get(lookupTable, errors);
  }
  return reason;
}
{'fraction_non_alphanumeric': 0.15434272300469484, 'fraction_numerical': 0.007042253521126761, 'mean_word_length': 4.429936305732484, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7038938', 'n_tokens_mistral': 538, 'n_tokens_neox': 530, 'n_words': 157}