Skip to content

Commit

Permalink
Expose error attributes on fetch connection (#325)
Browse files Browse the repository at this point in the history
  • Loading branch information
jaclarke committed Apr 4, 2022
1 parent 69b9e45 commit a12fbce
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/baseConn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ const OLD_ERROR_CODES = new Map([
export class BaseRawConnection {
protected connected: boolean = false;
protected alwaysUseOptimisticFlow: boolean = false; // XXX
protected exposeErrorAttributes: boolean = false;

protected lastStatus: string | null;

Expand Down Expand Up @@ -243,11 +244,17 @@ export class BaseRawConnection {
this.buffer.readChar(); // ignore severity
const code = this.buffer.readUInt32();
const message = this.buffer.readString();
this._ignoreHeaders(); // ignore attrs

const errorType = resolveErrorCode(OLD_ERROR_CODES.get(code) ?? code);
const err = new errorType(message);

if (this.exposeErrorAttributes) {
(err as any).attrs = this._parseHeaders();
} else {
this._ignoreHeaders(); // ignore attrs
}
this.buffer.finishMessage();

const err = new errorType(message);
return err;
}

Expand Down
1 change: 1 addition & 0 deletions src/fetchConn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ class BaseFetchConnection extends BaseRawConnection {
const conn = new this(config, registry);
conn.connected = true;
conn.alwaysUseOptimisticFlow = true;
conn.exposeErrorAttributes = true;
return conn;
}
}
Expand Down

0 comments on commit a12fbce

Please sign in to comment.