Skip to content

Add GqlStatus Finder Functions #1314

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: 6.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions packages/core/src/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,34 @@ class GQLError extends Error {
this.name = 'GQLError'
}

/**
* Returns whether a given GqlStatus code can be found in the cause chain of the error (including the error itself).
*
* @param {string} gqlStatus the GqlStatus code to find
* @returns {boolean}
*/
containsGqlCause (gqlStatus: string): boolean {
return this.findByGqlStatus(gqlStatus) !== undefined
}

/**
* Returns the first error in the cause chain (including the error itself) with a given GqlStatus code.
* Returns undefined if the GqlStatus code is not present anywhere in the chain.
*
* @param {string} gqlStatus the GqlStatus code to find
* @returns {GQLError | Neo4jError | undefined}
*/
findByGqlStatus (gqlStatus: string): GQLError | Neo4jError | undefined {
if (this.gqlStatus === gqlStatus) {
return this
}
if (this.cause !== undefined && (this.cause instanceof GQLError || this.cause instanceof Neo4jError)) {
return this.cause.findByGqlStatus(gqlStatus)
}

return undefined
}

/**
* The json string representation of the diagnostic record.
* The goal of this method is provide a serialized object for human inspection.
Expand Down
34 changes: 34 additions & 0 deletions packages/core/test/error.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
import {
Neo4jError,
GQLError,
isRetryableError,
newError,
newGQLError,
Expand Down Expand Up @@ -201,8 +202,41 @@ describe('Neo4jError', () => {
expect(Neo4jError.isRetryable(error)).toBe(false)
})
})

describe('.containsGqlCause()', () => {
it.each([
[createNestedErrors(0), 'cause0', false],
[createNestedErrors(0), 'root', true],
[createNestedErrors(10), 'cause8', true],
[createNestedErrors(10), 'cause11', false]
])('should correctly identify if GQLStatus is present in cause chain', (error, status, expectedResult) => {
expect(error.containsGqlCause(status)).toBe(expectedResult)
})
})

describe('.findByGqlStatus()', () => {
it.each([
[createNestedErrors(0), 'cause0', undefined],
[createNestedErrors(0), 'root', 'root'],
[createNestedErrors(10), 'cause8', '8cause'],
[createNestedErrors(10), 'cause11', undefined]
])('should correctly find error in cause chain', (error, status, expectedMessage) => {
expect(error.findByGqlStatus(status)?.message).toBe(expectedMessage)
})
})
})

function createNestedErrors (causes: number): Neo4jError {
const error = new Neo4jError('root', '', 'root', '')
let current: Neo4jError | GQLError = error
for (let i = 0; i < causes; i++) {
const cause = new GQLError(i.toString() + 'cause', 'cause' + i.toString(), '')
current.cause = cause
current = cause
}
return error
}

function getRetryableErrorsFixture (): Array<[Neo4jError]> {
return getRetryableCodes().map(code => [newError('message', code)])
}
Expand Down
28 changes: 28 additions & 0 deletions packages/neo4j-driver-deno/lib/core/error.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.