Coerting Errors to a string?
[1/7] from: philb:upnaway at: 21-Dec-2001 8:20
Hi Guys,
Given that my program recieves an error I disarm it, and I have an error object, for
example
err: disarm try [a: 1 / 0]
probe err
make object! [
code: 400
type: 'math
id: 'zero-divide
arg1: none
arg2: none
arg3: none
near: [a: 1 / 0]
where: none
]
How do I convert it to a nicely formatted readable string ?
>> err
** Math Error: Attempt to divide by zero
** Near: a: 1 / 0
[2/7] from: larry:ecotope at: 20-Dec-2001 16:37
Hi Phil,
How about MOLD? It will convert to a string formatted the same as PROBE.
>> print mold :err
make object! [
code: 400
type: 'math
id: 'zero-divide
arg1: none
arg2: none
arg3: none
near: [a: 1 / 0]
where: none
]
-Larry
[3/7] from: nitsch-lists:netcologne at: 21-Dec-2001 3:23
RE: [REBOL] Coerting Errors to a string?
(original by Bo somewhere, called print-error. forgot if i changed something.)
form-error: func [
error [object!]
/local arg1 arg2 arg3 message out
] [
out: make string! 100
set [arg1 arg2 arg3] [error/arg1 error/arg2 error/arg3]
message: get in get in system/error error/type error/id
if block? message [bind message 'arg1]
append out reform reduce message
append out reform ["^/Near:" mold error/near]
append out reform ["^/Where:" mold get in error 'where]
]
use with [print form-error disarm error]
-Volker
[philb--upnaway--com] wrote:
[4/7] from: philb:upnaway at: 21-Dec-2001 10:38
Re: Converting Errors to a string?
Hi Larry,
Well yes I could do that but what I really want to do is to display an alert box of some
kind with a more "user friendly" error message.
By the way the code snippet was for demonstration purposes only ....
My code is more like
if error? lv-err: try [...some code here ....]
[
; process lv-err here
; alert ...something here...
]
Cheers Phil
=== Original Message ===
Hi Phil,
How about MOLD? It will convert to a string formatted the same as PROBE.
>> print mold :err
make object! [
code: 400
type: 'math
id: 'zero-divide
arg1: none
arg2: none
arg3: none
near: [a: 1 / 0]
where: none
]
-Larry
[5/7] from: philb:upnaway at: 21-Dec-2001 11:02
Re: Coerting Errors to a string?
Thanks Volker,
I will use this in my error handling
The next question is am I handling my errors correctly?
For Example
if error? lv-err: try [1 / 0] [Print "Error"] ; works
if error? lv-err try [1 / 2] [Print "Error"] ; works
However
if error? lv-err try [Print "AAA"] [Print "Error"] ; does not work
gives
AAA
** Script Error: lv-err needs a value
** Near: if error? lv-err: try [Print "AAA"]
Cheers Phil
=== Original Message ===
RE: [REBOL] Coerting Errors to a string?
(original by Bo somewhere, called print-error. forgot if i changed something.)
form-error: func [
error [object!]
/local arg1 arg2 arg3 message out
] [
out: make string! 100
set [arg1 arg2 arg3] [error/arg1 error/arg2 error/arg3]
message: get in get in system/error error/type error/id
if block? message [bind message 'arg1]
append out reform reduce message
append out reform ["^/Near:" mold error/near]
append out reform ["^/Where:" mold get in error 'where]
]
use with [print form-error disarm error]
-Volker
[philb--upnaway--com] wrote:
[6/7] from: nitsch-lists:netcologne at: 21-Dec-2001 7:56
RE: [REBOL] Re: Coerting Errors to a string?
use:
if error? lv-err: try [Print "AAA" 'ok] [Print "Error"]
if there is no error, try works like do, returning the last value.
with
if error? lv-err try [1 / 2] [Print "Error"] ; works
you return a number, but with
if error? lv-err try [Print "AAA"] [Print "Error"] ; does not work
you return unset! (the result of a 'print)
so return something, i suggest 'ok here, anything goes.
or use
if error? set/any 'lv-err try [Print "AAA"] [Print "Error"]
which works ok with unset! too.
-Volker
[philb--upnaway--com] wrote:
[7/7] from: philb:upnaway at: 21-Dec-2001 21:27
Re: Converting Errors to a string?
Thanks Volker,
I passed something back in the end .... but using 'ok would be better.
I had never used unset before .... didnt realise that print returns unset .... I learn
something new from this list almost every day ....
Cheers Phil
=== Original Message ===
RE: [REBOL] Re: Coerting Errors to a string?
use:
if error? lv-err: try [Print "AAA" 'ok] [Print "Error"]
if there is no error, try works like do, returning the last value.
with
if error? lv-err try [1 / 2] [Print "Error"] ; works
you return a number, but with
if error? lv-err try [Print "AAA"] [Print "Error"] ; does not work
you return unset! (the result of a 'print)
so return something, i suggest 'ok here, anything goes.
or use
if error? set/any 'lv-err try [Print "AAA"] [Print "Error"]
which works ok with unset! too.
-Volker
[philb--upnaway--com] wrote: