> On Mar 31, 2017, at 1:00 PM, David Storrs <[email protected]> wrote:
> 
> In Perl I can use caller() [1] to access the stack so that I can have _baz 
> report errors as though the error were coming from 'foo' or 'bar' as 
> appropriate -- which is what the user expects to see, since their code called 
> 'foo' or 'bar', not '_baz'.  (Even better, I could report as though the error 
> were coming from the *caller* of foo / bar, which is the user's actual code 
> and the real source of the problem.) 

An exception propagates upward through the calling chain. So rather than 
immediately using `raise-arguments-error` and launching it to the top, you 
could raise the error in two steps:

1) create a new error type (say, `(struct _baz-error (msg))`, so it has a 
unique predicate). You can also inherit from something in the `exn` hierarchy.

2) raise this error within `_baz`: `(raise (_baz-error "whoops"))`

3) catch the error within `foo` or `bar` using `with-handlers`:

`(with-handlers ([_baz-error? (λ (exn) (handle-baz-error exn)])
   ···)`

Where `handle-baz-error` maybe re-raises the error with `raise-argument-errors` 
using the name of the current function.

4) Or following your "even better" idea, put the `with-handlers` in the caller 
of `foo` / `bar`.

Here's an example of this approach:

http://beautifulracket.com/basic-2/better-line-errors.html 
<http://beautifulracket.com/basic-2/better-line-errors.html>

5) You could extend this idea to more than two steps, where you collect other 
names in the calling chain on the way up.

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to