Files
knowai/auth/errors.ts
2025-11-23 22:26:39 +08:00

69 lines
1.7 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* 认证模块错误处理
*/
/**
* 认证错误类
*/
export class AuthError extends Error {
public readonly code: string;
public readonly details: unknown;
constructor(code: string, message: string, details?: unknown) {
super(message);
// 这个得删还有test文件里的也要删
this.name = 'AuthError';
this.code = code;
this.details = details;
}
/**
* 创建登录失败错误
*/
static loginFailed(message: string, details?: unknown): AuthError {
return new AuthError('LOGIN_FAILED', message, details);
}
/**
* 创建登出失败错误
*/
static logoutFailed(message: string, details?: unknown): AuthError {
return new AuthError('LOGOUT_FAILED', message, details);
}
/**
* 创建注册失败错误
*/
static registerFailed(message: string, details?: unknown): AuthError {
return new AuthError('REGISTER_FAILED', message, details);
}
/**
* 创建用户未找到错误
*/
static userNotFound(message: string, details?: unknown): AuthError {
return new AuthError('USER_NOT_FOUND', message, details);
}
/**
* Session过期
*/
static sessionExpired(message: string, details?: unknown): AuthError {
return new AuthError('SESSION_EXPIRED', message, details);
}
/**
* Session未认证
*/
static notAuthenticated(message: string = 'User not authenticated', details?: unknown): AuthError {
return new AuthError('NOT_AUTHENTICATED', message, details);
}
/**
* 通用未知报错
*/
static unknown(message: string = 'Unknown authentication error', details?: unknown): AuthError {
return new AuthError('UNKNOWN', message, details);
}
}