Jump to content

Advice re Error Handling


kiffab
 Share

Recommended Posts

Hi Guys

I am building a monitoring tool which is almost complete.

This is in the form of a dashboard reading data from various log files, databases, etc. As you can imagine with various data sources, there is a possibility that the application can error (missing files, files refreshing, SQL down, etc).

Most of the AutoIt errors I have seen exit upon failure. What's the best way to handle this in my app? I plan to write to a database by calling a function before it closes. For example :

DBConnect('Error', 'Server X', $time, 'unable to connect to sql database')

What is the cleanest way to "error" but continue running? Use the "break" command?

Thanks, appreciate any input.

K.

Link to comment
Share on other sites

You need to validate things prior to performing actions...such as IsObj() for db...that way, you don't perform methods, or get properties, from things you expect to be an object, but are not...same is true for files, through FileOpen, with your proper mode, and verifying not @error

IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.
Link to comment
Share on other sites

I would split your script into several functions.

The main function displays the dashboard. For every data type I would create a separate function (read a file, read a SQL database etc.) to retrieve the data. Each of this functions returns the data (on success) or sets @error. If @error is set the main function then can display the error message or a "Not available" for the the corresponding data.

Each retrieve function has to handle all possible errors and set @error accordingly.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

Hi Water

That is how it works. I have a GUI that just calls the function. Each function exists in it's own file and updates the GUI in some way.

An example may help:

Func SQLCheck()

$time = @YEAR & "-" & @MON & "-" & @MDAY & " " & @HOUR & ":" & @MIN & ":" & @SEC

TCPStartup()
$server = TCPNameToIP("MyDatabaseServer")
$port = 1433

$socket = TCPConnect($server,$port)
Switch $socket
    Case -1
            $SQLCheck = 1
                If $SQLCheck <> $SQLCheck_old Then
                    GUICtrlSetData($06_schematic_sql , _SetImage($06_schematic_sql, @ScriptDir & "buttonsschematic_down.png"))
                    DBConnect('SQL', 'SQL DB', $time, 'Down')
                    $SQLCheck_Old = $SQLCheck
                EndIf

    Case Else
            $SQLCheck = 2
                If $SQLCheck <> $SQLCheck_old Then

                    GUICtrlSetData($06_schematic_sql , _SetImage($06_schematic_sql, @ScriptDir & "buttonsschematic_up.png"))
                    $SQLCheck_Old = $SQLCheck
                EndIf
EndSwitch

TCPShutdown()

EndFunc

SQLcheck flags are set to 0 when the main GUI is reset or opened for the first time. I use these to try and update the image only when required (i.e. status change)

Link to comment
Share on other sites

Looks good.

You just have to make sure that you grab all possible errors.

So after TCPStartup and TCPNameToIP I would insert a check to make sure that @error = 0. If not, display the error sign.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...