# Buggy code
def template_settings(
    es_version, ecs_version, mappings_section, template_settings_file
):
    if template_settings_file:
        with open(template_settings_file) as f:
            template = json.load(f)
    else:
        template = default_template_settings(ecs_version)

    if es_version == 6:
        mappings_section = copy.deepcopy(mappings_section)
        es6_type_fallback(mappings_section["properties"])

        # error.stack_trace needs special handling to set
        # index: false and doc_values: false
        error_stack_trace_mappings = mappings_section["properties"]["error"][
            "properties"
        ]["stack_trace"]
        error_stack_trace_mappings.setdefault("index", False)
        error_stack_trace_mappings.setdefault("doc_values", False)

        template["mappings"] = {"_doc": mappings_section}
    else:
        template["mappings"] = mappings_section

    # _meta can't be at template root in legacy templates, so moving back to mappings section
    mappings_section["_meta"] = template.pop("_meta")

    return template


# Traceback (KeyError)
Loading schemas from git ref v1.6.0
Running generator. ECS version 1.6.0
Loading user defined schemas: ['usage-example/fields/custom/']
/Users/florent/Sources/ecs/scripts/schema/cleaner.py:185: UserWarning: Example value for field `header_flags` contains an object or array which must be quoted to avoid YAML interpretation.

This will cause an exception when running in strict mode.
Warning check:
check_example_value(field, strict=strict_mode)
/Users/florent/Sources/ecs/scripts/schema/cleaner.py:185: UserWarning: Example value for field `resolved_ip` contains an object or array which must be quoted to avoid YAML interpretation.
[...]
Traceback (most recent call last):
File "/Users/florent/Sources/ecs/scripts/generator.py", line 106, in <module>
main()
File "/Users/florent/Sources/ecs/scripts/generator.py", line 60, in main
es_template.generate_legacy(flat, ecs_version, out_dir, args.template_settings, args.mapping_settings)
File "/Users/florent/Sources/ecs/scripts/generators/es_template.py", line 109, in generate_legacy
generate_legacy_template_version(6, ecs_version, mappings_section, out_dir, template_settings_file)
File "/Users/florent/Sources/ecs/scripts/generators/es_template.py", line 115, in generate_legacy_template_version
template = template_settings(es_version, ecs_version, mappings_section, template_settings_file)
File "/Users/florent/Sources/ecs/scripts/generators/es_template.py", line 202, in template_settings
error_stack_trace_mappings = mappings_section['properties']['error']['properties']['stack_trace']
KeyError: 'error'

# Fixed code
def template_settings(
    es_version, ecs_version, mappings_section, template_settings_file
):
    if template_settings_file:
        with open(template_settings_file) as f:
            template = json.load(f)
    else:
        template = default_template_settings(ecs_version)

    if es_version == 6:
        mappings_section = copy.deepcopy(mappings_section)
        es6_type_fallback(mappings_section["properties"])

        # error.stack_trace needs special handling to set
        # index: false and doc_values: false if the field
        # is present in the mappings
        try:
            error_stack_trace_mappings = mappings_section["properties"]["error"][
                "properties"
            ]["stack_trace"]
            error_stack_trace_mappings.setdefault("index", False)
            error_stack_trace_mappings.setdefault("doc_values", False)
        except KeyError:
            pass

        template["mappings"] = {"_doc": mappings_section}
    else:
        template["mappings"] = mappings_section

    # _meta can't be at template root in legacy templates, so moving back to mappings section
    # if present
    if "_meta" in template:
        mappings_section["_meta"] = template.pop("_meta")

    return template