chore(server): filter out some logs (#9059)

This commit is contained in:
darkskygit
2024-12-12 09:19:48 +00:00
parent 69e5997608
commit cdb55a3393
6 changed files with 51 additions and 19 deletions
@@ -184,7 +184,7 @@ export class UserService {
const user = await this.findUserWithHashedPasswordByEmail(email); const user = await this.findUserWithHashedPasswordByEmail(email);
if (!user) { if (!user) {
throw new WrongSignInCredentials(); throw new WrongSignInCredentials({ email });
} }
if (!user.password) { if (!user.password) {
@@ -197,7 +197,7 @@ export class UserService {
); );
if (!passwordMatches) { if (!passwordMatches) {
throw new WrongSignInCredentials(); throw new WrongSignInCredentials({ email });
} }
return user; return user;
@@ -37,6 +37,25 @@ const BaseTypeToHttpStatusMap: Record<UserFriendlyErrorBaseType, HttpStatus> = {
internal_server_error: HttpStatus.INTERNAL_SERVER_ERROR, internal_server_error: HttpStatus.INTERNAL_SERVER_ERROR,
}; };
const IncludedEvents = new Set([
// email
'invalid_email',
'email_token_not_found',
'invalid_email_token',
'email_already_used',
'same_email_provided',
// magic link
'action_forbidden',
'link_expired',
'email_verification_required',
// oauth
'missing_oauth_query_parameter',
'unknown_oauth_provider',
'invalid_oauth_callback_state',
'oauth_state_expired',
'oauth_account_already_connected',
]);
export class UserFriendlyError extends Error { export class UserFriendlyError extends Error {
/** /**
* Standard HTTP status code * Standard HTTP status code
@@ -100,13 +119,11 @@ export class UserFriendlyError extends Error {
log(context: string) { log(context: string) {
// ignore all user behavior error log // ignore all user behavior error log
if (this.type !== 'internal_server_error') { if (
// always record auth related error this.type !== 'internal_server_error' &&
const isAuthError = !IncludedEvents.has(this.name)
typeof this.stack === 'string' && ) {
(this.stack.includes('/core/auth/') || return;
this.stack.includes('/plugins/oauth/'));
if (!isAuthError) return;
} }
new Logger(context).error( new Logger(context).error(
@@ -239,7 +256,8 @@ export const USER_FRIENDLY_ERRORS = {
}, },
wrong_sign_in_credentials: { wrong_sign_in_credentials: {
type: 'invalid_input', type: 'invalid_input',
message: 'Wrong user email or password.', args: { email: 'string' },
message: ({ email }) => `Wrong user email or password: ${email}`,
}, },
unknown_oauth_provider: { unknown_oauth_provider: {
type: 'invalid_input', type: 'invalid_input',
@@ -45,10 +45,14 @@ export class SameEmailProvided extends UserFriendlyError {
super('invalid_input', 'same_email_provided', message); super('invalid_input', 'same_email_provided', message);
} }
} }
@ObjectType()
class WrongSignInCredentialsDataType {
@Field() email!: string
}
export class WrongSignInCredentials extends UserFriendlyError { export class WrongSignInCredentials extends UserFriendlyError {
constructor(message?: string) { constructor(args: WrongSignInCredentialsDataType, message?: string | ((args: WrongSignInCredentialsDataType) => string)) {
super('invalid_input', 'wrong_sign_in_credentials', message); super('invalid_input', 'wrong_sign_in_credentials', message, args);
} }
} }
@ObjectType() @ObjectType()
@@ -670,5 +674,5 @@ registerEnumType(ErrorNames, {
export const ErrorDataUnionType = createUnionType({ export const ErrorDataUnionType = createUnionType({
name: 'ErrorDataUnion', name: 'ErrorDataUnion',
types: () => types: () =>
[UnknownOauthProviderDataType, MissingOauthQueryParameterDataType, InvalidEmailDataType, InvalidPasswordLengthDataType, SpaceNotFoundDataType, NotInSpaceDataType, AlreadyInSpaceDataType, SpaceAccessDeniedDataType, SpaceOwnerNotFoundDataType, DocNotFoundDataType, DocAccessDeniedDataType, VersionRejectedDataType, InvalidHistoryTimestampDataType, DocHistoryNotFoundDataType, BlobNotFoundDataType, UnsupportedSubscriptionPlanDataType, SubscriptionAlreadyExistsDataType, SubscriptionNotExistsDataType, SameSubscriptionRecurringDataType, SubscriptionPlanNotFoundDataType, CopilotMessageNotFoundDataType, CopilotPromptNotFoundDataType, CopilotProviderSideErrorDataType, RuntimeConfigNotFoundDataType, InvalidRuntimeConfigTypeDataType] as const, [WrongSignInCredentialsDataType, UnknownOauthProviderDataType, MissingOauthQueryParameterDataType, InvalidEmailDataType, InvalidPasswordLengthDataType, SpaceNotFoundDataType, NotInSpaceDataType, AlreadyInSpaceDataType, SpaceAccessDeniedDataType, SpaceOwnerNotFoundDataType, DocNotFoundDataType, DocAccessDeniedDataType, VersionRejectedDataType, InvalidHistoryTimestampDataType, DocHistoryNotFoundDataType, BlobNotFoundDataType, UnsupportedSubscriptionPlanDataType, SubscriptionAlreadyExistsDataType, SubscriptionNotExistsDataType, SameSubscriptionRecurringDataType, SubscriptionPlanNotFoundDataType, CopilotMessageNotFoundDataType, CopilotPromptNotFoundDataType, CopilotProviderSideErrorDataType, RuntimeConfigNotFoundDataType, InvalidRuntimeConfigTypeDataType] as const,
}); });
+5 -1
View File
@@ -209,7 +209,7 @@ type EditorType {
name: String! name: String!
} }
union ErrorDataUnion = AlreadyInSpaceDataType | BlobNotFoundDataType | CopilotMessageNotFoundDataType | CopilotPromptNotFoundDataType | CopilotProviderSideErrorDataType | DocAccessDeniedDataType | DocHistoryNotFoundDataType | DocNotFoundDataType | InvalidEmailDataType | InvalidHistoryTimestampDataType | InvalidPasswordLengthDataType | InvalidRuntimeConfigTypeDataType | MissingOauthQueryParameterDataType | NotInSpaceDataType | RuntimeConfigNotFoundDataType | SameSubscriptionRecurringDataType | SpaceAccessDeniedDataType | SpaceNotFoundDataType | SpaceOwnerNotFoundDataType | SubscriptionAlreadyExistsDataType | SubscriptionNotExistsDataType | SubscriptionPlanNotFoundDataType | UnknownOauthProviderDataType | UnsupportedSubscriptionPlanDataType | VersionRejectedDataType union ErrorDataUnion = AlreadyInSpaceDataType | BlobNotFoundDataType | CopilotMessageNotFoundDataType | CopilotPromptNotFoundDataType | CopilotProviderSideErrorDataType | DocAccessDeniedDataType | DocHistoryNotFoundDataType | DocNotFoundDataType | InvalidEmailDataType | InvalidHistoryTimestampDataType | InvalidPasswordLengthDataType | InvalidRuntimeConfigTypeDataType | MissingOauthQueryParameterDataType | NotInSpaceDataType | RuntimeConfigNotFoundDataType | SameSubscriptionRecurringDataType | SpaceAccessDeniedDataType | SpaceNotFoundDataType | SpaceOwnerNotFoundDataType | SubscriptionAlreadyExistsDataType | SubscriptionNotExistsDataType | SubscriptionPlanNotFoundDataType | UnknownOauthProviderDataType | UnsupportedSubscriptionPlanDataType | VersionRejectedDataType | WrongSignInCredentialsDataType
enum ErrorNames { enum ErrorNames {
ACCESS_DENIED ACCESS_DENIED
@@ -1048,6 +1048,10 @@ type WorkspaceType {
team: Boolean! team: Boolean!
} }
type WrongSignInCredentialsDataType {
email: String!
}
type tokenType { type tokenType {
refresh: String! refresh: String!
sessionToken: String sessionToken: String
@@ -50,7 +50,7 @@ test('should throw if user not found', async t => {
const { auth } = t.context; const { auth } = t.context;
await t.throwsAsync(() => auth.signIn('u2@affine.pro', '1'), { await t.throwsAsync(() => auth.signIn('u2@affine.pro', '1'), {
message: 'Wrong user email or password.', message: 'Wrong user email or password: u2@affine.pro',
}); });
}); });
@@ -72,7 +72,7 @@ test('should throw if password not match', async t => {
const { auth } = t.context; const { auth } = t.context;
await t.throwsAsync(() => auth.signIn('u1@affine.pro', '2'), { await t.throwsAsync(() => auth.signIn('u1@affine.pro', '2'), {
message: 'Wrong user email or password.', message: 'Wrong user email or password: u1@affine.pro',
}); });
}); });
@@ -87,7 +87,7 @@ test('should be able to change password', async t => {
await t.throwsAsync( await t.throwsAsync(
() => auth.signIn('u1@affine.pro', '1' /* old password */), () => auth.signIn('u1@affine.pro', '1' /* old password */),
{ {
message: 'Wrong user email or password.', message: 'Wrong user email or password: u1@affine.pro',
} }
); );
@@ -104,7 +104,7 @@ test('should be able to change email', async t => {
await auth.changeEmail(u1.id, 'u2@affine.pro'); await auth.changeEmail(u1.id, 'u2@affine.pro');
await t.throwsAsync(() => auth.signIn('u1@affine.pro' /* old email */, '1'), { await t.throwsAsync(() => auth.signIn('u1@affine.pro' /* old email */, '1'), {
message: 'Wrong user email or password.', message: 'Wrong user email or password: u1@affine.pro',
}); });
signedInU1 = await auth.signIn('u2@affine.pro', '1'); signedInU1 = await auth.signIn('u2@affine.pro', '1');
+7 -1
View File
@@ -285,7 +285,8 @@ export type ErrorDataUnion =
| SubscriptionPlanNotFoundDataType | SubscriptionPlanNotFoundDataType
| UnknownOauthProviderDataType | UnknownOauthProviderDataType
| UnsupportedSubscriptionPlanDataType | UnsupportedSubscriptionPlanDataType
| VersionRejectedDataType; | VersionRejectedDataType
| WrongSignInCredentialsDataType;
export enum ErrorNames { export enum ErrorNames {
ACCESS_DENIED = 'ACCESS_DENIED', ACCESS_DENIED = 'ACCESS_DENIED',
@@ -1408,6 +1409,11 @@ export interface WorkspaceTypePublicPageArgs {
pageId: Scalars['String']['input']; pageId: Scalars['String']['input'];
} }
export interface WrongSignInCredentialsDataType {
__typename?: 'WrongSignInCredentialsDataType';
email: Scalars['String']['output'];
}
export interface TokenType { export interface TokenType {
__typename?: 'tokenType'; __typename?: 'tokenType';
refresh: Scalars['String']['output']; refresh: Scalars['String']['output'];