@@ -529,9 +529,7 @@ public IndexMetaDataGenerations indexMetaDataGenerations() {
529
529
* from cached bytes that we trust to not contain broken generations.
530
530
*/
531
531
public static RepositoryData snapshotsFromXContent (XContentParser parser , long genId , boolean fixBrokenShardGens ) throws IOException {
532
- if (parser .nextToken () != XContentParser .Token .START_OBJECT ) {
533
- throw new ElasticsearchParseException ("start object expected" );
534
- }
532
+ XContentParserUtils .ensureExpectedToken (XContentParser .Token .START_OBJECT , parser .nextToken (), parser ::getTokenLocation );
535
533
536
534
final Map <String , SnapshotId > snapshots = new HashMap <>();
537
535
final Map <String , SnapshotState > snapshotStates = new HashMap <>();
@@ -551,15 +549,13 @@ public static RepositoryData snapshotsFromXContent(XContentParser parser, long g
551
549
parseIndices (parser , fixBrokenShardGens , snapshots , indexSnapshots , indexLookup , shardGenerations );
552
550
break ;
553
551
case INDEX_METADATA_IDENTIFIERS :
554
- if (parser .nextToken () != XContentParser .Token .START_OBJECT ) {
555
- throw new ElasticsearchParseException ("start object expected [" + INDEX_METADATA_IDENTIFIERS + "]" );
556
- }
552
+ XContentParserUtils .ensureExpectedToken (XContentParser .Token .START_OBJECT , parser .nextToken (),
553
+ parser ::getTokenLocation );
557
554
indexMetaIdentifiers = parser .mapStrings ();
558
555
break ;
559
556
case MIN_VERSION :
560
- if (parser .nextToken () != XContentParser .Token .VALUE_STRING ) {
561
- throw new ElasticsearchParseException ("version string expected [min_version]" );
562
- }
557
+ XContentParserUtils .ensureExpectedToken (XContentParser .Token .VALUE_STRING , parser .nextToken (),
558
+ parser ::getTokenLocation );
563
559
final Version version = Version .fromString (parser .text ());
564
560
assert SnapshotsService .useShardGenerations (version );
565
561
break ;
@@ -615,9 +611,7 @@ private static IndexMetaDataGenerations buildIndexMetaGenerations(Map<SnapshotId
615
611
private static void parseSnapshots (XContentParser parser , Map <String , SnapshotId > snapshots , Map <String , SnapshotState > snapshotStates ,
616
612
Map <String , Version > snapshotVersions ,
617
613
Map <SnapshotId , Map <String , String >> indexMetaLookup ) throws IOException {
618
- if (parser .nextToken () != XContentParser .Token .START_ARRAY ) {
619
- throw new ElasticsearchParseException ("expected array for [" + SNAPSHOTS + "]" );
620
- }
614
+ XContentParserUtils .ensureExpectedToken (XContentParser .Token .START_ARRAY , parser .nextToken (), parser ::getTokenLocation );
621
615
while (parser .nextToken () != XContentParser .Token .END_ARRAY ) {
622
616
String name = null ;
623
617
String uuid = null ;
@@ -673,18 +667,14 @@ private static void parseSnapshots(XContentParser parser, Map<String, SnapshotId
673
667
private static void parseIndices (XContentParser parser , boolean fixBrokenShardGens , Map <String , SnapshotId > snapshots ,
674
668
Map <IndexId , List <SnapshotId >> indexSnapshots , Map <String , IndexId > indexLookup ,
675
669
ShardGenerations .Builder shardGenerations ) throws IOException {
676
- if (parser .nextToken () != XContentParser .Token .START_OBJECT ) {
677
- throw new ElasticsearchParseException ("start object expected [indices]" );
678
- }
670
+ XContentParserUtils .ensureExpectedToken (XContentParser .Token .START_OBJECT , parser .nextToken (), parser ::getTokenLocation );
679
671
while (parser .nextToken () != XContentParser .Token .END_OBJECT ) {
680
672
final String indexName = parser .currentName ();
681
673
final List <SnapshotId > snapshotIds = new ArrayList <>();
682
674
final List <String > gens = new ArrayList <>();
683
675
684
676
IndexId indexId = null ;
685
- if (parser .nextToken () != XContentParser .Token .START_OBJECT ) {
686
- throw new ElasticsearchParseException ("start object expected index[" + indexName + "]" );
687
- }
677
+ XContentParserUtils .ensureExpectedToken (XContentParser .Token .START_OBJECT , parser .nextToken (), parser ::getTokenLocation );
688
678
while (parser .nextToken () != XContentParser .Token .END_OBJECT ) {
689
679
final String indexMetaFieldName = parser .currentName ();
690
680
final XContentParser .Token currentToken = parser .nextToken ();
@@ -693,9 +683,7 @@ private static void parseIndices(XContentParser parser, boolean fixBrokenShardGe
693
683
indexId = new IndexId (indexName , parser .text ());
694
684
break ;
695
685
case SNAPSHOTS :
696
- if (currentToken != XContentParser .Token .START_ARRAY ) {
697
- throw new ElasticsearchParseException ("start array expected [snapshots]" );
698
- }
686
+ XContentParserUtils .ensureExpectedToken (XContentParser .Token .START_ARRAY , currentToken , parser ::getTokenLocation );
699
687
XContentParser .Token currToken ;
700
688
while ((currToken = parser .nextToken ()) != XContentParser .Token .END_ARRAY ) {
701
689
final String uuid ;
0 commit comments