Note that there are some explanatory texts on larger screens.

plurals
  1. PODRY way of re-raising same set of exceptions in multiple places
    primarykey
    data
    text
    <p>short:</p> <p>Is there a way in Ruby to DRY-ify this:</p> <pre><code>def entry_point_one begin do_something rescue MySyntaxErrorOne, MySyntaxErrorTwo, MySyntaxErrorEtc =&gt; syn_err raise syn_err.exception(syn_err.message) end end def entry_point_two begin do_something_else rescue MySyntaxErrorOne, MySyntaxErrorTwo, MySyntaxErrorEtc =&gt; syn_err raise syn_err.exception(syn_err.message) end end </code></pre> <p>longer:</p> <p>I'm building an interpreter. This interpreter can be called using different entry points. If I feed this interpreter a 'dirty' string, I expect it to raise an error. However, it would be nice if I don't get spammed by the by the entire back trace of every method called directly or indirectly by do_something, especially since the interpreter makes use of recursion.</p> <p>As you can see in the above snippet, I already know a way to re raise an error and thereby removing the back trace. What I would like do is remove the duplication in the above example. The closest I have come thus far is this:</p> <pre><code>def entry_point_one re_raise_known_exceptions {do_something} end def entry_point_two re_raise_known_exceptions {do_something_else} end def re_raise_known_exceptions yield rescue MySyntaxErrorOne, MySyntaxErrorTwo, MySyntaxErrorEtc =&gt; syn_err raise syn_err.exception(syn_err.message) end </code></pre> <p>But that makes the method re-raise-known-exceptions show up in the back trace.</p> <p>edit: I guess what I want would be something like a C pre-processing macro</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload