-
Notifications
You must be signed in to change notification settings - Fork 25.6k
Decouple NamedXContentRegistry from ElasticsearchException #29253
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
483803a
4086064
36c6b86
b87b4e7
0bb05c1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| /* | ||
| * Licensed to Elasticsearch under one or more contributor | ||
| * license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright | ||
| * ownership. Elasticsearch licenses this file to you under | ||
| * the Apache License, Version 2.0 (the "License"); you may | ||
| * not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| package org.elasticsearch.common.xcontent; | ||
|
|
||
| import java.util.Optional; | ||
|
|
||
| /** | ||
| * Thrown when one of the XContent parsers cannot parse something. | ||
| */ | ||
| public class XContentParseException extends IllegalArgumentException { | ||
|
|
||
| private final Optional<XContentLocation> location; | ||
|
|
||
| public XContentParseException(String message) { | ||
| this(null, message); | ||
| } | ||
|
|
||
| public XContentParseException(XContentLocation location, String message) { | ||
| super(message); | ||
| this.location = Optional.ofNullable(location); | ||
| } | ||
|
|
||
| public int getLineNumber() { | ||
| return location.map(l -> l.lineNumber).orElse(-1); | ||
| } | ||
|
|
||
| public int getColumnNumber() { | ||
| return location.map(l -> l.columnNumber).orElse(-1); | ||
| } | ||
|
|
||
| @Override | ||
| public String getMessage() { | ||
| return location.map(l -> "[" + l.toString() + "] ").orElse("") + super.getMessage(); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -67,6 +67,7 @@ | |
| import static java.util.Collections.singletonMap; | ||
| import static org.hamcrest.Matchers.allOf; | ||
| import static org.hamcrest.Matchers.containsString; | ||
| import static org.hamcrest.Matchers.endsWith; | ||
| import static org.hamcrest.Matchers.equalTo; | ||
| import static org.hamcrest.Matchers.instanceOf; | ||
| import static org.hamcrest.Matchers.notNullValue; | ||
|
|
@@ -1024,22 +1025,20 @@ public void testNamedObject() throws IOException { | |
| { | ||
| p.nextToken(); | ||
| assertEquals("test", p.namedObject(Object.class, "str", null)); | ||
| UnknownNamedObjectException e = expectThrows(UnknownNamedObjectException.class, | ||
| XContentParseException e = expectThrows(XContentParseException.class, | ||
|
||
| () -> p.namedObject(Object.class, "unknown", null)); | ||
| assertEquals("Unknown Object [unknown]", e.getMessage()); | ||
| assertEquals("java.lang.Object", e.getCategoryClass()); | ||
| assertEquals("unknown", e.getName()); | ||
| assertThat(e.getMessage(), endsWith("unable to parse Object with name [unknown]: parser not found")); | ||
| } | ||
| { | ||
| Exception e = expectThrows(ElasticsearchException.class, () -> p.namedObject(String.class, "doesn't matter", null)); | ||
| assertEquals("Unknown namedObject category [java.lang.String]", e.getMessage()); | ||
| Exception e = expectThrows(XContentParseException.class, () -> p.namedObject(String.class, "doesn't matter", null)); | ||
|
||
| assertEquals("unknown named object category [java.lang.String]", e.getMessage()); | ||
| } | ||
| { | ||
| XContentParser emptyRegistryParser = xcontentType().xContent() | ||
| .createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, new byte[] {}); | ||
| Exception e = expectThrows(ElasticsearchException.class, | ||
| Exception e = expectThrows(XContentParseException.class, | ||
|
||
| () -> emptyRegistryParser.namedObject(String.class, "doesn't matter", null)); | ||
| assertEquals("namedObject is not supported for this parser", e.getMessage()); | ||
| assertEquals("named objects are not supported for this parser", e.getMessage()); | ||
| } | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -32,6 +32,7 @@ | |
| import org.elasticsearch.common.xcontent.ToXContent; | ||
| import org.elasticsearch.common.xcontent.XContentBuilder; | ||
| import org.elasticsearch.common.xcontent.XContentFactory; | ||
| import org.elasticsearch.common.xcontent.XContentParseException; | ||
| import org.elasticsearch.common.xcontent.XContentParser; | ||
| import org.elasticsearch.common.xcontent.XContentType; | ||
| import org.elasticsearch.common.xcontent.json.JsonXContent; | ||
|
|
@@ -220,8 +221,8 @@ public void testUnknownFieldsExpection() throws IOException { | |
| "}\n"; | ||
| { | ||
| XContentParser parser = createParser(rescoreElement); | ||
| Exception e = expectThrows(ParsingException.class, () -> RescorerBuilder.parseFromXContent(parser)); | ||
| assertEquals("Unknown RescorerBuilder [bad_rescorer_name]", e.getMessage()); | ||
| Exception e = expectThrows(XContentParseException.class, () -> RescorerBuilder.parseFromXContent(parser)); | ||
|
||
| assertEquals("[3:27] unable to parse RescorerBuilder with name [bad_rescorer_name]: parser not found", e.getMessage()); | ||
| } | ||
|
|
||
| rescoreElement = "{\n" + | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we're going to use this
XContentParseExceptionfor anything other that unknown named objects we shouldn't catch it here. We need to be sure this is an unknown named object. Either we should rename the exception or make a subclass of it for unknown named objects, I think.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah that's probably a good idea, for now there are no other uses of this exception, but if that changes in the future I will make a subclass of it for this.