langchain-ai / langchain

🦜🔗 Build context-aware reasoning applications
https://python.langchain.com
MIT License
88.64k stars 13.93k forks source link

DOC: Remove try/catch blocks from sample connection code to allow actual error to be shown #22410

Open cjbj opened 1 month ago

cjbj commented 1 month ago

URL

https://github.com/langchain-ai/langchain/blob/master/cookbook/oracleai_demo.ipynb

Checklist

Issue with current documentation:

The sample code in https://github.com/langchain-ai/langchain/blob/master/cookbook/oracleai_demo.ipynb uses try/catch blocks which don't print the actual driver or DB error, making it impossible to troubleshoot. For example it currently has:

import sys

import oracledb

# please update with your username, password, hostname and service_name
username = ""
password = ""
dsn = ""

try:
    conn = oracledb.connect(user=username, password=password, dsn=dsn)
    print("Connection successful!")
except Exception as e:
    print("Connection failed!")
    sys.exit(1)

For any connection failure this will only show:

Connection failed!

The code should be changed to:

import sys

import oracledb

# please update with your username, password, hostname and service_name
username = ""
password = ""
dsn = ""

conn = oracledb.connect(user=username, password=password, dsn=dsn)
print("Connection successful!")

This, for example, with an incorrect password will show a traceback and a useful error:

oracledb.exceptions.DatabaseError: ORA-01017: invalid credential or not authorized; logon denied
Help: https://docs.oracle.com/error-help/db/ora-01017/

The same try/catch problem exists in other examples.

Idea or request for content:

No response

isatyamks commented 4 weeks ago

@cjbj I would like to work on this issue. Could you please assign me issue #22410?

cjbj commented 4 weeks ago

@isatyamks I don't have privileges to assign it.

isatyamks commented 4 weeks ago

22635

The outer try/except block handles connection errors, and the inner try/except block handles SQL execution errors, providing detailed error messages for both.
try: conn = oracledb.connect(user=username, password=password, dsn=dsn) print("Connection successful!")

cursor = conn.cursor() try: cursor.execute( """ begin -- Drop user begin execute immediate 'drop user testuser cascade'; exception when others then dbms_output.put_line('Error dropping user: ' || SQLERRM); end;

@cjbj I hope this change will work...