Error handling question...

Hi all!
Just wondering if there are any good techniques for error handling? I’m trying to write a handler which will display a specific alert dialog depending on the error number. For example, say my script generates a error number 500, I’d like my handler to be able to go directly to the handler that will display the relevant dialog.
I can’t find anything helpful in the ASLG and AppleScript in a Nutshell (O’Reilly) doesn’t seem to give example of what I want to do either :frowning:
I guess what I’m trying to do is write some code that replaces the usual -
if errNum = 500 then
– display relevant dialog
else if errNum = 501 then
– display relevant dialog

else if errNum = 599 then
– display relevant dialog
end if
I’ve been trying to figure out if I could use a list somehow!? I’m really stuck on this but I’m convinced there must be a better way than a huge if…then…else statement!
I hope this all makes sense!
Many thanks,
Juerg

global ErrList --the standard list of errors (that Apple has or should have (but did not?) published)

on error theErr
display dialog "Error " & theErr & ": " & return & item theErr of ErrList buttons {"Whack!"} default button 1 with icon 0
end error

Eelco Houwink

: global ErrList --the standard list of errors (that Apple has or should have
: (but did not?) published)
: on error theErr
: display dialog "Error " & theErr & ": " &
: return & item theErr of ErrList buttons {“Whack!”} default
: button 1 with icon 0
: end error
: Eelco Houwink
Hi Eelco!
Um, I couldn’t get that code to work :frowning: It won’t compile as it is and I’m not sure what ‘ErrList’ is meant to be!?
Is there any chance of an explanation? Sorry for being an idiot! :slight_smile:
Thanks,
Juerg

Juerg,
It is long ago, but I recall that I used to have a list with 200 entries, all text explanations of standard Apple errorcodes.
Part of these were Applescript-related.
It should be somewhere in the public domain.
You’d be surprised to see what all can go wrong.
Anyway since then, I do not fly anymore, at least, not with a laptop.
Eelco Houwink

: Juerg,
: It is long ago, but I recall that I used to have a list with 200 entries, all
: text explanations of standard Apple errorcodes.
: Part of these were Applescript-related.
: It should be somewhere in the public domain.
: You’d be surprised to see what all can go wrong.
: Anyway since then, I do not fly anymore, at least, not with a laptop.
: Eelco Houwink
Right! I’ll see what I can find :slight_smile:
Thanks for your help,
Juerg

: Right! I’ll see what I can find :slight_smile:

: Thanks for your help,

: Juerg

try

-- your script statements here

on error ErrorText number ErrorNumber
	HandleError(ErrorText, ErrorNumber) of me

end try

-- this is the error handler

on HandleError(ErrorText, ErrorNumber)


if ErrorNumber is not -128 then
		beep -- optional
		display dialog Error_ & ErrorNumber & " " & return & return & ¬
			ErrorText buttons {_OK_} default button 1 with icon 0
	end if

error number -128
end HandleError

Sorry, here’s the correct syntax:

try



-- your script statements here



on error ErrorText number ErrorNumber

	HandleError(ErrorText, ErrorNumber) of me



end try



-- this is the error handler



on HandleError(ErrorText, ErrorNumber)





if ErrorNumber is not -128 then

		beep -- optional

		display dialog "Error: " & ErrorNumber & " " & return & return & ¬

			ErrorText buttons {"OK"} default button 1 with icon 0

	end if



error number -128

end HandleError

: Hi all!

: Just wondering if there are any good techniques for error handling? I’m
: trying to write a handler which will display a specific alert dialog
: depending on the error number. For example, say my script generates a
: error number 500, I’d like my handler to be able to go directly to the
: handler that will display the relevant dialog.

: I can’t find anything helpful in the ASLG and AppleScript in a Nutshell
: (O’Reilly) doesn’t seem to give example of what I want to do either :frowning:

: I guess what I’m trying to do is write some code that replaces the usual -

: if errNum = 500 then

: – display relevant dialog

: else if errNum = 501 then

: – display relevant dialog

: …

: else if errNum = 599 then

: – display relevant dialog

: end if

: I’ve been trying to figure out if I could use a list somehow!? I’m really
: stuck on this but I’m convinced there must be a better way than a huge
: if…then…else statement!

You need two lists, one containing the error codes (as text strings) and another
containing your error messages in corresponding positions. The script uses the “collect items of” command from Akua Sweets to determine the position of the error number in the first list:property errNbrList : {“101”, “102”, “103”}
property errMsgList : {“Msg for error 101”, “Msg for error 102”, “Msg for error 103”}

try
        --something
on error errMsg number errNbr
        handleError(errNbr as text, errMsg)
end try

on handleError(errNbr, stdMsg)
        set errPos to collect items of errNbrList that match errNbr
        if errPos is not {} then
                set errPos to item 1 of errPos
                set errMsg to item errPos of errMsgList
        else
                set errMsg to stdMsg
        end if
        display dialog "Error Number: " & errNbr & return & return & errMsg ¬
                buttons {"OK"} default button 1 with icon stop
        error number -128
end handleError

Please note that this is untested code. I just wrote it off the top of my head, but it ought to be pretty close.
Marc K. Myers
Marc@AppleScriptsToGo.com
AppleScriptsToGo
4020 W.220th St.
Fairview Park, OH 44126
(440) 331-1074

[4/24/02 2:56:18 AM]

: Hi Marc!

: Thanks for the code - I think this is more or less what I’d envisioned.
: Ideally I’m after a vanilla solution so I guess I’ll rewrite it a bit but
: this is a big help!

: I wasn’t too sure about having huge lists of error message strings but as you
: don’t mention it, I assume this isn’t going to be problem.

There’s a vanilla technique for finding the index of an item in a list of text strings. It involves converting the list to a string and getting the offset of the target substring, but I don’t remember the details. Can anyone help?
Marc K. Myers
Marc@AppleScriptsToGo.com
AppleScriptsToGo
4020 W.220th St.
Fairview Park, OH 44126
(440) 331-1074

[4/24/02 5:04:43 PM]

: There’s a vanilla technique for finding the index of an item in a list of
: text strings. It involves converting the list to a string and getting the
: offset of the target substring, but I don’t remember the details. Can
: anyone help?
Marc K. Myers < Marc@AppleScriptsToGo.com >
: AppleScriptsToGo
: 4020 W.220th St.
: Fairview Park, OH 44126
: (440) 331-1074
: [4/24/02 5:04:43 PM]

There might be something on The Little Page of Beta AppleScripts that does it.

Rob

Would some kind soul please enlighten me. What do you get from these lists that you don’t get from the standard “on error errTxt number errNum / display dialog…etc”?
[color=#FF00FF:000]Andreas[/color:000]

: Would some kind soul please enlighten me. What do you get from these lists
: that you don’t get from the standard “on error errTxt number errNum /
: display dialog…etc”?
: Andreas
In this case, I believe someone is going to roll there own error codes.
These might be errors which are unique to the script due to user
interaction and not caused by run time errors.
Rob (whose scripts are so perfect that they never produce errors) :stuck_out_tongue:

: Hi all!
: Just wondering if there are any good techniques for error handling? I’m
: trying to write a handler which will display a specific alert dialog
: depending on the error number. For example, say my script generates a
: error number 500, I’d like my handler to be able to go directly to the
: handler that will display the relevant dialog.
: I can’t find anything helpful in the ASLG and AppleScript in a Nutshell
: (O’Reilly) doesn’t seem to give example of what I want to do either :frowning:
: I guess what I’m trying to do is write some code that replaces the usual -
: if errNum = 500 then
: – display relevant dialog
: else if errNum = 501 then
: – display relevant dialog
: …
: else if errNum = 599 then
: – display relevant dialog
: end if
: I’ve been trying to figure out if I could use a list somehow!? I’m really
: stuck on this but I’m convinced there must be a better way than a huge
: if…then…else statement!
If the error numbers are all positive (and if not, you could manage them to be by adding a constant), you might consider putting all your custom errormessages in one huge global list and access them by index; like on error (errnum) display dialog (item errnum of myerrlist) end error

Hi Marc!
Thanks for the code - I think this is more or less what I’d envisioned. Ideally I’m after a vanilla solution so I guess I’ll rewrite it a bit but this is a big help!
I wasn’t too sure about having huge lists of error message strings but as you don’t mention it, I assume this isn’t going to be problem.
Thanks for your help,
Juerg

: Try the link below.
: --tet
That’s what I found! :slight_smile:
Cheers!
Juerg

:

try
: – your script statements here
: on error ErrorText number ErrorNumber
: HandleError(ErrorText, ErrorNumber) of me
: end try
: – this is the error handler
: on HandleError(ErrorText, ErrorNumber)
: if ErrorNumber is not -128 then
: beep – optional
: display dialog Error_ & ErrorNumber & " " & return
: & return & ¬
: ErrorText buttons {OK} default button 1 with icon 0
: end if
: error number -128
: end HandleError

Hi there!

Thanks for that!

It’s useful to see the correct syntax for error handling as I’ve never really looked at it before! (Ooops!) With the other posts I think I should be able to achieve my goal.

I think I’ll go have a play and maybe if I get stuck I’ll post back here :slight_smile:

Thanks again for your time,

Cheers,

Juerg

: If the error numbers are all positive (and if not, you could manage them to
: be by adding a constant), you might consider putting all your custom
: errormessages in one huge global list and access them by index; like on
: error (errnum) display dialog (item errnum of myerrlist) end error
Hey Karl!
I’m pretty sure this is the way to go! My errors will be numbered from 501 onwards - I understand these aren’t used by the OS or anything else.
I guess I’ll just use something like (errNum - 500) to get the item of the error string list.
Thanks for your help,
Juerg

Thanks, Rob, for bringing me Enlightenment.
Andreas
Oh, and - SNAP!

: There’s a vanilla technique for finding the index of an item in a list of
: text strings. It involves converting the list to a string and getting the
: offset of the target substring, but I don’t remember the details. Can
: anyone help?
Marc K. Myers < Marc@AppleScriptsToGo.com >
: AppleScriptsToGo
: 4020 W.220th St.
: Fairview Park, OH 44126
: (440) 331-1074
: [4/24/02 5:04:43 PM]

Yeah, I’m sure I’ve seen this sort of thing before, I just can’t find anything about it now! I’ve tried doing it myself but haven’t been able to manage it yet. The only way I could think of was to create a list of the string lengths, then create a string of the list of strings, get the offset of the required string in this list and somehow, using the string lengths list, get the offset from there!? Maybe…

Well, I know what I mean anyway! :slight_smile:

I may have another go at it…

Cheers,

Juerg

: In this case, I believe someone is going to roll there own error codes.
: These might be errors which are unique to the script due to user
: interaction and not caused by run time errors.
: Rob (whose scripts are so perfect that they never produce errors) :stuck_out_tongue:
Hi Rob!
Yeah, spot on! I’m writing a script which will require the user to input up to 10 values and I need the error handler when validating their input. You see I’m trying to support 8 different unit types (mm, cm, pt, picas etc). So if they enter 10aj, my script will display an alert informing them that it doesn’t support ‘aj’ as a unit type.
I will indeed be requiring my own error messages and numbers so it seems a list is by far the best vanilla option. I’m gradually getting there! I even have a handler which will insert parameters into the error text. (Hey, this is quite good for me! :slight_smile:
Cheers,
Juerg