1
1
package org .elasticsearch .script ;
2
2
3
3
import org .elasticsearch .ElasticsearchException ;
4
+ import org .elasticsearch .Version ;
4
5
import org .elasticsearch .common .Strings ;
5
6
import org .elasticsearch .common .io .stream .StreamInput ;
6
7
import org .elasticsearch .common .io .stream .StreamOutput ;
@@ -51,6 +52,7 @@ public class ScriptException extends ElasticsearchException {
51
52
private final List <String > scriptStack ;
52
53
private final String script ;
53
54
private final String lang ;
55
+ private final Position pos ;
54
56
55
57
/**
56
58
* Create a new ScriptException.
@@ -61,13 +63,22 @@ public class ScriptException extends ElasticsearchException {
61
63
* Must not be {@code null}, but can be empty (though this should be avoided if possible).
62
64
* @param script Identifier for which script failed. Must not be {@code null}.
63
65
* @param lang Scripting engine language, such as "painless". Must not be {@code null}.
64
- * @throws NullPointerException if any parameters are {@code null}.
66
+ * @param pos Position of error within script, may be {@code null}.
67
+ * @throws NullPointerException if any parameters are {@code null} except pos.
65
68
*/
66
- public ScriptException (String message , Throwable cause , List <String > scriptStack , String script , String lang ) {
69
+ public ScriptException (String message , Throwable cause , List <String > scriptStack , String script , String lang , Position pos ) {
67
70
super (Objects .requireNonNull (message ), Objects .requireNonNull (cause ));
68
71
this .scriptStack = Collections .unmodifiableList (Objects .requireNonNull (scriptStack ));
69
72
this .script = Objects .requireNonNull (script );
70
73
this .lang = Objects .requireNonNull (lang );
74
+ this .pos = pos ;
75
+ }
76
+
77
+ /**
78
+ * Create a new ScriptException with null Position.
79
+ */
80
+ public ScriptException (String message , Throwable cause , List <String > scriptStack , String script , String lang ) {
81
+ this (message , cause , scriptStack , script , lang , null );
71
82
}
72
83
73
84
/**
@@ -78,6 +89,11 @@ public ScriptException(StreamInput in) throws IOException {
78
89
scriptStack = Arrays .asList (in .readStringArray ());
79
90
script = in .readString ();
80
91
lang = in .readString ();
92
+ if (in .getVersion ().onOrAfter (Version .V_7_7_0 ) && in .readBoolean ()) {
93
+ pos = new Position (in );
94
+ } else {
95
+ pos = null ;
96
+ }
81
97
}
82
98
83
99
@ Override
@@ -86,13 +102,24 @@ public void writeTo(StreamOutput out) throws IOException {
86
102
out .writeStringArray (scriptStack .toArray (new String [0 ]));
87
103
out .writeString (script );
88
104
out .writeString (lang );
105
+ if (out .getVersion ().onOrAfter (Version .V_7_7_0 )) {
106
+ if (pos == null ) {
107
+ out .writeBoolean (false );
108
+ } else {
109
+ out .writeBoolean (true );
110
+ pos .writeTo (out );
111
+ }
112
+ }
89
113
}
90
114
91
115
@ Override
92
116
protected void metadataToXContent (XContentBuilder builder , Params params ) throws IOException {
93
117
builder .field ("script_stack" , scriptStack );
94
118
builder .field ("script" , script );
95
119
builder .field ("lang" , lang );
120
+ if (pos != null ) {
121
+ pos .toXContent (builder , params );
122
+ }
96
123
}
97
124
98
125
/**
@@ -119,6 +146,13 @@ public String getLang() {
119
146
return lang ;
120
147
}
121
148
149
+ /**
150
+ * Returns the position of the error.
151
+ */
152
+ public Position getPos () {
153
+ return pos ;
154
+ }
155
+
122
156
/**
123
157
* Returns a JSON version of this exception for debugging.
124
158
*/
@@ -138,4 +172,51 @@ public String toJsonString() {
138
172
public RestStatus status () {
139
173
return RestStatus .BAD_REQUEST ;
140
174
}
175
+
176
+ public static class Position {
177
+ public final int offset ;
178
+ public final int start ;
179
+ public final int end ;
180
+
181
+ public Position (int offset , int start , int end ) {
182
+ this .offset = offset ;
183
+ this .start = start ;
184
+ this .end = end ;
185
+ }
186
+
187
+ Position (StreamInput in ) throws IOException {
188
+ offset = in .readInt ();
189
+ start = in .readInt ();
190
+ end = in .readInt ();
191
+ }
192
+
193
+ void writeTo (StreamOutput out ) throws IOException {
194
+ out .writeInt (offset );
195
+ out .writeInt (start );
196
+ out .writeInt (end );
197
+ }
198
+
199
+ void toXContent (XContentBuilder builder , Params params ) throws IOException {
200
+ builder .startObject ("position" );
201
+ builder .field ("offset" , offset );
202
+ builder .field ("start" , start );
203
+ builder .field ("end" , end );
204
+ builder .endObject ();
205
+ }
206
+
207
+ @ Override
208
+ public boolean equals (Object o ) {
209
+ if (this == o )
210
+ return true ;
211
+ if (o == null || getClass () != o .getClass ())
212
+ return false ;
213
+ Position position = (Position ) o ;
214
+ return offset == position .offset && start == position .start && end == position .end ;
215
+ }
216
+
217
+ @ Override
218
+ public int hashCode () {
219
+ return Objects .hash (offset , start , end );
220
+ }
221
+ }
141
222
}
0 commit comments