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

View File

@@ -184,7 +184,7 @@ export class UserService {
const user = await this.findUserWithHashedPasswordByEmail(email);
if (!user) {
throw new WrongSignInCredentials();
throw new WrongSignInCredentials({ email });
}
if (!user.password) {
@@ -197,7 +197,7 @@ export class UserService {
);
if (!passwordMatches) {
throw new WrongSignInCredentials();
throw new WrongSignInCredentials({ email });
}
return user;

View File

@@ -37,6 +37,25 @@ const BaseTypeToHttpStatusMap: Record<UserFriendlyErrorBaseType, HttpStatus> = {
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 {
/**
* Standard HTTP status code
@@ -100,13 +119,11 @@ export class UserFriendlyError extends Error {
log(context: string) {
// ignore all user behavior error log
if (this.type !== 'internal_server_error') {
// always record auth related error
const isAuthError =
typeof this.stack === 'string' &&
(this.stack.includes('/core/auth/') ||
this.stack.includes('/plugins/oauth/'));
if (!isAuthError) return;
if (
this.type !== 'internal_server_error' &&
!IncludedEvents.has(this.name)
) {
return;
}
new Logger(context).error(
@@ -239,7 +256,8 @@ export const USER_FRIENDLY_ERRORS = {
},
wrong_sign_in_credentials: {
type: 'invalid_input',
message: 'Wrong user email or password.',
args: { email: 'string' },
message: ({ email }) => `Wrong user email or password: ${email}`,
},
unknown_oauth_provider: {
type: 'invalid_input',

View File

@@ -45,10 +45,14 @@ export class SameEmailProvided extends UserFriendlyError {
super('invalid_input', 'same_email_provided', message);
}
}
@ObjectType()
class WrongSignInCredentialsDataType {
@Field() email!: string
}
export class WrongSignInCredentials extends UserFriendlyError {
constructor(message?: string) {
super('invalid_input', 'wrong_sign_in_credentials', message);
constructor(args: WrongSignInCredentialsDataType, message?: string | ((args: WrongSignInCredentialsDataType) => string)) {
super('invalid_input', 'wrong_sign_in_credentials', message, args);
}
}
@ObjectType()
@@ -670,5 +674,5 @@ registerEnumType(ErrorNames, {
export const ErrorDataUnionType = createUnionType({
name: 'ErrorDataUnion',
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,
});

View File

@@ -209,7 +209,7 @@ type EditorType {
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 {
ACCESS_DENIED
@@ -1048,6 +1048,10 @@ type WorkspaceType {
team: Boolean!
}
type WrongSignInCredentialsDataType {
email: String!
}
type tokenType {
refresh: String!
sessionToken: String