DEV Community

Peter + AI
Peter + AI

Posted on

Understanding $dberror in Uniface 10.4: Your DBMS Error Detective 🔍

This blog post was created with AI assistance to help Uniface developers understand error handling better.

Hey Uniface developers! 👋 Today we're diving into one of the most useful debugging functions in Uniface 10.4: $dberror. If you've ever wondered why your database operations are failing, this little function is your best friend.

What is $dberror? 🤔

The $dberror function is a ProcScript function that returns the specific error code reported by your Database Management System (DBMS). Think of it as a translator that tells you exactly what went wrong when your database operation fails.

Unlike Uniface's internal error codes, $dberror gives you the raw error number directly from your database system (like Oracle, SQL Server, or MySQL). This means you get database-specific error information that can help you pinpoint exactly what's happening.

How to Use $dberror 💡

The best part? You can use $dberror in all component types in Uniface. Here's the basic syntax:

$dberror
Enter fullscreen mode Exit fullscreen mode

That's it! No parameters needed. The function simply returns the last error number from your DBMS.

Real-World Example 📝

Here's a practical example of how to use $dberror in your error handling:

if ($status < 0)
    putmess "DBMS / Network error %%$dberror: %%$dberrortext"
endif
Enter fullscreen mode Exit fullscreen mode

In this example:

  • We check if $status is negative (indicating an error occurred)
  • If there's an error, we display both the error number ($dberror) and the error message ($dberrortext)
  • The %% syntax is used to embed the function values in the message

Pro Tips for Better Error Handling 🚀

1. Always Pair with $dberrortext

While $dberror gives you the error number, $dberrortext provides the human-readable error message. Using both together gives you complete error information.

2. Database-Specific Errors

Remember that $dberror returns database-specific codes. An Oracle error code will be different from a SQL Server error code for the same type of problem. Always check your DBMS documentation for error code meanings.

3. Driver vs DBMS Errors

Your database driver can use $dberror for both internal driver errors and actual DBMS errors. This means you might see driver-specific error codes too.

When Should You Use $dberror? ⚡

Use $dberror whenever you need to:

  • Debug database connection issues 🔌
  • Handle specific database errors differently in your code
  • Log detailed error information for troubleshooting 📋
  • Provide better user feedback when database operations fail

Common Pitfalls to Avoid ⚠️

Don't confuse $dberror with $error: While $error returns Uniface's internal error codes, $dberror specifically returns database-related error codes. They serve different purposes in your error handling strategy.

Wrapping Up 🎯

The $dberror function is an essential tool for any Uniface developer working with databases. It provides direct access to DBMS error codes, making debugging much easier and more precise.

Next time your database operation fails, don't just rely on generic error messages. Use $dberror alongside $dberrortext to get the full picture of what's happening at the database level.

Happy coding! 💻✨

Top comments (0)