forked from elastic/elasticsearch
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Painless sandboxes some errors from Java for which it can recover. These errors are wrapped within a ScriptException. However, retaining the error as a cause can be confusing when walking the error chain. This commit wraps the error so that the real error type does not appear, but maintains the same error message in xcontent serialized form.
- Loading branch information
Showing
6 changed files
with
136 additions
and
19 deletions.
There are no files selected for viewing
50 changes: 50 additions & 0 deletions
50
modules/lang-painless/src/main/java/org/elasticsearch/painless/ErrorCauseWrapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
* 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.painless; | ||
|
||
import org.elasticsearch.ElasticsearchException; | ||
import org.elasticsearch.xcontent.XContentBuilder; | ||
|
||
import java.io.IOException; | ||
import java.util.List; | ||
|
||
/** | ||
* A wrapper for Error which hides the underlying Error from the exception cause chain. | ||
* <p> | ||
* Only errors which should be sandboxed and not cause the node to crash are wrapped. | ||
*/ | ||
class ErrorCauseWrapper extends ElasticsearchException { | ||
|
||
private static final List<Class<? extends Error>> wrappedErrors = List.of( | ||
PainlessError.class, | ||
OutOfMemoryError.class, | ||
StackOverflowError.class, | ||
LinkageError.class | ||
); | ||
|
||
final Throwable realCause; | ||
|
||
private ErrorCauseWrapper(Throwable realCause) { | ||
super(realCause.getMessage()); | ||
this.realCause = realCause; | ||
} | ||
|
||
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { | ||
builder.field("type", getExceptionName(realCause)); | ||
builder.field("reason", realCause.getMessage()); | ||
return builder; | ||
} | ||
|
||
static Throwable maybeWrap(Throwable t) { | ||
if (wrappedErrors.contains(t.getClass())) { | ||
return new ErrorCauseWrapper(t); | ||
} | ||
return t; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
modules/lang-painless/src/test/java/org/elasticsearch/painless/ErrorCauseWrapperTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* | ||
* 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.painless; | ||
|
||
import org.elasticsearch.test.ESTestCase; | ||
import org.elasticsearch.xcontent.ToXContent; | ||
import org.elasticsearch.xcontent.XContentFactory; | ||
import org.elasticsearch.xcontent.XContentParserConfiguration; | ||
import org.elasticsearch.xcontent.json.JsonXContent; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
import java.util.Map; | ||
|
||
import static org.hamcrest.Matchers.hasEntry; | ||
import static org.hamcrest.Matchers.instanceOf; | ||
import static org.hamcrest.Matchers.is; | ||
import static org.hamcrest.Matchers.nullValue; | ||
|
||
public class ErrorCauseWrapperTests extends ESTestCase { | ||
|
||
ErrorCauseWrapper assertWraps(Error realError) { | ||
var e = ErrorCauseWrapper.maybeWrap(realError); | ||
assertThat(e.getCause(), nullValue()); | ||
assertThat(e, instanceOf(ErrorCauseWrapper.class)); | ||
var wrapper = (ErrorCauseWrapper) e; | ||
assertThat(wrapper.realCause, is(realError)); | ||
return wrapper; | ||
} | ||
|
||
public void testOutOfMemoryError() { | ||
assertWraps(new OutOfMemoryError("oom")); | ||
} | ||
|
||
public void testStackOverflowError() { | ||
assertWraps(new StackOverflowError("soe")); | ||
} | ||
|
||
public void testLinkageError() { | ||
assertWraps(new LinkageError("le")); | ||
} | ||
|
||
public void testPainlessError() { | ||
assertWraps(new PainlessError("pe")); | ||
} | ||
|
||
public void testNotWrapped() { | ||
var realError = new AssertionError("not wrapped"); | ||
var e = ErrorCauseWrapper.maybeWrap(realError); | ||
assertThat(e, is(realError)); | ||
} | ||
|
||
public void testXContent() throws IOException { | ||
var e = assertWraps(new PainlessError("some error")); | ||
|
||
var output = new ByteArrayOutputStream(); | ||
var builder = XContentFactory.jsonBuilder(output); | ||
builder.startObject(); | ||
e.toXContent(builder, ToXContent.EMPTY_PARAMS); | ||
builder.endObject(); | ||
builder.flush(); | ||
|
||
try (var parser = JsonXContent.jsonXContent.createParser(XContentParserConfiguration.EMPTY, output.toByteArray())) { | ||
Map<String, String> content = parser.mapStrings(); | ||
assertThat(content, hasEntry("type", "painless_error")); | ||
assertThat(content, hasEntry("reason", "some error")); | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters