From 1a1041712f4dfc9521c2d79454a9b48590637eef Mon Sep 17 00:00:00 2001 From: forehalo Date: Fri, 8 Nov 2024 10:34:15 +0000 Subject: [PATCH] feat(core): track signin failed reason (#8746) fix AF-1568 --- .../frontend/core/src/modules/cloud/error.ts | 4 ++- .../core/src/modules/cloud/services/auth.ts | 28 +++++++++++++++---- packages/frontend/graphql/src/error.ts | 23 +++++++++------ packages/frontend/track/src/events.ts | 2 +- 4 files changed, 41 insertions(+), 16 deletions(-) diff --git a/packages/frontend/core/src/modules/cloud/error.ts b/packages/frontend/core/src/modules/cloud/error.ts index e1e66fc5a2..53201c9495 100644 --- a/packages/frontend/core/src/modules/cloud/error.ts +++ b/packages/frontend/core/src/modules/cloud/error.ts @@ -1,3 +1,5 @@ +import type { UserFriendlyError } from '@affine/graphql'; + export class NetworkError extends Error { constructor( public readonly originError: Error, @@ -14,7 +16,7 @@ export function isNetworkError(error: Error): error is NetworkError { export class BackendError extends Error { constructor( - public readonly originError: Error, + public readonly originError: UserFriendlyError, public readonly status?: number ) { super(`Server error: ${originError.message}`); diff --git a/packages/frontend/core/src/modules/cloud/services/auth.ts b/packages/frontend/core/src/modules/cloud/services/auth.ts index b09266cc3a..e8fd6b5cc6 100644 --- a/packages/frontend/core/src/modules/cloud/services/auth.ts +++ b/packages/frontend/core/src/modules/cloud/services/auth.ts @@ -12,6 +12,7 @@ import { distinctUntilChanged, map, skip } from 'rxjs'; import type { UrlService } from '../../url'; import { type AuthAccountInfo, AuthSession } from '../entities/session'; +import { BackendError } from '../error'; import type { AuthStore } from '../stores/auth'; import type { FetchService } from './fetch'; @@ -111,7 +112,10 @@ export class AuthService extends Service { }, }); } catch (e) { - track.$.$.auth.signInFail({ method: 'magic-link' }); + track.$.$.auth.signInFail({ + method: 'magic-link', + reason: e instanceof BackendError ? e.originError.type : 'unknown', + }); throw e; } } @@ -129,7 +133,10 @@ export class AuthService extends Service { this.session.revalidate(); track.$.$.auth.signedIn({ method: 'magic-link' }); } catch (e) { - track.$.$.auth.signInFail({ method: 'magic-link' }); + track.$.$.auth.signInFail({ + method: 'magic-link', + reason: e instanceof BackendError ? e.originError.type : 'unknown', + }); throw e; } } @@ -166,7 +173,11 @@ export class AuthService extends Service { return url; } catch (e) { - track.$.$.auth.signInFail({ method: 'oauth', provider }); + track.$.$.auth.signInFail({ + method: 'oauth', + provider, + reason: e instanceof BackendError ? e.originError.type : 'unknown', + }); throw e; } } @@ -186,7 +197,11 @@ export class AuthService extends Service { track.$.$.auth.signedIn({ method: 'oauth', provider }); return res.json(); } catch (e) { - track.$.$.auth.signInFail({ method: 'oauth', provider }); + track.$.$.auth.signInFail({ + method: 'oauth', + provider, + reason: e instanceof BackendError ? e.originError.type : 'unknown', + }); throw e; } } @@ -212,7 +227,10 @@ export class AuthService extends Service { this.session.revalidate(); track.$.$.auth.signedIn({ method: 'password' }); } catch (e) { - track.$.$.auth.signInFail({ method: 'password' }); + track.$.$.auth.signInFail({ + method: 'password', + reason: e instanceof BackendError ? e.originError.type : 'unknown', + }); throw e; } } diff --git a/packages/frontend/graphql/src/error.ts b/packages/frontend/graphql/src/error.ts index f4b5be18a0..c10623454a 100644 --- a/packages/frontend/graphql/src/error.ts +++ b/packages/frontend/graphql/src/error.ts @@ -12,14 +12,17 @@ export interface UserFriendlyErrorResponse { stacktrace?: string; } -export class UserFriendlyError implements UserFriendlyErrorResponse { - status = this.response.status; - code = this.response.code; - type = this.response.type; - name = this.response.name; - message = this.response.message; - args = this.response.args; - stacktrace = this.response.stacktrace; +export class UserFriendlyError + extends Error + implements UserFriendlyErrorResponse +{ + readonly status = this.response.status; + readonly code = this.response.code; + readonly type = this.response.type; + override readonly name = this.response.name; + override readonly message = this.response.message; + readonly args = this.response.args; + readonly stacktrace = this.response.stacktrace; static fromAnyError(response: any) { if (response instanceof GraphQLError) { @@ -51,7 +54,9 @@ export class UserFriendlyError implements UserFriendlyErrorResponse { }); } - constructor(private readonly response: UserFriendlyErrorResponse) {} + constructor(private readonly response: UserFriendlyErrorResponse) { + super(response.message); + } } export class GraphQLError extends BaseGraphQLError { diff --git a/packages/frontend/track/src/events.ts b/packages/frontend/track/src/events.ts index 3ca4576788..9b963bb492 100644 --- a/packages/frontend/track/src/events.ts +++ b/packages/frontend/track/src/events.ts @@ -382,7 +382,7 @@ export type EventArgs = { createWorkspace: { flavour: string }; signIn: AuthArgs; signedIn: AuthArgs; - signInFail: AuthArgs; + signInFail: AuthArgs & { reason: string }; viewPlans: PaymentEventArgs; checkout: PaymentEventArgs; subscribe: PaymentEventArgs;