### Elasticsearch Version

8.10.4

### Installed Plugins

_No response_

### Java Version

_bundled_

### OS Version

Cloud

### Problem Description

The [search template API](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-template-api.html) and [render search template API](https://www.elastic.co/guide/en/elasticsearch/reference/current/render-search-template-api.html) handle invalid rendered JSON differently.

The search template API (correctly) returns a `400` response, while the render search template API returns a`500` response. This is due to the fact that the render search template API implementation [returns early from the convert method](https://github.com/elastic/elasticsearch/blob/3ce905b75432e36648ae013a3580b1f12537cae9/modules/lang-mustache/src/main/java/org/elasticsearch/script/mustache/TransportSearchTemplateAction.java#L108), skipping the code that would throw `XContentParseException`.

### Steps to Reproduce

1. Create a search template that will render invalid JSON:

```
PUT _scripts/test-search-template
{
  "script": {
    "lang": "mustache",
    "source": """
    {
      "query": {
        "multi_match": {
          "query": "{{query_string}}",
          "fields": [{{#text_fields}}"{{name}}^{{boost}}"{{/text_fields}}]
        }
      },
      "from": "{{from}}",
      "size": "{{size}}"
    }
    """,
    "params": {
      "query_string": "My query string"
    }
  }
}
```

2. Use the search template API to run a query:

```
GET my-index/_search/template
{
  "id": "test-search-template",
  "params": {
    "text_fields": [
      {"name": "title", "boost": 10},
      {"name": "description", "boost": 5}
    ],
    "from": 0,
    "size": 10
  }
}
```

Observe the `400` response:

```
{
  "error": {
    "root_cause": [
      {
        "type": "x_content_parse_exception",
        "reason": """[6:33] Unexpected character ('"' (code 34)): was expecting comma to separate Array entries
 at [Source: (String)"
    {
      "query": {
        "multi_match": {
          "query": "",
          "fields": ["title^10""description^5"]
        }
      },
      "from": "0",
      "size": "10"
    }
    "; line: 6, column: 33]"""
      }
    ],
    "type": "x_content_parse_exception",
    "reason": """[6:33] Unexpected character ('"' (code 34)): was expecting comma to separate Array entries
 at [Source: (String)"
    {
      "query": {
        "multi_match": {
          "query": "",
          "fields": ["title^10""description^5"]
        }
      },
      "from": "0",
      "size": "10"
    }
    "; line: 6, column: 33]""",
    "caused_by": {
      "type": "json_parse_exception",
      "reason": """Unexpected character ('"' (code 34)): was expecting comma to separate Array entries
 at [Source: (String)"
    {
      "query": {
        "multi_match": {
          "query": "",
          "fields": ["title^10""description^5"]
        }
      },
      "from": "0",
      "size": "10"
    }
    "; line: 6, column: 33]"""
    }
  },
  "status": 400
}
```

3. Use the render search template API to render the search template:

```
POST _render/template
{
  "id": "test-search-template",
  "params": {
    "text_fields": [
      {"name": "title", "boost": 10},
      {"name": "description", "boost": 5}
    ],
    "from": 0,
    "size": 10
  }
}
```

Observe the `500` response:

```
{
  "error": {
    "root_cause": [
      {
        "type": "json_parse_exception",
        "reason": """Unexpected character ('"' (code 34)): was expecting comma to separate Array entries
 at [Source: (org.elasticsearch.common.io.stream.ByteBufferStreamInput); line: 6, column: 33]"""
      }
    ],
    "type": "json_parse_exception",
    "reason": """Unexpected character ('"' (code 34)): was expecting comma to separate Array entries
 at [Source: (org.elasticsearch.common.io.stream.ByteBufferStreamInput); line: 6, column: 33]""",
    "suppressed": [
      {
        "type": "illegal_state_exception",
        "reason": "Failed to close the XContentBuilder",
        "caused_by": {
          "type": "i_o_exception",
          "reason": "Unclosed object or array found"
        }
      }
    ]
  },
  "status": 500
}
```

### Logs (if relevant)

_No response_
modules/lang-mustache/src/main/java/org/elasticsearch/script/mustache/TransportSearchTemplateAction.java
/*
 * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
 * or more contributor license agreements. Licensed under the Elastic License
 * 2.0 and the Server Side Public License, v 1; you may not use this file except
 * in compliance with, at your election, the Elastic License 2.0 or the Server
 * Side Public License, v 1.
 */
package org.elasticsearch.script.mustache;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.support.ActionFilters;
import org.elasticsearch.action.support.HandledTransportAction;
import org.elasticsearch.client.internal.node.NodeClient;
import org.elasticsearch.common.bytes.BytesArray;
import org.elasticsearch.common.inject.Inject;