Back to all reviewers

Check exceptions consistently

oven-sh/bun
Based on 6 comments
C++

Always check for exceptions immediately after operations that might throw them, especially before using the results in control flow decisions. When accessing properties that might be used in conditionals, retrieve the property first, check for exceptions, and then use the property in the conditional.

Error Handling C++

Reviewer Prompt

Always check for exceptions immediately after operations that might throw them, especially before using the results in control flow decisions. When accessing properties that might be used in conditionals, retrieve the property first, check for exceptions, and then use the property in the conditional.

Incorrect pattern:

if (JSValue property = object->getIfPropertyExists(globalObject, propertyName)) {
    // Use property...
    RETURN_IF_EXCEPTION(scope, {});  // Too late - exception may have occurred during getIfPropertyExists
}

Correct pattern:

JSValue property = object->getIfPropertyExists(globalObject, propertyName);
RETURN_IF_EXCEPTION(scope, {});  // Check immediately after the operation
if (property) {
    // Now safe to use property...
}

This pattern applies to any function that might throw exceptions, including property access, method calls, and object construction. Ensure exception checking occurs after each potentially throwing operation and before using its results. Use consistent macros like RETURN_IF_EXCEPTION or RELEASE_AND_RETURN based on your codebase’s conventions.

For property chains or multiple operations, check after each step:

auto value = params->get(globalObject, property);
RETURN_IF_EXCEPTION(scope, nullptr);
// Now safe to use value

Following this pattern prevents hard-to-debug crashes and ensures exceptions are properly handled throughout all code paths.

6
Comments Analyzed
C++
Primary Language
Error Handling
Category

Source Discussions