RedisStoreException

Overview

RedisStoreException is a custom exception class in the Midnite81\Guardian package. This exception is thrown when there are issues related to Redis operations within the RedisStore class, which is one of the storage options for the Guardian rate limiting system.

Class Details

  • Namespace: Midnite81\Guardian\Exceptions\Store

  • Extends: StoreException

Purpose

The primary purpose of this exception is to provide specific error handling for Redis-related issues that may occur when Guardian is using Redis for storing rate limiting data. It helps distinguish Redis-specific errors from other types of storage errors in the Guardian system.

Usage

This exception is typically thrown by the RedisStore class when it encounters issues with Redis operations such as:

  • Connection failures

  • Command execution errors

  • Serialization or deserialization issues

Example of where this exception might be thrown (inside RedisStore):

try {
    $value = $this->redis->get($this->prefix . $key);
    if ($value === false) {
        return $default;
    }
    $result = json_decode($value, true, 512, JSON_THROW_ON_ERROR);
    return $result ?? $default;
} catch (RedisException|JsonException $e) {
    throw new RedisStoreException($e->getMessage(), $e->getCode(), $e);
}

Catching and Handling

When using Guardian with a Redis store, you should be prepared to catch and handle this exception:

use Midnite81\Guardian\Exceptions\Store\RedisStoreException;

try {
    // Your Guardian code using RedisStore
    $guardian->send(function() {
        // Your rate-limited code here
    });
} catch (RedisStoreException $e) {
    // Log the error
    error_log("Redis error in Guardian: " . $e->getMessage());
    
    // Gracefully degrade or use a fallback mechanism
    // For example, you might choose to allow the request in case of storage errors
    return true;
}

Best Practices

  1. Always catch this exception when using Guardian with a RedisStore.

  2. Log the exception details for debugging and monitoring purposes.

  3. Implement appropriate fallback mechanisms or graceful degradation when Redis operations fail.

  4. Consider using a different storage mechanism (like database or file-based storage) if Redis issues are frequent.

  5. Implement retry logic for transient Redis errors, if appropriate for your use case.

Error Messages

The error messages for this exception will typically include details about the specific Redis operation that failed. These can include:

  • Connection errors

  • Timeout issues

  • Command execution failures

For example:

Redis connection timed out

or

Redis command execution failed: WRONGTYPE Operation against a key holding the wrong kind of value

Integration with Guardian

This exception is an integral part of Guardian's error handling system for Redis storage. It allows the system to differentiate between different types of storage errors and provide more specific error handling and reporting.

Security Considerations

When handling RedisStoreException, be careful not to expose sensitive Redis server information in user-facing error messages. Always log the full exception details securely, but provide only general error messages to end-users.

Troubleshooting

If you frequently encounter RedisStoreException:

  1. Check your Redis server status and connection settings.

  2. Ensure that your Redis server has enough memory allocated.

  3. Verify that the Redis commands being used are supported by your Redis version.

  4. Check for network issues between your application and the Redis server.

  5. Consider implementing a Redis sentinel or cluster for improved reliability.

Performance Implications

While Redis is generally very fast, frequent RedisStoreException occurrences could impact the performance of your rate limiting system. Monitor these exceptions closely and consider adjusting your Redis configuration or scaling your Redis infrastructure if needed.

By properly handling RedisStoreException, you can ensure that your application gracefully manages Redis-related issues in the Guardian rate limiting system, maintaining reliability and performance even when Redis operations fail.

Last updated