Skip to content

Commit 2baf9dd

Browse files
authored
Add GqlStatus Finder Functions (#1314)
1 parent 0a42233 commit 2baf9dd

File tree

3 files changed

+90
-0
lines changed

3 files changed

+90
-0
lines changed

packages/core/src/error.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,34 @@ class GQLError extends Error {
150150
this.name = 'GQLError'
151151
}
152152

153+
/**
154+
* Returns whether a given GqlStatus code can be found in the cause chain of the error (including the error itself).
155+
*
156+
* @param {string} gqlStatus the GqlStatus code to find
157+
* @returns {boolean}
158+
*/
159+
containsGqlCause (gqlStatus: string): boolean {
160+
return this.findByGqlStatus(gqlStatus) !== undefined
161+
}
162+
163+
/**
164+
* Returns the first error in the cause chain (including the error itself) with a given GqlStatus code.
165+
* Returns undefined if the GqlStatus code is not present anywhere in the chain.
166+
*
167+
* @param {string} gqlStatus the GqlStatus code to find
168+
* @returns {GQLError | Neo4jError | undefined}
169+
*/
170+
findByGqlStatus (gqlStatus: string): GQLError | Neo4jError | undefined {
171+
if (this.gqlStatus === gqlStatus) {
172+
return this
173+
}
174+
if (this.cause !== undefined && (this.cause instanceof GQLError || this.cause instanceof Neo4jError)) {
175+
return this.cause.findByGqlStatus(gqlStatus)
176+
}
177+
178+
return undefined
179+
}
180+
153181
/**
154182
* The json string representation of the diagnostic record.
155183
* The goal of this method is provide a serialized object for human inspection.

packages/core/test/error.test.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
*/
1717
import {
1818
Neo4jError,
19+
GQLError,
1920
isRetryableError,
2021
newError,
2122
newGQLError,
@@ -201,8 +202,41 @@ describe('Neo4jError', () => {
201202
expect(Neo4jError.isRetryable(error)).toBe(false)
202203
})
203204
})
205+
206+
describe('.containsGqlCause()', () => {
207+
it.each([
208+
[createNestedErrors(0), 'cause0', false],
209+
[createNestedErrors(0), 'root', true],
210+
[createNestedErrors(10), 'cause8', true],
211+
[createNestedErrors(10), 'cause11', false]
212+
])('should correctly identify if GQLStatus is present in cause chain', (error, status, expectedResult) => {
213+
expect(error.containsGqlCause(status)).toBe(expectedResult)
214+
})
215+
})
216+
217+
describe('.findByGqlStatus()', () => {
218+
it.each([
219+
[createNestedErrors(0), 'cause0', undefined],
220+
[createNestedErrors(0), 'root', 'root'],
221+
[createNestedErrors(10), 'cause8', '8cause'],
222+
[createNestedErrors(10), 'cause11', undefined]
223+
])('should correctly find error in cause chain', (error, status, expectedMessage) => {
224+
expect(error.findByGqlStatus(status)?.message).toBe(expectedMessage)
225+
})
226+
})
204227
})
205228

229+
function createNestedErrors (causes: number): Neo4jError {
230+
const error = new Neo4jError('root', '', 'root', '')
231+
let current: Neo4jError | GQLError = error
232+
for (let i = 0; i < causes; i++) {
233+
const cause = new GQLError(i.toString() + 'cause', 'cause' + i.toString(), '')
234+
current.cause = cause
235+
current = cause
236+
}
237+
return error
238+
}
239+
206240
function getRetryableErrorsFixture (): Array<[Neo4jError]> {
207241
return getRetryableCodes().map(code => [newError('message', code)])
208242
}

packages/neo4j-driver-deno/lib/core/error.ts

Lines changed: 28 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)