-
Notifications
You must be signed in to change notification settings - Fork 151
Fix serialization of broken objects in the driver logs #983
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/** | ||
* Copyright (c) "Neo4j" | ||
* Neo4j Sweden AB [http://neo4j.com] | ||
* | ||
* This file is part of Neo4j. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
// eslint-disable-next-line @typescript-eslint/naming-convention | ||
const __isBrokenObject__ = '__isBrokenObject__' | ||
// eslint-disable-next-line @typescript-eslint/naming-convention | ||
const __reason__ = '__reason__' | ||
|
||
/** | ||
* Creates a object on which all method calls will throw the given error | ||
* | ||
* @param {Error} error The error | ||
* @param {any} object The object. Default: {} | ||
* @returns {any} A broken object | ||
*/ | ||
function createBrokenObject<T extends object> (error: Error, object: any = {}): T { | ||
const fail: <T>() => T = () => { | ||
throw error | ||
} | ||
|
||
return new Proxy(object, { | ||
get: (_: T, p: string | Symbol): any => { | ||
if (p === __isBrokenObject__) { | ||
return true | ||
} else if (p === __reason__) { | ||
return error | ||
} else if (p === 'toJSON') { | ||
return undefined | ||
} | ||
fail() | ||
}, | ||
set: fail, | ||
apply: fail, | ||
construct: fail, | ||
defineProperty: fail, | ||
deleteProperty: fail, | ||
getOwnPropertyDescriptor: fail, | ||
getPrototypeOf: fail, | ||
has: fail, | ||
isExtensible: fail, | ||
ownKeys: fail, | ||
preventExtensions: fail, | ||
setPrototypeOf: fail | ||
}) | ||
} | ||
|
||
/** | ||
* Verifies if it is a Broken Object | ||
* @param {any} object The object | ||
* @returns {boolean} If it was created with createBrokenObject | ||
*/ | ||
function isBrokenObject (object: any): boolean { | ||
return object !== null && typeof object === 'object' && object[__isBrokenObject__] === true | ||
} | ||
|
||
/** | ||
* Returns if the reason the object is broken. | ||
* | ||
* This method should only be called with instances create with {@link createBrokenObject} | ||
* | ||
* @param {any} object The object | ||
* @returns {Error} The reason the object is broken | ||
*/ | ||
function getBrokenObjectReason (object: any): Error { | ||
return object[__reason__] | ||
} | ||
|
||
export { | ||
createBrokenObject, | ||
isBrokenObject, | ||
getBrokenObjectReason | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`json .stringify should handle BigInt 1`] = `"\\"42n\\""`; | ||
|
||
exports[`json .stringify should handle BigInt in a list 1`] = `"[\\"42n\\",\\"-24n\\"]"`; | ||
|
||
exports[`json .stringify should handle BigInt in a object 1`] = `"{\\"theResponse\\":\\"42n\\"}"`; | ||
|
||
exports[`json .stringify should handle objects created with createBrokenObject 1`] = `"{\\"__isBrokenObject__\\":true,\\"__reason__\\":{\\"code\\":\\"N/A\\",\\"name\\":\\"Neo4jError\\"}}"`; | ||
|
||
exports[`json .stringify should handle objects created with createBrokenObject in list 1`] = `"[{\\"__isBrokenObject__\\":true,\\"__reason__\\":{\\"code\\":\\"N/A\\",\\"name\\":\\"Neo4jError\\"}}]"`; | ||
|
||
exports[`json .stringify should handle objects created with createBrokenObject inside other object 1`] = `"{\\"number\\":1,\\"broken\\":{\\"__isBrokenObject__\\":true,\\"__reason__\\":{\\"code\\":\\"N/A\\",\\"name\\":\\"Neo4jError\\"}}}"`; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/** | ||
* Copyright (c) "Neo4j" | ||
* Neo4j Sweden AB [http://neo4j.com] | ||
* | ||
* This file is part of Neo4j. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
import { newError } from '../../src' | ||
import { | ||
createBrokenObject, | ||
isBrokenObject, | ||
getBrokenObjectReason | ||
} from '../../src/internal/object-util' | ||
|
||
describe('isBrokenObject', () => { | ||
it('should return true when object created with createBrokenObject', () => { | ||
const object = createBrokenObject(newError('error'), {}) | ||
|
||
expect(isBrokenObject(object)).toBe(true) | ||
}) | ||
|
||
it('should return false for regular objects', () => { | ||
const object = {} | ||
|
||
expect(isBrokenObject(object)).toBe(false) | ||
}) | ||
|
||
it('should return false for non-objects', () => { | ||
expect(isBrokenObject(null)).toBe(false) | ||
expect(isBrokenObject(undefined)).toBe(false) | ||
expect(isBrokenObject(1)).toBe(false) | ||
expect(isBrokenObject(() => {})).toBe(false) | ||
expect(isBrokenObject('string')).toBe(false) | ||
}) | ||
}) | ||
|
||
describe('getBrokenObjectReason', () => { | ||
it('should return the reason the object is broken', () => { | ||
const reason = newError('error') | ||
const object = createBrokenObject(reason, {}) | ||
|
||
expect(getBrokenObjectReason(object)).toBe(reason) | ||
}) | ||
}) | ||
|
||
describe('createBrokenObject', () => { | ||
describe('toJSON', () => { | ||
it('should return undefined', () => { | ||
const reason = newError('error') | ||
const object = createBrokenObject(reason, {}) | ||
|
||
// @ts-expect-error | ||
expect(object.toJSON).toBeUndefined() | ||
}) | ||
}) | ||
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/** | ||
* Copyright (c) "Neo4j" | ||
* Neo4j Sweden AB [http://neo4j.com] | ||
* | ||
* This file is part of Neo4j. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import { json, newError } from '../src' | ||
import { createBrokenObject } from '../src/internal/object-util' | ||
|
||
describe('json', () => { | ||
bigmontz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
describe('.stringify', () => { | ||
it('should handle objects created with createBrokenObject', () => { | ||
const reason = newError('some error') | ||
const broken = createBrokenObject(reason, { }) | ||
|
||
expect(json.stringify(broken)).toMatchSnapshot() | ||
}) | ||
|
||
it('should handle objects created with createBrokenObject in list', () => { | ||
const reason = newError('some error') | ||
const broken = createBrokenObject(reason, { }) | ||
|
||
expect(json.stringify([broken])).toMatchSnapshot() | ||
}) | ||
|
||
it('should handle objects created with createBrokenObject inside other object', () => { | ||
const reason = newError('some error') | ||
const broken = createBrokenObject(reason, { }) | ||
|
||
expect(json.stringify({ | ||
number: 1, | ||
broken | ||
})).toMatchSnapshot() | ||
}) | ||
|
||
it('should handle BigInt', () => { | ||
const bigint = BigInt(42) | ||
|
||
expect(json.stringify(bigint)).toMatchSnapshot() | ||
}) | ||
|
||
it('should handle BigInt in a list', () => { | ||
const bigintList = [BigInt(42), BigInt(-24)] | ||
|
||
expect(json.stringify(bigintList)).toMatchSnapshot() | ||
}) | ||
|
||
it('should handle BigInt in a object', () => { | ||
const bigintInObject = { | ||
theResponse: BigInt(42) | ||
} | ||
|
||
expect(json.stringify(bigintInObject)).toMatchSnapshot() | ||
}) | ||
}) | ||
}) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.