|
| 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 | +// eslint-disable-next-line @typescript-eslint/naming-convention |
| 20 | +const __isBrokenObject__ = '__isBrokenObject__' |
| 21 | +// eslint-disable-next-line @typescript-eslint/naming-convention |
| 22 | +const __reason__ = '__reason__' |
| 23 | + |
| 24 | +/** |
| 25 | + * Creates a object which all method call will throw the given error |
| 26 | + * |
| 27 | + * @param {Error} error The error |
| 28 | + * @param {any} object The object. Default: {} |
| 29 | + * @returns {any} A broken object |
| 30 | + */ |
| 31 | +function createBrokenObject<T extends object> (error: Error, object: any = {}): T { |
| 32 | + const fail: <T>() => T = () => { |
| 33 | + throw error |
| 34 | + } |
| 35 | + |
| 36 | + return new Proxy(object, { |
| 37 | + get: (_: T, p: string | Symbol): any => { |
| 38 | + if (p === __isBrokenObject__) { |
| 39 | + return true |
| 40 | + } else if (p === __reason__) { |
| 41 | + return error |
| 42 | + } else if (p === 'toJSON') { |
| 43 | + return undefined |
| 44 | + } |
| 45 | + fail() |
| 46 | + }, |
| 47 | + set: fail, |
| 48 | + apply: fail, |
| 49 | + construct: fail, |
| 50 | + defineProperty: fail, |
| 51 | + deleteProperty: fail, |
| 52 | + getOwnPropertyDescriptor: fail, |
| 53 | + getPrototypeOf: fail, |
| 54 | + has: fail, |
| 55 | + isExtensible: fail, |
| 56 | + ownKeys: fail, |
| 57 | + preventExtensions: fail, |
| 58 | + setPrototypeOf: fail |
| 59 | + }) |
| 60 | +} |
| 61 | + |
| 62 | +/** |
| 63 | + * Verifies if it is a Broken Object |
| 64 | + * @param {any} object The object |
| 65 | + * @returns {boolean} If it was created with createBrokenObject |
| 66 | + */ |
| 67 | +function isBrokenObject (object: any): boolean { |
| 68 | + return object !== null && typeof object === 'object' && object[__isBrokenObject__] === true |
| 69 | +} |
| 70 | + |
| 71 | +/** |
| 72 | + * Returns if the reason the object is broken. |
| 73 | + * |
| 74 | + * This method should only be called with instances create with {@link createBrokenObject} |
| 75 | + * |
| 76 | + * @param {any} object The object |
| 77 | + * @returns {Error} The reason the object is broken |
| 78 | + */ |
| 79 | +function getBrokenObjectReason (object: any): Error { |
| 80 | + return object[__reason__] |
| 81 | +} |
| 82 | + |
| 83 | +export { |
| 84 | + createBrokenObject, |
| 85 | + isBrokenObject, |
| 86 | + getBrokenObjectReason |
| 87 | +} |
0 commit comments