Skip to content

Commit 71d8614

Browse files
committed
Factor UnknownNamedObjectException into its own class (#28931)
* Factor UnknownNamedObjectException into its own class This moves the inner class `UnknownNamedObjectException` from `NamedXContentRegistry` into a top-level class. This is so that `NamedXContentRegistry` doesn't have to depend on StreamInput and StreamOutput. Relates to #28504
1 parent 0ac6a36 commit 71d8614

File tree

10 files changed

+83
-59
lines changed

10 files changed

+83
-59
lines changed

server/src/main/java/org/elasticsearch/ElasticsearchException.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -986,8 +986,8 @@ private enum ElasticsearchExceptionHandle {
986986
org.elasticsearch.tasks.TaskCancelledException::new, 146, Version.V_5_1_1),
987987
SHARD_LOCK_OBTAIN_FAILED_EXCEPTION(org.elasticsearch.env.ShardLockObtainFailedException.class,
988988
org.elasticsearch.env.ShardLockObtainFailedException::new, 147, Version.V_5_0_2),
989-
UNKNOWN_NAMED_OBJECT_EXCEPTION(org.elasticsearch.common.xcontent.NamedXContentRegistry.UnknownNamedObjectException.class,
990-
org.elasticsearch.common.xcontent.NamedXContentRegistry.UnknownNamedObjectException::new, 148, Version.V_5_2_0),
989+
UNKNOWN_NAMED_OBJECT_EXCEPTION(org.elasticsearch.common.xcontent.UnknownNamedObjectException.class,
990+
org.elasticsearch.common.xcontent.UnknownNamedObjectException::new, 148, Version.V_5_2_0),
991991
TOO_MANY_BUCKETS_EXCEPTION(MultiBucketConsumerService.TooManyBucketsException.class,
992992
MultiBucketConsumerService.TooManyBucketsException::new, 149,
993993
Version.V_6_2_0);

server/src/main/java/org/elasticsearch/cluster/metadata/MetaData.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
import org.elasticsearch.common.settings.Setting;
4444
import org.elasticsearch.common.settings.Setting.Property;
4545
import org.elasticsearch.common.settings.Settings;
46-
import org.elasticsearch.common.xcontent.NamedXContentRegistry.UnknownNamedObjectException;
46+
import org.elasticsearch.common.xcontent.UnknownNamedObjectException;
4747
import org.elasticsearch.common.xcontent.ToXContent;
4848
import org.elasticsearch.common.xcontent.ToXContentFragment;
4949
import org.elasticsearch.common.xcontent.XContentBuilder;

server/src/main/java/org/elasticsearch/common/xcontent/NamedXContentRegistry.java

Lines changed: 0 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@
2323
import org.elasticsearch.common.CheckedFunction;
2424
import org.elasticsearch.common.ParseField;
2525
import org.elasticsearch.common.ParsingException;
26-
import org.elasticsearch.common.io.stream.StreamInput;
27-
import org.elasticsearch.common.io.stream.StreamOutput;
2826

2927
import java.io.IOException;
3028
import java.util.ArrayList;
@@ -36,7 +34,6 @@
3634
import static java.util.Collections.emptyList;
3735
import static java.util.Collections.emptyMap;
3836
import static java.util.Collections.unmodifiableMap;
39-
import static java.util.Objects.requireNonNull;
4037

4138
public class NamedXContentRegistry {
4239
/**
@@ -143,50 +140,4 @@ public <T, C> T parseNamedObject(Class<T> categoryClass, String name, XContentPa
143140
return categoryClass.cast(entry.parser.parse(parser, context));
144141
}
145142

146-
/**
147-
* Thrown when {@link NamedXContentRegistry#parseNamedObject(Class, String, XContentParser, Object)} is called with an unregistered
148-
* name. When this bubbles up to the rest layer it is converted into a response with {@code 400 BAD REQUEST} status.
149-
*/
150-
public static class UnknownNamedObjectException extends ParsingException {
151-
private final String categoryClass;
152-
private final String name;
153-
154-
public UnknownNamedObjectException(XContentLocation contentLocation, Class<?> categoryClass,
155-
String name) {
156-
super(contentLocation, "Unknown " + categoryClass.getSimpleName() + " [" + name + "]");
157-
this.categoryClass = requireNonNull(categoryClass, "categoryClass is required").getName();
158-
this.name = requireNonNull(name, "name is required");
159-
}
160-
161-
/**
162-
* Read from a stream.
163-
*/
164-
public UnknownNamedObjectException(StreamInput in) throws IOException {
165-
super(in);
166-
categoryClass = in.readString();
167-
name = in.readString();
168-
}
169-
170-
@Override
171-
public void writeTo(StreamOutput out) throws IOException {
172-
super.writeTo(out);
173-
out.writeString(categoryClass);
174-
out.writeString(name);
175-
}
176-
177-
/**
178-
* Category class that was missing a parser. This is a String instead of a class because the class might not be on the classpath
179-
* of all nodes or it might be exclusive to a plugin or something.
180-
*/
181-
public String getCategoryClass() {
182-
return categoryClass;
183-
}
184-
185-
/**
186-
* Name of the missing parser.
187-
*/
188-
public String getName() {
189-
return name;
190-
}
191-
}
192143
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.elasticsearch.common.xcontent;
21+
22+
import org.elasticsearch.common.ParsingException;
23+
import org.elasticsearch.common.io.stream.StreamInput;
24+
import org.elasticsearch.common.io.stream.StreamOutput;
25+
26+
import java.io.IOException;
27+
28+
import static java.util.Objects.requireNonNull;
29+
30+
/**
31+
* Thrown when {@link NamedXContentRegistry#parseNamedObject(Class, String, XContentParser, Object)} is called with an unregistered
32+
* name. When this bubbles up to the rest layer it is converted into a response with {@code 400 BAD REQUEST} status.
33+
*/
34+
public class UnknownNamedObjectException extends ParsingException {
35+
private final String categoryClass;
36+
private final String name;
37+
38+
public UnknownNamedObjectException(XContentLocation contentLocation, Class<?> categoryClass, String name) {
39+
super(contentLocation, "Unknown " + categoryClass.getSimpleName() + " [" + name + "]");
40+
this.categoryClass = requireNonNull(categoryClass, "categoryClass is required").getName();
41+
this.name = requireNonNull(name, "name is required");
42+
}
43+
44+
/**
45+
* Read from a stream.
46+
*/
47+
public UnknownNamedObjectException(StreamInput in) throws IOException {
48+
super(in);
49+
categoryClass = in.readString();
50+
name = in.readString();
51+
}
52+
53+
@Override
54+
public void writeTo(StreamOutput out) throws IOException {
55+
super.writeTo(out);
56+
out.writeString(categoryClass);
57+
out.writeString(name);
58+
}
59+
60+
/**
61+
* Category class that was missing a parser. This is a String instead of a class because the class might not be on the classpath
62+
* of all nodes or it might be exclusive to a plugin or something.
63+
*/
64+
public String getCategoryClass() {
65+
return categoryClass;
66+
}
67+
68+
/**
69+
* Name of the missing parser.
70+
*/
71+
public String getName() {
72+
return name;
73+
}
74+
}

server/src/main/java/org/elasticsearch/common/xcontent/yaml/YamlXContent.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import com.fasterxml.jackson.core.JsonEncoding;
2323
import com.fasterxml.jackson.core.JsonParser;
2424
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
25-
import org.elasticsearch.ElasticsearchParseException;
2625
import org.elasticsearch.common.io.FastStringReader;
2726
import org.elasticsearch.common.xcontent.DeprecationHandler;
2827
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
@@ -66,7 +65,7 @@ public XContentType type() {
6665

6766
@Override
6867
public byte streamSeparator() {
69-
throw new ElasticsearchParseException("yaml does not support stream parsing...");
68+
throw new UnsupportedOperationException("yaml does not support stream parsing...");
7069
}
7170

7271
@Override

server/src/main/java/org/elasticsearch/index/query/AbstractQueryBuilder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
import org.elasticsearch.common.io.stream.StreamOutput;
3232
import org.elasticsearch.common.lucene.BytesRefs;
3333
import org.elasticsearch.common.xcontent.AbstractObjectParser;
34-
import org.elasticsearch.common.xcontent.NamedXContentRegistry.UnknownNamedObjectException;
34+
import org.elasticsearch.common.xcontent.UnknownNamedObjectException;
3535
import org.elasticsearch.common.xcontent.XContentBuilder;
3636
import org.elasticsearch.common.xcontent.XContentLocation;
3737
import org.elasticsearch.common.xcontent.XContentParser;

server/src/test/java/org/elasticsearch/ExceptionSerializationTests.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
import org.elasticsearch.common.unit.ByteSizeValue;
5454
import org.elasticsearch.common.util.CancellableThreadsTests;
5555
import org.elasticsearch.common.util.set.Sets;
56+
import org.elasticsearch.common.xcontent.UnknownNamedObjectException;
5657
import org.elasticsearch.common.xcontent.XContentLocation;
5758
import org.elasticsearch.discovery.DiscoverySettings;
5859
import org.elasticsearch.env.ShardLockObtainFailedException;
@@ -813,7 +814,7 @@ public void testIds() {
813814
ids.put(145, org.elasticsearch.ElasticsearchStatusException.class);
814815
ids.put(146, org.elasticsearch.tasks.TaskCancelledException.class);
815816
ids.put(147, org.elasticsearch.env.ShardLockObtainFailedException.class);
816-
ids.put(148, org.elasticsearch.common.xcontent.NamedXContentRegistry.UnknownNamedObjectException.class);
817+
ids.put(148, UnknownNamedObjectException.class);
817818
ids.put(149, MultiBucketConsumerService.TooManyBucketsException.class);
818819

819820
Map<Class<? extends ElasticsearchException>, Integer> reverse = new HashMap<>();

server/src/test/java/org/elasticsearch/common/xcontent/BaseXContentTestCase.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1023,7 +1023,7 @@ public void testNamedObject() throws IOException {
10231023
{
10241024
p.nextToken();
10251025
assertEquals("test", p.namedObject(Object.class, "str", null));
1026-
NamedXContentRegistry.UnknownNamedObjectException e = expectThrows(NamedXContentRegistry.UnknownNamedObjectException.class,
1026+
UnknownNamedObjectException e = expectThrows(UnknownNamedObjectException.class,
10271027
() -> p.namedObject(Object.class, "unknown", null));
10281028
assertEquals("Unknown Object [unknown]", e.getMessage());
10291029
assertEquals("java.lang.Object", e.getCategoryClass());

server/src/test/java/org/elasticsearch/common/xcontent/UnknownNamedObjectExceptionTests.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121

2222
import org.elasticsearch.common.io.stream.BytesStreamOutput;
2323
import org.elasticsearch.common.io.stream.StreamInput;
24-
import org.elasticsearch.common.xcontent.NamedXContentRegistry.UnknownNamedObjectException;
2524
import org.elasticsearch.rest.RestStatus;
2625
import org.elasticsearch.test.ESTestCase;
2726

server/src/test/java/org/elasticsearch/common/xcontent/XContentParserUtilsTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ public void testParseTypedKeysObject() throws IOException {
187187
ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.nextToken(), parser::getTokenLocation);
188188
ensureExpectedToken(XContentParser.Token.FIELD_NAME, parser.nextToken(), parser::getTokenLocation);
189189
ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.nextToken(), parser::getTokenLocation);
190-
NamedXContentRegistry.UnknownNamedObjectException e = expectThrows(NamedXContentRegistry.UnknownNamedObjectException.class,
190+
UnknownNamedObjectException e = expectThrows(UnknownNamedObjectException.class,
191191
() -> parseTypedKeysObject(parser, delimiter, Boolean.class, a -> {}));
192192
assertEquals("Unknown Boolean [type]", e.getMessage());
193193
assertEquals("type", e.getName());

0 commit comments

Comments
 (0)