Skip to content

Commit

Permalink
Replace Error.source with standardized cause (#1007)
Browse files Browse the repository at this point in the history
`Error.cause` is now supported across all of our supported platforms.
Use that instead of the custom `source` property.
  • Loading branch information
scotttrinh authored May 6, 2024
1 parent 9c4b038 commit cc0bddc
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 10 deletions.
1 change: 0 additions & 1 deletion packages/driver/src/errors/base.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
10 changes: 6 additions & 4 deletions packages/driver/src/rawConn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,17 +152,20 @@ 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);
}

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);
Expand Down Expand Up @@ -335,7 +338,6 @@ export class RawConnection extends BaseRawConnection {
);
break;
}
err.source = e;
throw err;
}
} finally {
Expand Down
4 changes: 2 additions & 2 deletions packages/driver/src/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
6 changes: 3 additions & 3 deletions packages/driver/test/connection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
Expand Down

0 comments on commit cc0bddc

Please sign in to comment.