Sitelet https://github.com/redis/node-redis/issues/2595
Skip to content

Error Object Null in Redis Caching Function #2595

Description

@BarAppTeam

Description

Issue Title: Error Object Null in Redis Caching Function

Issue Description:
I encountered an issue with the setCache function in my application while trying to cache data using Redis. The issue is that when an error occurs within the function, the error object is unexpectedly null, and the error details are not being logged or displayed properly in the catch block or console.log.

Reproduction Steps:

  1. Call the setCache function with valid parameters.
  2. Observe that an exception is thrown, but the error object within the catch block is null.
  3. The expected behavior is that the error object should contain information about the error, including a stack trace and error message.

Code Sample:

export const client = createClient({url: Config.env(REDIS_CONNECTION_URL)});

export const setCache = async (key: string, value: unknown, defaultExpiration: number = null): Promise<void> => {
    try {
        if (defaultExpiration) {
            await client.SET(key, JSON.stringify(value), {
                EX: defaultExpiration
            });
        } else {
            await client.SET(key, JSON.stringify(value));
        }
    } catch (error) {
        console.log("Failed to insert item to redis", {
            key,
            value,
            redisError: error,
            defaultExpiration,
            errorDebug: "try-catch"
        });
    }
};

Expected Behavior:

  • When an error occurs during the Redis caching operation, the catch block should capture the error object with relevant error details.
  • The console.log statement within the catch block should output the error details, including error message and stack trace.

Actual Behavior:

  • When an error occurs, the catch block is executed, but the error object is null.
  • The console.log statement outputs incomplete information, as the redisError field contains null.

Additional Information:

  • I have verified that the Redis client is properly initialized and connected to the Redis server.
  • I have tried logging the error using both console.log and console.error within the catch block, but the error object remains null.

Possible Solution:
I suspect that there might be an issue with the way the Redis client or the Promise rejection is being handled within the library. I've followed best practices for error handling and asynchronous programming, but the issue persists.

Node.js Version

v19.6.0

Redis Server Version

6.2.6

Node Redis Version

redis@4.6.7

Platform

macOS

Logs

No response

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions