Skip to content

Commit

Permalink
Apply CodeRabbit review suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
chacha912 committed Oct 24, 2024
1 parent 840cf00 commit acd5ca6
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 61 deletions.
2 changes: 1 addition & 1 deletion packages/sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
"@bufbuild/buf": "^1.28.1",
"@bufbuild/protoc-gen-es": "^1.6.0",
"@connectrpc/protoc-gen-connect-es": "^1.4.0",
"@types/express": "^5.0.0",
"@types/express": "^4.17.21",
"@types/google-protobuf": "^3.15.5",
"@types/long": "^4.0.1",
"@typescript-eslint/eslint-plugin": "^6.21.0",
Expand Down
29 changes: 13 additions & 16 deletions packages/sdk/src/api/converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -803,26 +803,16 @@ function toChangePack(pack: ChangePack<Indexable>): PbChangePack {
});
}

/**
* `errorCodeOf` returns the error code of the given connect error.
*/
export function errorCodeOf(error: ConnectError): string {
// NOTE(hackerwins): Currently, we only use the first detail to represent the
// error code.
const infos = error.findDetails(ErrorInfo);
for (const info of infos) {
if (info.metadata.code) {
return info.metadata.code;
}
}

return '';
}

/**
* `errorMetadataOf` returns the error code of the given connect error.
*/
export function errorMetadataOf(error: ConnectError): Record<string, string> {
if (!(error instanceof ConnectError)) {
return {};
}

// NOTE(chacha912): Currently, we only use the first detail to represent the
// error metadata.
const infos = error.findDetails(ErrorInfo);
for (const info of infos) {
return info.metadata;
Expand All @@ -831,6 +821,13 @@ export function errorMetadataOf(error: ConnectError): Record<string, string> {
return {};
}

/**
* `errorCodeOf` returns the error code of the given connect error.
*/
export function errorCodeOf(error: ConnectError): string {
return errorMetadataOf(error).code ?? '';
}

/**
* `fromChangeID` converts the given Protobuf format to model format.
*/
Expand Down
20 changes: 4 additions & 16 deletions packages/sdk/src/client/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -462,10 +462,7 @@ export class Client {
}

if (await this.handleConnectError(err)) {
if (
err instanceof ConnectError &&
errorCodeOf(err) === Code.ErrUnauthenticated
) {
if (errorCodeOf(err) === Code.ErrUnauthenticated) {
doc.publish([
{
type: DocEventType.AuthError,
Expand Down Expand Up @@ -554,10 +551,7 @@ export class Client {
}

if (await this.handleConnectError(err)) {
if (
err instanceof ConnectError &&
errorCodeOf(err) === Code.ErrUnauthenticated
) {
if (errorCodeOf(err) === Code.ErrUnauthenticated) {
doc.publish([
{
type: DocEventType.AuthError,
Expand Down Expand Up @@ -960,10 +954,7 @@ export class Client {
logger.debug(`[WD] c:"${this.getKey()}" unwatches`);

if (await this.handleConnectError(err)) {
if (
err instanceof ConnectError &&
errorCodeOf(err) === Code.ErrUnauthenticated
) {
if (errorCodeOf(err) === Code.ErrUnauthenticated) {
if (retryCount >= this.maxRequestRetries) {
logger.error(
`[WD] c:"${this.getKey()}" max retries (${
Expand Down Expand Up @@ -1109,10 +1100,7 @@ export class Client {
}

if (await this.handleConnectError(err)) {
if (
err instanceof ConnectError &&
errorCodeOf(err) === Code.ErrUnauthenticated
) {
if (errorCodeOf(err) === Code.ErrUnauthenticated) {
doc.publish([
{
type: DocEventType.AuthError,
Expand Down
56 changes: 28 additions & 28 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit acd5ca6

Please sign in to comment.