Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance GetBridgeDBInstance: Add Error Handling for LevelDB Initialization #1198

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

vinayak0035
Copy link

Overview

Currently, the GetBridgeDBInstance function does not handle scenarios where bridgeDB may be nil. If the database fails to open for any reason, returning a nil object can lead to potential runtime panics or errors when the returned database instance is used. Adding error handling will help prevent these issues and improve the robustness of the code.

Changes Made

  • Updated the GetBridgeDBInstance function signature to return an error alongside the database instance.
  • Added error handling within the sync.Once block to capture and return any errors encountered while opening the LevelDB database.

Code Changes

Before:

func GetBridgeDBInstance(filePath string) *leveldb.DB {
    bridgeDBOnce.Do(func() {
        bridgeDB, _ = leveldb.OpenFile(filePath, nil)
    })
    return bridgeDB
}

After:

func GetBridgeDBInstance(filePath string) (*leveldb.DB, error) {
    var err error
    bridgeDBOnce.Do(func() {
        bridgeDB, err = leveldb.OpenFile(filePath, nil)
    })
    if err != nil {
        return nil, err
    }
    return bridgeDB, nil
}

Benefits

  • Enhanced reliability of the application.
  • Easier debugging by providing clear error messages.
  • Improved user experience by avoiding crashes due to nil references.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant