Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix tsc errors related to error type checking #1091

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions src/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
import { buildWsFatalInsight, buildWsSuccessAfterFailureInsight, postInsights } from './insights';
import { ConnectAPIResponse, ConnectionOpen, ExtendableGenerics, DefaultGenerics, UR, LogLevel } from './types';
import { StreamChat } from './client';
import { isAPIError } from 'errors';

// Type guards to check WebSocket error type
const isCloseEvent = (res: WebSocket.CloseEvent | WebSocket.Data | WebSocket.ErrorEvent): res is WebSocket.CloseEvent =>
Expand Down Expand Up @@ -117,6 +118,10 @@ export class StableWSConnection<StreamChatGenerics extends ExtendableGenerics =
this.isHealthy = false;
this.consecutiveFailures += 1;

if (!isAPIError(error)) {
throw error;
}

if (error.code === chatCodes.TOKEN_EXPIRED && !this.client.tokenManager.isStatic()) {
this._log('connect() - WS failure due to expired token, so going to try to reload token and reconnect');
this._reconnect({ refreshToken: true });
Expand All @@ -125,7 +130,7 @@ export class StableWSConnection<StreamChatGenerics extends ExtendableGenerics =
throw new Error(
JSON.stringify({
code: error.code,
StatusCode: error.StatusCode,
StatusCode: error.code,
message: error.message,
isWSFailure: error.isWSFailure,
}),
Expand All @@ -149,11 +154,14 @@ export class StableWSConnection<StreamChatGenerics extends ExtendableGenerics =
try {
return await this.connectionOpen;
} catch (error) {
if (!isAPIError(error)) {
continue;
}
if (i === timeout) {
throw new Error(
JSON.stringify({
code: error.code,
StatusCode: error.StatusCode,
StatusCode: error.code,
message: error.message,
isWSFailure: error.isWSFailure,
}),
Expand Down Expand Up @@ -300,7 +308,10 @@ export class StableWSConnection<StreamChatGenerics extends ExtendableGenerics =
}
} catch (err) {
this.isConnecting = false;
this._log(`_connect() - Error - `, err);
if (isAPIError(err)) {
this._log(`_connect() - Error - `, { code: err.code });
}

if (this.client.options.enableInsights) {
this.client.insightMetrics.wsConsecutiveFailures++;
this.client.insightMetrics.wsTotalFailures++;
Expand Down Expand Up @@ -369,6 +380,9 @@ export class StableWSConnection<StreamChatGenerics extends ExtendableGenerics =
} catch (error) {
this.isHealthy = false;
this.consecutiveFailures += 1;
if (!isAPIError(error)) {
throw error;
}
if (error.code === chatCodes.TOKEN_EXPIRED && !this.client.tokenManager.isStatic()) {
this._log('_reconnect() - WS failure due to expired token, so going to try to reload token and reconnect');

Expand Down
39 changes: 29 additions & 10 deletions src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,28 +33,47 @@ export const APIErrorCodes: Record<string, { name: string; retryable: boolean }>

type APIError = Error & { code: number; isWSFailure?: boolean };

export function isAPIError(error: Error): error is APIError {
return (error as APIError).code !== undefined;
export function isAPIError(error: unknown): error is APIError {
return error instanceof Error && 'code' in error && typeof error.code === 'number';
}

export function isErrorRetryable(error: APIError) {
if (!error.code) return false;
export function isErrorRetryable(error: unknown) {
if (!isAPIError(error)) return false;
const err = APIErrorCodes[`${error.code}`];
if (!err) return false;
return err.retryable;
}

export function isConnectionIDError(error: APIError) {
return error.code === 46; // ConnectionIDNotFoundError
export function isConnectionIDError(error: unknown) {
return isAPIError(error) && error.code === 46; // ConnectionIDNotFoundError
}

export function isWSFailure(err: APIError): boolean {
if (typeof err.isWSFailure === 'boolean') {
return err.isWSFailure;
interface ErrorWithMessage {
message: string;
}

export function isErrorWithMessage(error: unknown): error is ErrorWithMessage {
return (
typeof error === 'object' &&
error !== null &&
'message' in error &&
typeof (error as Record<string, unknown>).message === 'string'
);
}

export function isWSFailure(err: unknown): boolean {
if (isAPIError(err)) {
return err.isWSFailure ?? false;
}

try {
return JSON.parse(err.message).isWSFailure;
if (isErrorWithMessage(err)) {
const message = JSON.parse(err.message);
if ('isWSFailure' in message && typeof message.isWSFailure === 'boolean') {
return message.isWSFailure;
}
}
return false;
} catch (_) {
return false;
}
Expand Down