|
| 1 | +/** |
| 2 | + * Copyright (c) "Neo4j" |
| 3 | + * Neo4j Sweden AB [http://neo4j.com] |
| 4 | + * |
| 5 | + * This file is part of Neo4j. |
| 6 | + * |
| 7 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | + * you may not use this file except in compliance with the License. |
| 9 | + * You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, software |
| 14 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | + * See the License for the specific language governing permissions and |
| 17 | + * limitations under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +import { Neo4jError, SERVICE_UNAVAILABLE, SESSION_EXPIRED } from '../error' |
| 21 | + |
| 22 | +/** |
| 23 | + * Verified error and returns if it could be retried or not |
| 24 | + * |
| 25 | + * @param _error The error |
| 26 | + * @returns If the transaction could be retried. |
| 27 | + */ |
| 28 | +function canRetryOn (_error: any): boolean { |
| 29 | + return ( |
| 30 | + _error && |
| 31 | + _error instanceof Neo4jError && |
| 32 | + _error.code && |
| 33 | + (_error.code === SERVICE_UNAVAILABLE || |
| 34 | + _error.code === SESSION_EXPIRED || |
| 35 | + _isAuthorizationExpired(_error) || |
| 36 | + _isTransientError(_error)) |
| 37 | + ) |
| 38 | +} |
| 39 | + |
| 40 | +function _isTransientError (error: Neo4jError): boolean { |
| 41 | + // Retries should not happen when transaction was explicitly terminated by the user. |
| 42 | + // Termination of transaction might result in two different error codes depending on where it was |
| 43 | + // terminated. These are really client errors but classification on the server is not entirely correct and |
| 44 | + // they are classified as transient. |
| 45 | + |
| 46 | + const code = error.code |
| 47 | + if (code.indexOf('TransientError') >= 0) { |
| 48 | + if ( |
| 49 | + code === 'Neo.TransientError.Transaction.Terminated' || |
| 50 | + code === 'Neo.TransientError.Transaction.LockClientStopped' |
| 51 | + ) { |
| 52 | + return false |
| 53 | + } |
| 54 | + return true |
| 55 | + } |
| 56 | + return false |
| 57 | +} |
| 58 | + |
| 59 | +function _isAuthorizationExpired (error: Neo4jError): boolean { |
| 60 | + return error.code === 'Neo.ClientError.Security.AuthorizationExpired' |
| 61 | +} |
| 62 | + |
| 63 | +export { canRetryOn } |
0 commit comments