Jump to content

@error, error handling


Recommended Posts

Are there any more way of capturing error messages other then @error?

Can I get a line number, or a function call that failed or an error message other then an integer representing a code?

I am trying to capture as much info as possible when a script fails and email the info out.

Link to comment
Share on other sites

Are you talking about on a compiled code or running from Scite? A compiled code has no line numbers. Errors are all about how/what handling works best for you and the particular code you are running. What errors are you trying to capture information about? If something returns an error code inside a function you can use return seterror to retun an error code to the main body of your script and then deal with it from there.

If you give us more info about specifically what is going wrong that you are trying to capture info about maybe I can give you something more specific.

EDIT: Oh forgot to ask do you mean a hard autoit error that crashes autoit?

Edited by someone
While ProcessExists('Andrews bad day.exe')
	BlockInput(1)
	SoundPlay('Music.wav')
	SoundSetWaveVolume('Louder')
WEnd
Link to comment
Share on other sites

I am running my code in scite now during development. Later it will be a scedule job in Windows. I use: _IECreate, _IELinkClickByText, _IEAttach, ...

The documentation and output in the scite window lists error codes like these, but I cant get then at runtime:

@Error: 0 ($_IEStatus_Success) = No Error

1 ($_IEStatus_GeneralError) = General Error

3 ($_IEStatus_InvalidDataType) = Invalid Data Type

4 ($_IEStatus_InvalidObjectType) = Invalid Object Type

6 ($_IEStatus_LoadWaitTimeout) = Load Wait Timeout

7 ($_IEStatus_NoMatch) = No Match

8 ($_IEStatus_AccessIsDenied) = Access Is Denied

9 ($_IEStatus_ClientDisconnected) = Client Disconnected

And some of the built-in functions return an error code and other set the @error var.

In my code I write things like this, is there a better way?:

$SuccessFailCode = _IELinkClickByText ($oIE, "Data Entries Extract")

if ($SuccessFailCode = 0) Then

HandleError("Data Entries Extract link click", @error) ; custom function to send email

ContinueLoop

EndIf

Is there a better way then to constantly track return codes and error flags?

How to get full error messages into an email?

Link to comment
Share on other sites

  • 10 months later...

I know this is an old post, but someone searching for a solution for this might find this helpful...

I had two big hurdles to overcome. 1) I have had to figure out a way for AutoIt to run autonomously yet still notify me if there is a problem so I can check it. 2) I also have to interact with a program that does not support interactivity (a dumbed-down version of wIntegrate), so the only way to see what is going on is to Select All, Copy, and use ClipGet to analyze the screen data. I modified Asimzameer's/Walkabout's _INetSmtpMailCom to be more specific to my needs and named it _ErrorMail. This allows me to have an error log that is used all the time, plus the option to have msgbox pop-ups when errors occur (if someone will be at the console), plus I have the option to receive a text message with the error, an email with the error, or both. Simplified:

Log file only

Log file + pop-up

Log file + text and/or email

Log file + pop-up + text and/or email

I have a separate page where I keep all the configuration parameters, so below is the list of the variables I use.

Global $err_Notify = 1 ;0 = Logfile/console only, 1 = Log + Texts/eMails only, 2 = Log + Pop-ups only, 3 = All

Global $err_NotifyContacts = 1 ;1 = Primary only, 2 = Secondary only, 3 = Both (ignored if $err_Notify is 0 or 2)

Global $err_MailServer = "mail.YOURMAILSERVER.com" ;Mail server

Global $err_SentFromName = "DD" ;The name shown in the "Sent From" box of the email

Global $err_SentFromAddy = "USERNAME@YOURMAILSERVER.com" ;The email address of the error sender

Global $err_Password = "USERPASSWORD" ;The password for the error sender email

Global $err_MailServerPort = "2525" ;Mail server port

Global $err_SendToAddyPri = "949xxxxxxx@messaging.sprintpcs.com" ;The primary address to send errors to

Global $err_SendToAddySec = "xxxxxx@sbcglobal.net" ;The secondary address to send errors to

Global $err_Subject = "Error" ;Error message subject

I'm sorry that this is not in proper UDF format, but I never intended it to be used by anyone besides me. And if you can't use the screenshot feature, you can just omit lines 20 (If $screenshot = "Y" Then) through 41 (EndIf).

If you are not going to be getting COM errors and must specify your own, this isn't a bad solution. It IS tedious, because you have to write out each $error, but because you write them out you can put as much info as you'd like and the texts/emails you receive are very useful.

Global $errorWriter = FileOpen($errorLog, 1); at the top of the script

;Write errors in the log file, $screenshot="Y" if it's an error caused by something pulling if/then from the screen
Func era_ErrorLogger($error, $screenshot="N")
If $error = "" Then Return
If $err_Notify > 3 Then Return
If $err_Notify < 0 Then Return
$tTime = _Date_Time_GetLocalTime()
ConsoleWrite(@CRLF&@CRLF&"Error recorded at:   "& _Date_Time_SystemTimeToDateTimeStr($tTime)&@CRLF)
ConsoleWrite("Error that occurred: "&$error&@CRLF&@CRLF&@CRLF)
$formatTime = _Date_Time_SystemTimeToDateTimeStr($tTime)
$err_Subject = $formatTime
FileWriteLine($errorWriter, "*****************************************************************************************************************")
FileWriteLine($errorWriter, "*       *************************************************************************************************       *")
FileWriteLine($errorWriter, "*              ***********************************************************************************              *")
FileWriteLine($errorWriter, "*                                                                                                               *")
FileWriteLine($errorWriter, "*                                                                                                               *")
FileWriteLine($errorWriter, "   ERROR ** Error logged at: "& _Date_Time_SystemTimeToDateTimeStr($tTime))
FileWriteLine($errorWriter, "   "&$error)
FileWriteLine($errorWriter, "*                                                                                                               *")
FileWriteLine($errorWriter, "*                                                                                                               *")
FileWriteLine($errorWriter, "*              ***********************************************************************************              *")
FileWriteLine($errorWriter, "*       *************************************************************************************************       *")
If $screenshot = "Y" Then
FileWriteLine($errorWriter, "*****************************************************************************************************************")
FileWriteLine($errorWriter, "**************************************       *          *          *       **************************************")
FileWriteLine($errorWriter, "**************************************       *          *          *       **************************************")
FileWriteLine($errorWriter, "**************************************       *          *          *       **************************************")
FileWriteLine($errorWriter, "*          BEGIN SCREENSHOT          *    *******    *******    *******    *          BEGIN SCREENSHOT          *")
FileWriteLine($errorWriter, "**************************************     *****      *****      *****     **************************************")
FileWriteLine($errorWriter, "**************************************      ***        ***        ***      **************************************")
FileWriteLine($errorWriter, "**************************************       *          *          *       **************************************")
FileWriteLine($errorWriter, "*****************************************************************************************************************")
$screen = ClipGet()
If StringStripWS($screen, 8) = "" Then $screen = "-                                       Failed to capture the screen data                                       -"
FileWriteLine($errorWriter, @CRLF&@CRLF&@CRLF&$screen&@CRLF&@CRLF&@CRLF&@CRLF)
FileWriteLine($errorWriter, "*****************************************************************************************************************")
FileWriteLine($errorWriter, "**************************************       *          *          *       **************************************")
FileWriteLine($errorWriter, "**************************************      ***        ***        ***      **************************************")
FileWriteLine($errorWriter, "**************************************     *****      *****      *****     **************************************")
FileWriteLine($errorWriter, "*           END SCREENSHOT           *    *******    *******    *******    *           END SCREENSHOT           *")
FileWriteLine($errorWriter, "**************************************       *          *          *       **************************************")
FileWriteLine($errorWriter, "**************************************       *          *          *       **************************************")
FileWriteLine($errorWriter, "**************************************       *          *          *       **************************************")
EndIf
FileWriteLine($errorWriter, "*****************************************************************************************************************"&@CRLF&@CRLF&@CRLF&@CRLF)
FileWriteLine($errorWriter, "              N     N  EEEEEEE  X     X  TTTTTTT        EEEEEEE  RRRRRR   RRRRRR    OOOOO   RRRRRR               ")
FileWriteLine($errorWriter, "              NN    N  E         X   X      T           E        R     R  R     R  O     O  R     R")
FileWriteLine($errorWriter, "              N N   N  E          X X       T           E        R     R  R     R  O     O  R     R")
FileWriteLine($errorWriter, "              N  N  N  EEEEEE      X        T           EEEEEE   RRRRRR   RRRRRR   O     O  RRRRRR")
FileWriteLine($errorWriter, "              N   N N  E          X X       T           E        R   R    R   R    O     O  R   R")
FileWriteLine($errorWriter, "              N    NN  E         X   X      T           E        R    R   R    R   O     O  R    R")
FileWriteLine($errorWriter, "              N     N  EEEEEEE  X     X     T           EEEEEEE  R     R  R     R   OOOOO   R     R"&@CRLF&@CRLF&@CRLF&@CRLF)
If $err_Notify > 0 Then
If $err_Notify > 1 Then MsgBox(0, "Error", $error, 3)
If $err_Notify <> 2 Then _ErrorMail($err_MailServer, $err_SentFromName, $err_SentFromAddy, $err_SendToAddyPri, $err_Subject, $error, "", $err_SendToAddySec)
EndIf
Return
EndFunc;==>



; #FUNCTION# ====================================================================================================================
; Name...........: _ErrorMail
; Description ...: Sends an email using SMTP over TCP IP.
; Parameters ....: $s_SmtpServer    - SMTP server to be used for sending email
;                  $s_FromName      - Name of sender
;                  $s_FromAddress   - eMail address of sender
;                  $s_ToAddress     - Address that email is to be sent to
;                  $s_Subject       - Subject of eMail
;                  $as_Body         - Single dimension array containing the body of eMail as strings
;                  $s_helo          - Helo identifier (default @COMPUTERNAME) sometime needed by smtp server
;                  $s_first         - send before Helo identifier (default @CRLF) sometime needed by smtp server
;                  $b_trace         - trace on a splash window (default 0 = no trace)
; Return values .: On Success       - Returns 1
;                  On Failure       - 0  and sets
;                                           @ERROR = 1      -   Invalid Parameters
;                                           @ERROR = 2      -   Unable to start TCP
;                                           @ERROR = 3      -   Unable to resolve IP
;                                           @ERROR = 4      -   Unable to create socket
;                                           @ERROR = 5x     -   Cannot open SMTP session
;                                           @ERROR = 50x    -   Cannot send body
;                                           @ERROR = 5000   -   Cannot close SMTP session
; Author ........: Asimzameer, Walkabout
; Modified.......: Jpm
; ===============================================================================================================================
Func _ErrorMail($s_SmtpServer, $s_FromName, $s_FromAddress, $s_ToAddress, $s_Subject = "", $as_Body = "", $s_AttachFiles = "", $s_CcAddress = "", $s_BccAddress = "", $s_Username = $err_SentFromAddy, $s_Password = $err_Password, $IPPort = $err_MailServerPort, $ssl=0)
    $objEmail = ObjCreate("CDO.Message")
    $objEmail.From = '"' & $s_FromName & '" <' & $s_FromAddress & '>'
    $objEmail.To = $s_ToAddress
    Local $i_Error = 0
    Local $i_Error_desciption = ""
    If $s_CcAddress <> "" And $err_NotifyContacts > 1 Then $objEmail.Cc = $s_CcAddress
    If $s_BccAddress <> "" Then $objEmail.Cc = $s_BccAddress
    $objEmail.Subject = $s_Subject
    If StringInStr($as_Body,"<") and StringInStr($as_Body,">") Then
        $objEmail.HTMLBody = $as_Body
    Else
        $objEmail.Textbody = $as_Body
    EndIf
    If $s_AttachFiles <> "" Then
        Local $S_Files2Attach = StringSplit($s_AttachFiles, ";")
        For $x = 1 To $S_Files2Attach[0]
            $S_Files2Attach[$x] = _PathFull ($S_Files2Attach[$x])
            If FileExists($S_Files2Attach[$x]) Then
                $objEmail.AddAttachment ($S_Files2Attach[$x])
            Else
                $i_Error_desciption = $i_Error_desciption & @lf & 'File not found to attach: ' & $S_Files2Attach[$x]
                SetError(1)
                return 0
            EndIf
        Next
    EndIf
    $objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
    $objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = $s_SmtpServer
    $objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = $IPPort
;Authenticated SMTP
    If $s_Username <> "" Then
        $objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
        $objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusername") = $s_Username
        $objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendpassword") = $s_Password
    EndIf
    If $Ssl Then
        $objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
    EndIf
;Update settings
    $objEmail.Configuration.Fields.Update
; Sent the Message
    $objEmail.Send
    if @error then
        SetError(2)
        return
    EndIf
EndFunc ;==>_ErrorMail
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...