Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to catch errors when connecting to Redis.
Redis
try { const client = redis.createClient(); await client.connect(); } catch ([1]) { console.error('Connection error:', [1]); }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a variable name not declared in the catch block.
Leaving the catch parentheses empty.
✗ Incorrect
The catch block receives the error object, commonly named err to handle connection errors.
2fill in blank
mediumComplete the code to listen for Redis client error events.
Redis
client.on('error', function([1]) { console.log('Redis error:', [1]); });
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a variable name different from the one used inside the function.
Not passing any parameter to the function.
✗ Incorrect
The error event handler receives an error object, commonly named err.
3fill in blank
hardFix the error in the code to properly handle Redis command errors.
Redis
client.get('key', function(err, reply) { if ([1]) { console.error('Command error:', err); return; } console.log('Value:', reply); });
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Checking the reply instead of the error.
Using a wrong variable name for the error.
✗ Incorrect
Check if err exists to detect command errors before using the reply.
4fill in blank
hardFill both blanks to handle Redis connection errors and close the client.
Redis
client.on('error', function([1]) { console.error('Redis error:', [1]); client.[2](); });
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong parameter names in the error handler.
Calling a non-existent method to close the client.
✗ Incorrect
The error event handler receives err. To close the client, call client.quit().
5fill in blank
hardFill all three blanks to retry connecting on error with a delay.
Redis
client.on('error', async function([1]) { console.error('Error:', [1]); await new Promise(resolve => setTimeout(resolve, [2])); await client.[3](); });
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong variable names or delay values.
Calling the wrong method to reconnect.
✗ Incorrect
Use err as the error parameter, wait 1000 milliseconds, then call client.connect() to retry.