<commit_before>import {sequence} from 'cerebral'
import {equals, set} from 'cerebral/operators'
import {props} from 'cerebral/tags'
import showFlash from './showFlash'

function getSchemaValidationErrorMessages ({props}) {
  const errorMessages = Object.keys(props.result).reduce(
    (errorMessages, errorField) => {
      if (Array.isArray(props.result[errorField])) {
        errorMessages.push(
          errorField + ': ' + props.result[errorField].join(', ')
        )
      }
      return errorMessages
    }, []
  )
  return {errorMessages: errorMessages.join('\n')}
}

function showValidationError (defaultErrorMessage) {
  return sequence('Show validation error', [
    equals(props`status`), {
      403: set(props`errorMessages`, props`result.validationError`),
      409: set(props`errorMessages`, props`result.validationError`),
      422: getSchemaValidationErrorMessages,
      otherwise: set(props`errorMessages`, defaultErrorMessage)
    },
    showFlash(props`errorMessages`, 'warning')
  ])
}

export default showValidationError<commit_msg>fix(client): update to new http error API<commit_after>import {sequence} from 'cerebral'
import {equals, set} from 'cerebral/operators'
import {props} from 'cerebral/tags'
import showFlash from './showFlash'

function getSchemaValidationErrorMessages ({props}) {
  const errorMessages = Object.keys(props.error.body).reduce(
    (errorMessages, errorField) => {
      if (Array.isArray(props.error.body[errorField])) {
        errorMessages.push(
          errorField + ': ' + props.error.body[errorField].join(', ')
        )
      }
      return errorMessages
    }, []
  )
  return {errorMessages: errorMessages.join('\n')}
}

function showValidationError (defaultErrorMessage) {
  return sequence('Show validation error', [
    equals(props`error.status`), {
      403: set(props`errorMessages`, props`error.body.validationError`),
      409: set(props`errorMessages`, props`error.body.validationError`),
      422: getSchemaValidationErrorMessages,
      otherwise: set(props`errorMessages`, defaultErrorMessage)
    },
    showFlash(props`errorMessages`, 'warning')
  ])
}

export default showValidationError
{'fraction_non_alphanumeric': 0.12925482980680772, 'fraction_numerical': 0.00827966881324747, 'mean_word_length': 4.343980343980344, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '4665571', 'n_tokens_mistral': 638, 'n_tokens_neox': 615, 'n_words': 123}