-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
Status.java
485 lines (412 loc) · 20.4 KB
/
Status.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
/*
* Copyright (c) 2002-2016 "Neo Technology,"
* Network Engine for Objects in Lund AB [http://neotechnology.com]
*
* This file is part of Neo4j.
*
* Neo4j is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.neo4j.kernel.api.exceptions;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import static java.lang.String.format;
import static org.neo4j.kernel.api.exceptions.Status.Classification.ClientError;
import static org.neo4j.kernel.api.exceptions.Status.Classification.ClientNotification;
import static org.neo4j.kernel.api.exceptions.Status.Classification.DatabaseError;
import static org.neo4j.kernel.api.exceptions.Status.Classification.TransientError;
/**
* This is the codification of all available surface-api status codes. If you are throwing an error to a user through
* one of the key APIs, you should opt for using or adding an error code here.
*
* Each {@link Status} has an associated category, represented by the inner enums in this class.
* Each {@link Status} also has an associated {@link Classification} which defines meta-data about the code, such
* as if the error was caused by a user or the database (and later on if the code denotes an error or merely a warning).
*
* This class is not part of the public Neo4j API, and backwards compatibility for using it as a Java class is not
* guaranteed. Instead, the automatically generated documentation derived from this class and available in the Neo4j
* manual should be considered a user-level API.
*
* Currently, only the transactional http endpoint is dedicated to using these status codes.
*/
public interface Status
{
/*
* A note on naming: Since these are public status codes and users will base error handling on them, please take
* care to place them in correct categories and assign them correct classifications. Also make sure you are not
* introducing duplicates.
*
* If you are unsure, contact Jake or Tobias before making modifications.
*/
enum Network implements Status
{
// transient
UnknownFailure( TransientError, "An unknown network failure occurred, a retry may resolve the issue." );
private final Code code;
@Override
public Code code()
{
return code;
}
Network( Classification classification, String description )
{
this.code = new Code( classification, this, description );
}
}
enum Request implements Status
{
// client
Invalid( ClientError, "The client provided an invalid request." ),
InvalidFormat( ClientError, "The client provided a request that was missing required fields, or had values " +
"that are not allowed." );
private final Code code;
@Override
public Code code()
{
return code;
}
Request( Classification classification, String description )
{
this.code = new Code( classification, this, description );
}
}
enum Transaction implements Status
{
UnknownId( ClientError, "The request referred to a transaction that does not exist."),
ConcurrentRequest( ClientError, "There were concurrent requests accessing the same transaction, which is not " +
"allowed." ),
CouldNotBegin( DatabaseError, "The database was unable to start the transaction." ),
CouldNotRollback( DatabaseError, "The database was unable to roll back the transaction." ),
CouldNotCommit( DatabaseError, "The database was unable to commit the transaction." ),
CouldNotWriteToLog( DatabaseError, "The database was unable to write transaction to log." ),
InvalidType( ClientError, "The transaction is of the wrong type to service the request. For instance, a " +
"transaction that has had schema modifications performed in it cannot be used to subsequently " +
"perform data operations, and vice versa." ),
ReleaseLocksFailed( DatabaseError, "The transaction was unable to release one or more of its locks." ),
AcquireLockTimeout( TransientError, "The transaction was unable to acquire a lock, for instance due to a " +
"timeout or the transaction thread being interrupted." ),
LockSessionInvalid( TransientError, "The lock session under which this transaction was started is no longer valid." ),
DeadlockDetected( TransientError, "This transaction, and at least one more transaction, has acquired locks " +
"in a way that it will wait indefinitely, and the database has aborted it. Retrying this transaction " +
"will most likely be successful."),
EventHandlerThrewException( ClientError, "A transaction event handler threw an exception. The transaction " +
"will be rolled back." ),
ValidationFailed( ClientError, "Transaction changes did not pass validation checks" ),
ConstraintsChanged( TransientError, "Database constraints changed since the start of this transaction" ),
HookFailed( ClientError, "Transaction hook failure." ),
MarkedAsFailed( ClientError, "Transaction was marked as both successful and failed. Failure takes precedence" +
" and so this transaction was rolled back although it may have looked like it was going to be " +
"committed" ),
;
private final Code code;
@Override
public Code code()
{
return code;
}
Transaction( Classification classification, String description )
{
this.code = new Code( classification, this, description );
}
}
enum Statement implements Status
{
// client
InvalidSyntax( ClientError, "The statement contains invalid or unsupported syntax." ),
InvalidSemantics( ClientError, "The statement is syntactically valid, but expresses something that the " +
"database cannot do." ),
ParameterMissing( ClientError, "The statement is referring to a parameter that was not provided in the " +
"request." ),
ConstraintViolation( ClientError, "A constraint imposed by the statement is violated by the data in the " +
"database." ),
EntityNotFound( ClientError, "The statement is directly referring to an entity that does not exist." ),
NoSuchProperty( ClientError, "The statement is referring to a property that does not exist." ),
NoSuchLabel( ClientError, "The statement is referring to a label that does not exist."),
InvalidType( ClientError, "The statement is attempting to perform operations on values with types that " +
"are not supported by the operation." ),
InvalidArguments( ClientError, "The statement is attempting to perform operations using invalid arguments"),
ArithmeticError( ClientError, "Invalid use of arithmetic, such as dividing by zero." ),
// database
ExecutionFailure( DatabaseError, "The database was unable to execute the statement." ),
ExternalResourceFailure( TransientError, "The external resource is not available"),
CartesianProduct( ClientNotification, "This query builds a cartesian product between disconnected patterns." ),
PlannerUnsupportedWarning( ClientNotification, "This query is not supported by the COST planner." ),
RuntimeUnsupportedWarning( ClientNotification, "This query is not supported by the compiled runtime." ),
DeprecationWarning( ClientNotification, "This feature is deprecated and will be removed in future versions." ),
JoinHintUnfulfillableWarning( ClientNotification, "The database was unable to plan a hinted join." ),
JoinHintUnsupportedWarning( ClientNotification, "Queries with join hints are not supported by the RULE planner." ),
DynamicPropertyWarning( ClientNotification, "Queries using dynamic properties will use neither index seeks " +
"nor index scans for those properties" ),
EagerWarning(ClientNotification, "The execution plan for this query contains the Eager operator, " +
"which forces all dependent data to be materialized in main memory " +
"before proceeding"),
IndexMissingWarning( ClientNotification, "Adding a schema index may speed up this query." ),
LabelMissingWarning( ClientNotification, "The provided label is not in the database." ),
RelTypeMissingWarning( ClientNotification, "The provided relationship type is not in the database." ),
PropertyNameMissingWarning( ClientNotification, "The provided property name is not in the database" ),
UnboundedPatternWarning( ClientNotification, "The provided pattern is unbounded, consider adding an upper limit to the number of node hops." );
private final Code code;
@Override
public Code code()
{
return code;
}
Statement( Classification classification, String description )
{
this.code = new Code( classification, this, description );
}
}
enum Schema implements Status
{
/** A constraint in the database was violated by the query. */
ConstraintViolation( ClientError, "A constraint imposed by the database was violated." ),
NoSuchIndex( ClientError, "The request (directly or indirectly) referred to an index that does not exist." ),
NoSuchConstraint( ClientError, "The request (directly or indirectly) referred to a constraint that does " +
"not exist." ),
IndexCreationFailure( DatabaseError, "Failed to create an index."),
ConstraintAlreadyExists( ClientError, "Unable to perform operation because it would clash with a pre-existing" +
" constraint." ),
IndexAlreadyExists( ClientError, "Unable to perform operation because it would clash with a pre-existing " +
"index." ),
IndexDropFailure( DatabaseError, "The database failed to drop a requested index." ),
ConstraintVerificationFailure( ClientError, "Unable to create constraint because data that exists in the " +
"database violates it." ),
ConstraintCreationFailure( DatabaseError, "Creating a requested constraint failed." ),
ConstraintDropFailure( DatabaseError, "The database failed to drop a requested constraint." ),
IllegalTokenName( ClientError, "A token name, such as a label, relationship type or property key, used is " +
"not valid. Tokens cannot be empty strings and cannot be null." ),
IndexBelongsToConstraint(
ClientError, "A requested operation can not be performed on the specified index because the index is " +
"part of a constraint. If you want to drop the index, for instance, you must drop the constraint." ),
NoSuchLabel( DatabaseError, "The request accessed a label that did not exist." ),
NoSuchPropertyKey( DatabaseError, "The request accessed a property that does not exist." ),
NoSuchRelationshipType( DatabaseError, "The request accessed a relationship type that does not exist." ),
NoSuchSchemaRule( DatabaseError, "The request referred to a schema rule that does not exist." ),
DuplicateSchemaRule( DatabaseError, "The request referred to a schema rule that defined multiple times." ),
LabelLimitReached( ClientError, "The maximum number of labels supported has been reached, no more labels can be created." ),
IndexLimitReached( ClientError, "The maximum number of index entries supported has been reached, no more entities can be indexed." ),
ModifiedConcurrently( TransientError, "The database schema was modified while this transaction was running, the transaction should be retried." ),
;
private final Code code;
@Override
public Code code()
{
return code;
}
Schema( Classification classification, String description )
{
this.code = new Code( classification, this, description );
}
}
enum LegacyIndex implements Status
{
NoSuchIndex( ClientError, "The request (directly or indirectly) referred to a index that does not exist." )
;
private final Code code;
@Override
public Code code()
{
return code;
}
LegacyIndex( Classification classification, String description )
{
this.code = new Code( classification, this, description );
}
}
enum Security implements Status
{
// client
AuthenticationFailed( ClientError, "The client provided an incorrect username and/or password." ),
AuthorizationFailed( ClientError, "The client does not have privileges to perform the operation requested." ),
AuthenticationRateLimit( ClientError, "The client has provided incorrect authentication details too many times in a row." ),
ModifiedConcurrently( TransientError, "The user was modified concurrently to this request." ),
EncryptionRequired( ClientError, "A TLS encrypted connection is required." );
private final Code code;
@Override
public Code code()
{
return code;
}
Security( Classification classification, String description )
{
this.code = new Code( classification, this, description );
}
}
enum General implements Status
{
ReadOnly( ClientError, "This is a read only database, writing or modifying the database is not allowed." ),
// database
FailedIndex( DatabaseError, "The request (directly or indirectly) referred to an index that is in a failed " +
"state. The index needs to be dropped and recreated manually." ),
UnknownFailure( DatabaseError, "An unknown failure occurred." ),
DatabaseUnavailable( TransientError, "The database is not currently available to serve your request, refer to the database logs for more details. Retrying your request at a later time may succeed." ),
CorruptSchemaRule( DatabaseError, "A malformed schema rule was encountered. Please contact your support representative." ),
;
private final Code code;
@Override
public Code code()
{
return code;
}
General( Classification classification, String description )
{
this.code = new Code( classification, this, description );
}
}
Code code();
class Code
{
public static Collection<Status> all()
{
Collection<Status> result = new ArrayList<>();
for ( Class<?> child : Status.class.getDeclaredClasses() )
{
if ( child.isEnum() && Status.class.isAssignableFrom( child ) )
{
@SuppressWarnings("unchecked")
Class<? extends Status> statusType = (Class<? extends Status>) child;
Collections.addAll( result, statusType.getEnumConstants() );
}
}
return result;
}
private final Classification classification;
private final String description;
private final String category;
private final String title;
<C extends Enum<C> & Status> Code( Classification classification, C categoryAndTitle, String description )
{
this.classification = classification;
this.category = categoryAndTitle.getDeclaringClass().getSimpleName();
this.title = categoryAndTitle.name();
this.description = description;
}
@Override
public String toString()
{
return "Status.Code[" + serialize() + "]";
}
/**
* The portable, serialized status code. This will always be in the format:
*
* <pre>
* Neo.[Classification].[Category].[Title]
* </pre>
* @return
*/
public final String serialize()
{
return format( "Neo.%s.%s.%s", classification, category, title );
}
public final String description()
{
return description;
}
public Classification classification()
{
return classification;
}
@Override
public boolean equals( Object o )
{
if ( this == o )
{
return true;
}
if ( o == null || getClass() != o.getClass() )
{
return false;
}
Code code = (Code) o;
if ( !category.equals( code.category ) )
{
return false;
}
if ( classification != code.classification )
{
return false;
}
if ( !title.equals( code.title ) )
{
return false;
}
return true;
}
@Override
public int hashCode()
{
int result = classification.hashCode();
result = 31 * result + category.hashCode();
result = 31 * result + title.hashCode();
return result;
}
}
enum Classification
{
/** The Client sent a bad request - changing the request might yield a successful outcome. */
ClientError( TransactionEffect.ROLLBACK, PublishingPolicy.PUBLISHABLE,
"The Client sent a bad request - changing the request might yield a successful outcome."),
/** There are notifications about the request sent by the client.*/
ClientNotification( TransactionEffect.NONE, PublishingPolicy.PUBLISHABLE,
"There are notifications about the request sent by the client." ),
/** The database failed to service the request. */
DatabaseError( TransactionEffect.ROLLBACK, PublishingPolicy.INTERNAL,
"The database failed to service the request. " ),
/** The database cannot service the request right now, retrying later might yield a successful outcome. */
TransientError( TransactionEffect.ROLLBACK, PublishingPolicy.PUBLISHABLE,
"The database cannot service the request right now, retrying later might yield a successful outcome. "),
;
private enum TransactionEffect
{
ROLLBACK, NONE,
}
/**
* A PUBLISHABLE error/warning is one which allows sending a meaningful error message to
* the client with the expectation that the user will be able to take any necessary action
* to resolve the error. INTERNAL errors are more sensitive and may not be resolvable by
* the user - these will be logged and a only a reference will be forwarded to the user.
* This is a security feature to avoid leaking potentially sensitive information to end
* users.
*/
private enum PublishingPolicy
{
PUBLISHABLE, INTERNAL,
}
private final boolean rollbackTransaction;
private final boolean publishable;
private final String description;
Classification( TransactionEffect transactionEffect, PublishingPolicy publishingPolicy, String description )
{
this.description = description;
this.publishable = publishingPolicy == PublishingPolicy.PUBLISHABLE;
this.rollbackTransaction = transactionEffect == TransactionEffect.ROLLBACK;
}
public boolean rollbackTransaction()
{
return rollbackTransaction;
}
public boolean publishable()
{
return publishable;
}
public String description()
{
return description;
}
}
interface HasStatus
{
Status status();
}
}