<commit_before>import React from 'react'
import _get from 'lodash/get'
import {FormField} from 'tocco-ui'
import consoleLogger from '../consoleLogger'

export const formFieldFactory = (mapping, data, resources = {}) => {
  const {
    formDefinitionField,
    modelField,
    entityField,
    id,
    value,
    dirty,
    touched,
    onChange,
    events,
    error,
    utils,
    readOnly
  } = data

  const readOnlyFinally = (
    formDefinitionField.displayType === 'READONLY'
    || readOnly === true
    || !_get(entityField, 'value.writable', true)
  )

  const mandatory = _get(modelField, `validation.mandatory`, false)

  const valueField = valueFieldFactory(
    mapping,
    formDefinitionField,
    modelField,
    {
      id,
      value,
      onChange,
      readOnly: readOnlyFinally,
      mandatory
    },
    events,
    utils
  )

  return <FormField
    key={id}
    id={id}
    label={formDefinitionField.label}
    mandatory={mandatory}
    mandatoryTitle={resources.mandatoryTitle}
    error={error}
    touched={touched}
    dirty={dirty}
    useLabel={formDefinitionField.useLabel === 'YES'}
  >
    {valueField}
  </FormField>
}

const valueFieldFactory = (mapping, formField, modelField, props, events, utils) => {
  let typeFactory = mapping[formField.type]<commit_msg>refactor(tocco-util): add formField error handling<commit_after>import React from 'react'
import _get from 'lodash/get'
import {FormField} from 'tocco-ui'
import consoleLogger from '../consoleLogger'

export const formFieldFactory = (mapping, data, resources = {}) => {
  try {
    const {
      formDefinitionField,
      modelField,
      entityField,
      id,
      value,
      dirty,
      touched,
      onChange,
      events,
      error,
      utils,
      readOnly
    } = data

    const readOnlyFinally = (
      formDefinitionField.displayType === 'READONLY'
      || readOnly === true
      || !_get(entityField, 'value.writable', true)
    )

    const mandatory = _get(modelField, `validation.mandatory`, false)

    const valueField = valueFieldFactory(
      mapping,
      formDefinitionField,
      modelField,
      {
        id,
        value,
        onChange,
        readOnly: readOnlyFinally,
        mandatory
      },
      events,
      utils
    )

    return (
      <FormField
        key={id}
        id={id}
        label={formDefinitionField.label}
        mandatory={mandatory}
        mandatoryTitle={resources.mandatoryTitle}
        error={error}
        touched={touched}
        dirty={dirty}
        useLabel={formDefinitionField.useLabel === 'YES'}
      >
        {valueField}
      </FormField>
    )
  } catch (exception) {
    consoleLogger.logError('Error creating formField', exception)
    return <span/>
  }
}

const valueFieldFactory = (mapping, formField, modelField, props, events, utils) => {
  let typeFactory = mapping[formField.type]
{'fraction_non_alphanumeric': 0.10953697304768487, 'fraction_numerical': 0.0, 'mean_word_length': 2.6506935687263558, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 12, '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': '5992530', 'n_tokens_mistral': 861, 'n_tokens_neox': 857, 'n_words': 190}