diff --git a/packages/sdk/package.json b/packages/sdk/package.json index 0cd9af350..53457da3c 100644 --- a/packages/sdk/package.json +++ b/packages/sdk/package.json @@ -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", diff --git a/packages/sdk/src/api/converter.ts b/packages/sdk/src/api/converter.ts index 3926bace0..250ec2f40 100644 --- a/packages/sdk/src/api/converter.ts +++ b/packages/sdk/src/api/converter.ts @@ -803,26 +803,16 @@ function toChangePack(pack: ChangePack): 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 { + 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; @@ -831,6 +821,13 @@ export function errorMetadataOf(error: ConnectError): Record { 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. */ diff --git a/packages/sdk/src/client/client.ts b/packages/sdk/src/client/client.ts index d46bf21f4..99f8dbb1d 100644 --- a/packages/sdk/src/client/client.ts +++ b/packages/sdk/src/client/client.ts @@ -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, @@ -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, @@ -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 (${ @@ -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, diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index b261a0b2e..e6e9adc0d 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -298,7 +298,7 @@ importers: version: 2.4.2 ts-loader: specifier: ^9.5.1 - version: 9.5.1(typescript@5.4.2)(webpack@5.93.0(webpack-cli@5.1.4)) + version: 9.5.1(typescript@5.4.2)(webpack@5.93.0) webpack: specifier: ^5.91.0 version: 5.93.0(webpack-cli@5.1.4) @@ -389,8 +389,8 @@ importers: specifier: ^1.4.0 version: 1.4.0(@bufbuild/protoc-gen-es@1.10.0(@bufbuild/protobuf@1.10.0))(@connectrpc/connect@1.4.0(@bufbuild/protobuf@1.10.0)) '@types/express': - specifier: ^5.0.0 - version: 5.0.0 + specifier: ^4.17.21 + version: 4.17.21 '@types/google-protobuf': specifier: ^3.15.5 version: 3.15.5 @@ -423,7 +423,7 @@ importers: version: 2.7.1 ts-node: specifier: ^10.9.1 - version: 10.9.1(@swc/core@1.3.104)(@types/node@20.4.2)(typescript@5.3.3) + version: 10.9.1(@swc/core@1.3.104(@swc/helpers@0.5.2))(@types/node@20.4.2)(typescript@5.3.3) typedoc: specifier: ^0.25.13 version: 0.25.13(typescript@5.3.3) @@ -2942,11 +2942,11 @@ packages: '@types/estree@1.0.5': resolution: {integrity: sha512-/kYRxGDLWzHOB7q+wtSUQlFrtcdUccpfy+X+9iMBpHK8QLLhx2wIPYuS5DYtR9Wa/YlZAbIovy7qVdB1Aq6Lyw==} - '@types/express-serve-static-core@5.0.0': - resolution: {integrity: sha512-AbXMTZGt40T+KON9/Fdxx0B2WK5hsgxcfXJLr5bFpZ7b4JCex2WyQPTEKdXqfHiY5nKKBScZ7yCoO6Pvgxfvnw==} + '@types/express-serve-static-core@4.19.6': + resolution: {integrity: sha512-N4LZ2xG7DatVqhCZzOGb1Yi5lMbXSZcmdLDe9EzSndPV2HpWYWzRbaerl2n27irrm94EPpprqa8KpskPT085+A==} - '@types/express@5.0.0': - resolution: {integrity: sha512-DvZriSMehGHL1ZNLzi6MidnsDhUZM/x2pRdDIKdwbUNqqwHxMlRdkxtn6/EPKyqKpHqTl/4nRZsRNLpZxZRpPQ==} + '@types/express@4.17.21': + resolution: {integrity: sha512-ejlPM315qwLpaQlQDTjPdsUFSc6ZsP4AN6AlWnogPjQ7CVi7PYF3YVz+CY3jE2pwYf7E/7HlDAN0rV2GxTG0HQ==} '@types/filesystem@0.0.36': resolution: {integrity: sha512-vPDXOZuannb9FZdxgHnqSwAG/jvdGM8Wq+6N4D/d80z+D4HWH+bItqsZaVRQykAn6WEVeEkLm2oQigyHtgb0RA==} @@ -10154,17 +10154,17 @@ snapshots: '@types/estree@1.0.5': {} - '@types/express-serve-static-core@5.0.0': + '@types/express-serve-static-core@4.19.6': dependencies: '@types/node': 20.9.0 '@types/qs': 6.9.16 '@types/range-parser': 1.2.7 '@types/send': 0.17.4 - '@types/express@5.0.0': + '@types/express@4.17.21': dependencies: '@types/body-parser': 1.19.5 - '@types/express-serve-static-core': 5.0.0 + '@types/express-serve-static-core': 4.19.6 '@types/qs': 6.9.16 '@types/serve-static': 1.15.7 @@ -10597,17 +10597,17 @@ snapshots: '@webassemblyjs/ast': 1.12.1 '@xtuc/long': 4.2.2 - '@webpack-cli/configtest@2.1.1(webpack-cli@5.1.4(webpack@5.93.0))(webpack@5.93.0(webpack-cli@5.1.4))': + '@webpack-cli/configtest@2.1.1(webpack-cli@5.1.4)(webpack@5.93.0)': dependencies: webpack: 5.93.0(webpack-cli@5.1.4) webpack-cli: 5.1.4(webpack@5.93.0) - '@webpack-cli/info@2.0.2(webpack-cli@5.1.4(webpack@5.93.0))(webpack@5.93.0(webpack-cli@5.1.4))': + '@webpack-cli/info@2.0.2(webpack-cli@5.1.4)(webpack@5.93.0)': dependencies: webpack: 5.93.0(webpack-cli@5.1.4) webpack-cli: 5.1.4(webpack@5.93.0) - '@webpack-cli/serve@2.0.5(webpack-cli@5.1.4(webpack@5.93.0))(webpack@5.93.0(webpack-cli@5.1.4))': + '@webpack-cli/serve@2.0.5(webpack-cli@5.1.4)(webpack@5.93.0)': dependencies: webpack: 5.93.0(webpack-cli@5.1.4) webpack-cli: 5.1.4(webpack@5.93.0) @@ -11608,7 +11608,7 @@ snapshots: debug: 4.3.4 enhanced-resolve: 5.17.1 eslint: 8.19.0 - eslint-module-utils: 2.8.1(@typescript-eslint/parser@6.21.0(eslint@8.19.0)(typescript@5.3.3))(eslint-import-resolver-node@0.3.9)(eslint-import-resolver-typescript@3.6.1(@typescript-eslint/parser@6.21.0(eslint@8.19.0)(typescript@5.3.3))(eslint-import-resolver-node@0.3.9)(eslint-plugin-import@2.29.1)(eslint@8.19.0))(eslint@8.19.0) + eslint-module-utils: 2.8.1(@typescript-eslint/parser@6.21.0(eslint@8.19.0)(typescript@5.3.3))(eslint-import-resolver-node@0.3.9)(eslint-import-resolver-typescript@3.6.1)(eslint@8.19.0) eslint-plugin-import: 2.29.1(@typescript-eslint/parser@6.21.0(eslint@8.19.0)(typescript@5.3.3))(eslint-import-resolver-typescript@3.6.1)(eslint@8.19.0) fast-glob: 3.3.2 get-tsconfig: 4.7.6 @@ -11620,7 +11620,7 @@ snapshots: - eslint-import-resolver-webpack - supports-color - eslint-module-utils@2.8.1(@typescript-eslint/parser@6.21.0(eslint@8.19.0)(typescript@5.3.3))(eslint-import-resolver-node@0.3.9)(eslint-import-resolver-typescript@3.6.1(@typescript-eslint/parser@6.21.0(eslint@8.19.0)(typescript@5.3.3))(eslint-import-resolver-node@0.3.9)(eslint-plugin-import@2.29.1)(eslint@8.19.0))(eslint@8.19.0): + eslint-module-utils@2.8.1(@typescript-eslint/parser@6.21.0(eslint@8.19.0)(typescript@5.3.3))(eslint-import-resolver-node@0.3.9)(eslint-import-resolver-typescript@3.6.1)(eslint@8.19.0): dependencies: debug: 3.2.7 optionalDependencies: @@ -11641,7 +11641,7 @@ snapshots: doctrine: 2.1.0 eslint: 8.19.0 eslint-import-resolver-node: 0.3.9 - eslint-module-utils: 2.8.1(@typescript-eslint/parser@6.21.0(eslint@8.19.0)(typescript@5.3.3))(eslint-import-resolver-node@0.3.9)(eslint-import-resolver-typescript@3.6.1(@typescript-eslint/parser@6.21.0(eslint@8.19.0)(typescript@5.3.3))(eslint-import-resolver-node@0.3.9)(eslint-plugin-import@2.29.1)(eslint@8.19.0))(eslint@8.19.0) + eslint-module-utils: 2.8.1(@typescript-eslint/parser@6.21.0(eslint@8.19.0)(typescript@5.3.3))(eslint-import-resolver-node@0.3.9)(eslint-import-resolver-typescript@3.6.1)(eslint@8.19.0) hasown: 2.0.2 is-core-module: 2.15.0 is-glob: 4.0.3 @@ -14266,7 +14266,7 @@ snapshots: type-fest: 2.19.0 unique-string: 3.0.0 - terser-webpack-plugin@5.3.10(webpack@5.93.0(webpack-cli@5.1.4)): + terser-webpack-plugin@5.3.10(webpack@5.93.0): dependencies: '@jridgewell/trace-mapping': 0.3.25 jest-worker: 27.5.1 @@ -14342,7 +14342,7 @@ snapshots: ts-interface-checker@0.1.13: {} - ts-loader@9.5.1(typescript@5.4.2)(webpack@5.93.0(webpack-cli@5.1.4)): + ts-loader@9.5.1(typescript@5.4.2)(webpack@5.93.0): dependencies: chalk: 4.1.1 enhanced-resolve: 5.17.1 @@ -14352,14 +14352,14 @@ snapshots: typescript: 5.4.2 webpack: 5.93.0(webpack-cli@5.1.4) - ts-node@10.9.1(@swc/core@1.3.104(@swc/helpers@0.5.2))(@types/node@20.9.0)(typescript@5.3.3): + ts-node@10.9.1(@swc/core@1.3.104(@swc/helpers@0.5.2))(@types/node@20.4.2)(typescript@5.3.3): dependencies: '@cspotcode/source-map-support': 0.8.1 '@tsconfig/node10': 1.0.8 '@tsconfig/node12': 1.0.9 '@tsconfig/node14': 1.0.1 '@tsconfig/node16': 1.0.3 - '@types/node': 20.9.0 + '@types/node': 20.4.2 acorn: 8.10.0 acorn-walk: 8.2.0 arg: 4.1.3 @@ -14371,16 +14371,15 @@ snapshots: yn: 3.1.1 optionalDependencies: '@swc/core': 1.3.104(@swc/helpers@0.5.2) - optional: true - ts-node@10.9.1(@swc/core@1.3.104)(@types/node@20.4.2)(typescript@5.3.3): + ts-node@10.9.1(@swc/core@1.3.104(@swc/helpers@0.5.2))(@types/node@20.9.0)(typescript@5.3.3): dependencies: '@cspotcode/source-map-support': 0.8.1 '@tsconfig/node10': 1.0.8 '@tsconfig/node12': 1.0.9 '@tsconfig/node14': 1.0.1 '@tsconfig/node16': 1.0.3 - '@types/node': 20.4.2 + '@types/node': 20.9.0 acorn: 8.10.0 acorn-walk: 8.2.0 arg: 4.1.3 @@ -14392,6 +14391,7 @@ snapshots: yn: 3.1.1 optionalDependencies: '@swc/core': 1.3.104(@swc/helpers@0.5.2) + optional: true tsconfck@3.1.1(typescript@5.3.3): optionalDependencies: @@ -14785,9 +14785,9 @@ snapshots: webpack-cli@5.1.4(webpack@5.93.0): dependencies: '@discoveryjs/json-ext': 0.5.7 - '@webpack-cli/configtest': 2.1.1(webpack-cli@5.1.4(webpack@5.93.0))(webpack@5.93.0(webpack-cli@5.1.4)) - '@webpack-cli/info': 2.0.2(webpack-cli@5.1.4(webpack@5.93.0))(webpack@5.93.0(webpack-cli@5.1.4)) - '@webpack-cli/serve': 2.0.5(webpack-cli@5.1.4(webpack@5.93.0))(webpack@5.93.0(webpack-cli@5.1.4)) + '@webpack-cli/configtest': 2.1.1(webpack-cli@5.1.4)(webpack@5.93.0) + '@webpack-cli/info': 2.0.2(webpack-cli@5.1.4)(webpack@5.93.0) + '@webpack-cli/serve': 2.0.5(webpack-cli@5.1.4)(webpack@5.93.0) colorette: 2.0.20 commander: 10.0.1 cross-spawn: 7.0.3 @@ -14830,7 +14830,7 @@ snapshots: neo-async: 2.6.2 schema-utils: 3.3.0 tapable: 2.2.1 - terser-webpack-plugin: 5.3.10(webpack@5.93.0(webpack-cli@5.1.4)) + terser-webpack-plugin: 5.3.10(webpack@5.93.0) watchpack: 2.4.1 webpack-sources: 3.2.3 optionalDependencies: