From cc0bddcc39778791849eb89502178f4cd06ff730 Mon Sep 17 00:00:00 2001 From: Scott Trinh Date: Mon, 6 May 2024 12:25:52 -0400 Subject: [PATCH] Replace `Error.source` with standardized `cause` (#1007) `Error.cause` is now supported across all of our supported platforms. Use that instead of the custom `source` property. --- packages/driver/src/errors/base.ts | 1 - packages/driver/src/rawConn.ts | 10 ++++++---- packages/driver/src/transaction.ts | 4 ++-- packages/driver/test/connection.test.ts | 6 +++--- 4 files changed, 11 insertions(+), 10 deletions(-) diff --git a/packages/driver/src/errors/base.ts b/packages/driver/src/errors/base.ts index 6e8afa3f5..971048b6e 100644 --- a/packages/driver/src/errors/base.ts +++ b/packages/driver/src/errors/base.ts @@ -1,7 +1,6 @@ import { utf8Decoder } from "../primitives/buffer"; export class EdgeDBError extends Error { - source?: Error; protected static tags: object = {}; private _message: string; private _query?: string; diff --git a/packages/driver/src/rawConn.ts b/packages/driver/src/rawConn.ts index 7b8b71d01..39403e133 100644 --- a/packages/driver/src/rawConn.ts +++ b/packages/driver/src/rawConn.ts @@ -152,7 +152,10 @@ export class RawConnection extends BaseRawConnection { this.buffer.takeMessage() && this.buffer.getMessageType() === chars.$E ) { - newErr.source = this._parseErrorMessage(); + Object.defineProperty(newErr, "cause", { + enumerable: false, + value: this._parseErrorMessage(), + }); } this._abortWithError(newErr); @@ -160,9 +163,9 @@ export class RawConnection extends BaseRawConnection { protected _onError(err: Error): void { const newErr = new errors.ClientConnectionClosedError( - `network error: ${err}` + `network error: ${err}`, + { cause: err } ); - newErr.source = err; try { this._abortWaiters(newErr); @@ -335,7 +338,6 @@ export class RawConnection extends BaseRawConnection { ); break; } - err.source = e; throw err; } } finally { diff --git a/packages/driver/src/transaction.ts b/packages/driver/src/transaction.ts index c21e0c879..fe175c290 100644 --- a/packages/driver/src/transaction.ts +++ b/packages/driver/src/transaction.ts @@ -75,9 +75,9 @@ export class Transaction implements Executor { const abortError = this._rawConn.getConnAbortError(); if ( abortError instanceof errors.EdgeDBError && - abortError.source instanceof errors.TransactionTimeoutError + abortError.cause instanceof errors.TransactionTimeoutError ) { - throw abortError.source; + throw abortError.cause; } else { throw abortError; } diff --git a/packages/driver/test/connection.test.ts b/packages/driver/test/connection.test.ts index 2cc884a59..01779d30b 100644 --- a/packages/driver/test/connection.test.ts +++ b/packages/driver/test/connection.test.ts @@ -49,7 +49,7 @@ test("connect: refused", async () => { throw new Error("connection isn't refused"); } catch (e: any) { expect(e).toBeInstanceOf(errors.ClientConnectionClosedError); - expect(e.source.code).toMatch("ECONNREFUSED"); + expect(e.cause.code).toMatch("ECONNREFUSED"); } finally { if (typeof client !== "undefined") { await client.close(); @@ -68,8 +68,8 @@ test("connect: invalid name", async () => { throw new Error("name was resolved"); } catch (e: any) { expect(e).toBeInstanceOf(errors.ClientConnectionClosedError); - expect(e.source.code).toMatch("ENOTFOUND"); - expect(e.source.syscall).toMatch("getaddrinfo"); + expect(e.cause.code).toMatch("ENOTFOUND"); + expect(e.cause.syscall).toMatch("getaddrinfo"); } finally { if (typeof client !== "undefined") { await client.close();