Pregunta

Rebol's error! type comes back as an object you can inspect and extract properties out of.

>> result: try [1 / 0]
** Math error: attempt to divide by zero
** Where: / try
** Near: / 0

>> probe result
make error! [
    code: 400
    type: 'Math
    id: 'zero-divide
    arg1: none
    arg2: none
    arg3: none
    near: [/ 0]
    where: [/ try]
]
...

Note that when that error bubbles up to the console and is the last value of the chain of the evaluation, it turns it into a string and presents it to the user. e.g. "Math error: attempt to divide by zero".

How do I generate this string in my own code? I know I can dig into the system object and find those strings, and try to put it together myself. But isn't there some official function that ships in the binary to do this?

¿Fue útil?

Solución

In Rebol 3, you can simply use FORM to convert an error! object into its pretty-printed representation:

>> err: try [1 / 0]
...

>> form err
== {** Math error: attempt to divide by zero
** Where: / try
** Near: / 0
}

Otros consejos

For Rebol2 you can use the following function (I took it from Doc or Gabriele IIRC)

form-error: func [
    "Forms an error message"
    errobj [object!] "Disarmed error"
    /all "Use the same format as the REBOL console"
    /local errtype text where
][
    errtype: system/error/(errobj/type)
    text: errtype/(errobj/id)
    if block? text [text: reform bind text errobj]
    either all [
        where: get in errobj 'where
        rejoin [
            "** " errtype/type ": " text newline
            either :where [join "** Where: " [mold :where newline]] [""]
            either errobj/near [join "** Near: " [mold/only errobj/near newline]] [""]
        ]
    ] [text]
]

you need to disarm your error:

>> print form-error/all disarm try [1 / 0]
** Math Error: Attempt to divide by zero
** Near: 1 / 0

You can create your own errors like this, where the 'do on the error object arms the error.

make-sl4a-error: func [
    message
][
    do make error! [
        type: 'Access
        id: 'Protocol
        arg1: message
    ]
]

In Rebol 2, you need to disarm the error before forming

>> err: try [ 1 / 0 ]
>> print mold disarm err

make object! [
    code: 400
    type: 'math
    id: 'zero-divide
    arg1: none
    arg2: none
    arg3: none
    near: [1 / 0]
    where: none
]
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top