Search code examples
stringexceptionerror-handlingrebolrebol3

How to format Rebol errors as strings?


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?


Solution

  • 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
    }