export class AppError extends Error {
  public readonly statusCode: number
  public readonly isOperational: boolean
  public readonly code?: string

  constructor(
    message: string,
    statusCode: number = 500,
    code?: string,
    isOperational: boolean = true
  ) {
    super(message)
    this.statusCode = statusCode
    this.code = code
    this.isOperational = isOperational

    Error.captureStackTrace(this, this.constructor)
  }
}

export class ValidationError extends AppError {
  constructor(message: string, code?: string) {
    super(message, 400, code || 'VALIDATION_ERROR')
  }
}

export class AuthenticationError extends AppError {
  constructor(message: string = '未授权访问', code?: string) {
    super(message, 401, code || 'AUTHENTICATION_ERROR')
  }
}

export class AuthorizationError extends AppError {
  constructor(message: string = '权限不足', code?: string) {
    super(message, 403, code || 'AUTHORIZATION_ERROR')
  }
}

export class NotFoundError extends AppError {
  constructor(message: string = '资源不存在', code?: string) {
    super(message, 404, code || 'NOT_FOUND_ERROR')
  }
}

export class ConflictError extends AppError {
  constructor(message: string, code?: string) {
    super(message, 409, code || 'CONFLICT_ERROR')
  }
}

export class RateLimitError extends AppError {
  constructor(message: string = '请求过于频繁', code?: string) {
    super(message, 429, code || 'RATE_LIMIT_ERROR')
  }
}

export class InternalServerError extends AppError {
  constructor(message: string = '服务器内部错误') {
    super(message, 500, 'INTERNAL_SERVER_ERROR', false)
  }
} 