Skip to content

Commit 1a10417

Browse files
committed
feat(core): track signin failed reason (#8746)
fix AF-1568
1 parent 02dbe13 commit 1a10417

File tree

4 files changed

+41
-16
lines changed

4 files changed

+41
-16
lines changed

packages/frontend/core/src/modules/cloud/error.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import type { UserFriendlyError } from '@affine/graphql';
2+
13
export class NetworkError extends Error {
24
constructor(
35
public readonly originError: Error,
@@ -14,7 +16,7 @@ export function isNetworkError(error: Error): error is NetworkError {
1416

1517
export class BackendError extends Error {
1618
constructor(
17-
public readonly originError: Error,
19+
public readonly originError: UserFriendlyError,
1820
public readonly status?: number
1921
) {
2022
super(`Server error: ${originError.message}`);

packages/frontend/core/src/modules/cloud/services/auth.ts

+23-5
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { distinctUntilChanged, map, skip } from 'rxjs';
1212

1313
import type { UrlService } from '../../url';
1414
import { type AuthAccountInfo, AuthSession } from '../entities/session';
15+
import { BackendError } from '../error';
1516
import type { AuthStore } from '../stores/auth';
1617
import type { FetchService } from './fetch';
1718

@@ -111,7 +112,10 @@ export class AuthService extends Service {
111112
},
112113
});
113114
} catch (e) {
114-
track.$.$.auth.signInFail({ method: 'magic-link' });
115+
track.$.$.auth.signInFail({
116+
method: 'magic-link',
117+
reason: e instanceof BackendError ? e.originError.type : 'unknown',
118+
});
115119
throw e;
116120
}
117121
}
@@ -129,7 +133,10 @@ export class AuthService extends Service {
129133
this.session.revalidate();
130134
track.$.$.auth.signedIn({ method: 'magic-link' });
131135
} catch (e) {
132-
track.$.$.auth.signInFail({ method: 'magic-link' });
136+
track.$.$.auth.signInFail({
137+
method: 'magic-link',
138+
reason: e instanceof BackendError ? e.originError.type : 'unknown',
139+
});
133140
throw e;
134141
}
135142
}
@@ -166,7 +173,11 @@ export class AuthService extends Service {
166173

167174
return url;
168175
} catch (e) {
169-
track.$.$.auth.signInFail({ method: 'oauth', provider });
176+
track.$.$.auth.signInFail({
177+
method: 'oauth',
178+
provider,
179+
reason: e instanceof BackendError ? e.originError.type : 'unknown',
180+
});
170181
throw e;
171182
}
172183
}
@@ -186,7 +197,11 @@ export class AuthService extends Service {
186197
track.$.$.auth.signedIn({ method: 'oauth', provider });
187198
return res.json();
188199
} catch (e) {
189-
track.$.$.auth.signInFail({ method: 'oauth', provider });
200+
track.$.$.auth.signInFail({
201+
method: 'oauth',
202+
provider,
203+
reason: e instanceof BackendError ? e.originError.type : 'unknown',
204+
});
190205
throw e;
191206
}
192207
}
@@ -212,7 +227,10 @@ export class AuthService extends Service {
212227
this.session.revalidate();
213228
track.$.$.auth.signedIn({ method: 'password' });
214229
} catch (e) {
215-
track.$.$.auth.signInFail({ method: 'password' });
230+
track.$.$.auth.signInFail({
231+
method: 'password',
232+
reason: e instanceof BackendError ? e.originError.type : 'unknown',
233+
});
216234
throw e;
217235
}
218236
}

packages/frontend/graphql/src/error.ts

+14-9
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,17 @@ export interface UserFriendlyErrorResponse {
1212
stacktrace?: string;
1313
}
1414

15-
export class UserFriendlyError implements UserFriendlyErrorResponse {
16-
status = this.response.status;
17-
code = this.response.code;
18-
type = this.response.type;
19-
name = this.response.name;
20-
message = this.response.message;
21-
args = this.response.args;
22-
stacktrace = this.response.stacktrace;
15+
export class UserFriendlyError
16+
extends Error
17+
implements UserFriendlyErrorResponse
18+
{
19+
readonly status = this.response.status;
20+
readonly code = this.response.code;
21+
readonly type = this.response.type;
22+
override readonly name = this.response.name;
23+
override readonly message = this.response.message;
24+
readonly args = this.response.args;
25+
readonly stacktrace = this.response.stacktrace;
2326

2427
static fromAnyError(response: any) {
2528
if (response instanceof GraphQLError) {
@@ -51,7 +54,9 @@ export class UserFriendlyError implements UserFriendlyErrorResponse {
5154
});
5255
}
5356

54-
constructor(private readonly response: UserFriendlyErrorResponse) {}
57+
constructor(private readonly response: UserFriendlyErrorResponse) {
58+
super(response.message);
59+
}
5560
}
5661

5762
export class GraphQLError extends BaseGraphQLError {

packages/frontend/track/src/events.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@ export type EventArgs = {
382382
createWorkspace: { flavour: string };
383383
signIn: AuthArgs;
384384
signedIn: AuthArgs;
385-
signInFail: AuthArgs;
385+
signInFail: AuthArgs & { reason: string };
386386
viewPlans: PaymentEventArgs;
387387
checkout: PaymentEventArgs;
388388
subscribe: PaymentEventArgs;

0 commit comments

Comments
 (0)