Skip to content

Commit f35e44c

Browse files
committed
Handle SecurtyError instead of Auth errors
1 parent 8cf7e4e commit f35e44c

File tree

5 files changed

+35
-54
lines changed

5 files changed

+35
-54
lines changed

packages/bolt-connection/src/connection-provider/connection-provider-direct.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ export default class DirectConnectionProvider extends PooledConnectionProvider {
5050
async acquireConnection ({ accessMode, database, bookmarks, auth, forceReAuth } = {}) {
5151
const databaseSpecificErrorHandler = ConnectionErrorHandler.create({
5252
errorCode: SERVICE_UNAVAILABLE,
53-
handleAuthorizationExpired: (error, address, conn) =>
54-
this._handleAuthorizationExpired(error, address, conn, database)
53+
handleSecurityError: (error, address, conn) =>
54+
this._handleSecurityError(error, address, conn, database)
5555
})
5656

5757
const connection = await this._connectionPool.acquire({ auth, forceReAuth }, this._address)
@@ -68,12 +68,12 @@ export default class DirectConnectionProvider extends PooledConnectionProvider {
6868
return new DelegateConnection(connection, databaseSpecificErrorHandler)
6969
}
7070

71-
_handleAuthorizationExpired (error, address, connection, database) {
71+
_handleSecurityError (error, address, connection, database) {
7272
this._log.warn(
7373
`Direct driver ${this._id} will close connection to ${address} for database '${database}' because of an error ${error.code} '${error.message}'`
7474
)
7575

76-
return super._handleAuthorizationExpired(error, address, connection)
76+
return super._handleSecurityError(error, address, connection)
7777
}
7878

7979
async _hasProtocolVersion (versionPredicate) {

packages/bolt-connection/src/connection-provider/connection-provider-pooled.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ export default class PooledConnectionProvider extends ConnectionProvider {
223223
conn._updateCurrentObserver()
224224
}
225225

226-
_handleAuthorizationExpired (error, address, connection) {
226+
_handleSecurityError (error, address, connection) {
227227
const handled = this._authenticationProvider.handleError({ connection, code: error.code })
228228

229229
if (handled) {

packages/bolt-connection/src/connection-provider/connection-provider-routing.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,12 +116,12 @@ export default class RoutingConnectionProvider extends PooledConnectionProvider
116116
return error
117117
}
118118

119-
_handleAuthorizationExpired (error, address, connection, database) {
119+
_handleSecurityError (error, address, connection, database) {
120120
this._log.warn(
121121
`Routing driver ${this._id} will close connections to ${address} for database '${database}' because of an error ${error.code} '${error.message}'`
122122
)
123123

124-
return super._handleAuthorizationExpired(error, address, connection, database)
124+
return super._handleSecurityError(error, address, connection, database)
125125
}
126126

127127
_handleWriteFailure (error, address, database) {
@@ -150,7 +150,7 @@ export default class RoutingConnectionProvider extends PooledConnectionProvider
150150
(error, address) => this._handleUnavailability(error, address, context.database),
151151
(error, address) => this._handleWriteFailure(error, address, context.database),
152152
(error, address, conn) =>
153-
this._handleAuthorizationExpired(error, address, conn, context.database)
153+
this._handleSecurityError(error, address, conn, context.database)
154154
)
155155

156156
const routingTable = await this._freshRoutingTable({
@@ -584,7 +584,7 @@ export default class RoutingConnectionProvider extends PooledConnectionProvider
584584

585585
const databaseSpecificErrorHandler = ConnectionErrorHandler.create({
586586
errorCode: SESSION_EXPIRED,
587-
handleAuthorizationExpired: (error, address, conn) => this._handleAuthorizationExpired(error, address, conn)
587+
handleSecurityError: (error, address, conn) => this._handleSecurityError(error, address, conn)
588588
})
589589

590590
const delegateConnection = !connection._sticky

packages/bolt-connection/src/connection/connection-error-handler.js

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,25 +26,25 @@ export default class ConnectionErrorHandler {
2626
errorCode,
2727
handleUnavailability,
2828
handleWriteFailure,
29-
handleAuthorizationExpired
29+
handleSecurityError
3030
) {
3131
this._errorCode = errorCode
3232
this._handleUnavailability = handleUnavailability || noOpHandler
3333
this._handleWriteFailure = handleWriteFailure || noOpHandler
34-
this._handleAuthorizationExpired = handleAuthorizationExpired || noOpHandler
34+
this._handleSecurityError = handleSecurityError || noOpHandler
3535
}
3636

3737
static create ({
3838
errorCode,
3939
handleUnavailability,
4040
handleWriteFailure,
41-
handleAuthorizationExpired
41+
handleSecurityError
4242
}) {
4343
return new ConnectionErrorHandler(
4444
errorCode,
4545
handleUnavailability,
4646
handleWriteFailure,
47-
handleAuthorizationExpired
47+
handleSecurityError
4848
)
4949
}
5050

@@ -63,8 +63,8 @@ export default class ConnectionErrorHandler {
6363
* @return {Neo4jError} new error that should be propagated to the user.
6464
*/
6565
handleAndTransformError (error, address, connection) {
66-
if (isAutorizationExpiredError(error)) {
67-
return this._handleAuthorizationExpired(error, address, connection)
66+
if (isSecurityError(error)) {
67+
return this._handleSecurityError(error, address, connection)
6868
}
6969
if (isAvailabilityError(error)) {
7070
return this._handleUnavailability(error, address, connection)
@@ -76,11 +76,10 @@ export default class ConnectionErrorHandler {
7676
}
7777
}
7878

79-
function isAutorizationExpiredError (error) {
80-
return error && (
81-
error.code === 'Neo.ClientError.Security.AuthorizationExpired' ||
82-
error.code === 'Neo.ClientError.Security.TokenExpired'
83-
)
79+
function isSecurityError (error) {
80+
return error != null &&
81+
error.code != null &&
82+
error.code.startsWith('Neo.ClientError.Security.')
8483
}
8584

8685
function isAvailabilityError (error) {

packages/bolt-connection/test/connection/connection-error-handler.test.js

Lines changed: 16 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -79,35 +79,7 @@ describe('#unit ConnectionErrorHandler', () => {
7979
])
8080
})
8181

82-
it('should handle and transform authorization expired error', () => {
83-
const errors = []
84-
const addresses = []
85-
const transformedError = newError('Message', 'Code')
86-
const handler = ConnectionErrorHandler.create({
87-
errorCode: SERVICE_UNAVAILABLE,
88-
handleAuthorizationExpired: (error, address) => {
89-
errors.push(error)
90-
addresses.push(address)
91-
return transformedError
92-
}
93-
})
94-
95-
const error1 = newError(
96-
'C',
97-
'Neo.ClientError.Security.AuthorizationExpired'
98-
)
99-
100-
const errorTransformed1 = handler.handleAndTransformError(
101-
error1,
102-
ServerAddress.fromUrl('localhost:0')
103-
)
104-
105-
expect(errorTransformed1).toEqual(transformedError)
106-
107-
expect(addresses).toEqual([ServerAddress.fromUrl('localhost:0')])
108-
})
109-
110-
it('should return original erro if authorization expired handler is not informed', () => {
82+
it('should return original error if authorization expired handler is not informed', () => {
11183
const errors = []
11284
const addresses = []
11385
const transformedError = newError('Message', 'Code')
@@ -140,13 +112,18 @@ describe('#unit ConnectionErrorHandler', () => {
140112
expect(addresses).toEqual([])
141113
})
142114

143-
it('should handle and transform token expired error', () => {
115+
it.each([
116+
'Neo.ClientError.Security.TokenExpired',
117+
'Neo.ClientError.Security.AuthorizationExpired',
118+
'Neo.ClientError.Security.Unauthorized',
119+
'Neo.ClientError.Security.MadeUp'
120+
])('should handle and transform "%s"', (code) => {
144121
const errors = []
145122
const addresses = []
146123
const transformedError = newError('Message', 'Code')
147124
const handler = ConnectionErrorHandler.create({
148125
errorCode: SERVICE_UNAVAILABLE,
149-
handleAuthorizationExpired: (error, address) => {
126+
handleSecurityError: (error, address) => {
150127
errors.push(error)
151128
addresses.push(address)
152129
return transformedError
@@ -155,7 +132,7 @@ describe('#unit ConnectionErrorHandler', () => {
155132

156133
const error1 = newError(
157134
'C',
158-
'Neo.ClientError.Security.TokenExpired'
135+
code
159136
)
160137

161138
const errorTransformed1 = handler.handleAndTransformError(
@@ -168,7 +145,12 @@ describe('#unit ConnectionErrorHandler', () => {
168145
expect(addresses).toEqual([ServerAddress.fromUrl('localhost:0')])
169146
})
170147

171-
it('should return original erro if token expired handler is not informed', () => {
148+
it.each([
149+
'Neo.ClientError.Security.TokenExpired',
150+
'Neo.ClientError.Security.AuthorizationExpired',
151+
'Neo.ClientError.Security.Unauthorized',
152+
'Neo.ClientError.Security.MadeUp'
153+
])('should return original error code equals "%s" if security error handler is not informed', (code) => {
172154
const errors = []
173155
const addresses = []
174156
const transformedError = newError('Message', 'Code')
@@ -188,7 +170,7 @@ describe('#unit ConnectionErrorHandler', () => {
188170

189171
const error1 = newError(
190172
'C',
191-
'Neo.ClientError.Security.TokenExpired'
173+
code
192174
)
193175

194176
const errorTransformed1 = handler.handleAndTransformError(

0 commit comments

Comments
 (0)