https://redmine.ruby-lang.org/https://redmine.ruby-lang.org/favicon.ico?17113305112019-06-18T22:23:27ZRuby Issue Tracking SystemRuby master - Feature #15936: on_error in lieu of rescue, raisehttps://redmine.ruby-lang.org/issues/15936?journal_id=786802019-06-18T22:23:27Zshevegen (Robert A. Heiler)shevegen@gmail.com
<ul></ul><p>Hmmm.</p>
<p>I have not made up my mind so I can not even say whether this may be interesting<br>
or not.</p>
<p>But I think just a few general thoughts:</p>
<ul>
<li>
<p>People may expect begin/rescue/end, more than any alternatives. They may wonder<br>
what on_error is or how it would be used (or any other name).</p>
</li>
<li>
<p>Is it very common to use ensure/rescue/re-raise? I have no statistical data but<br>
in my own code, but also in code by other people, it seems as if simple begin<br>
rescue clauses are highly prevalent. This should not be assumed as a con opinion,<br>
I am just pointing this out in context as to whether on_error would be worth<br>
to be added (and I honestly do not know).</p>
</li>
</ul>
<p>As for potentially pro-points, if I understood one part of your issue correctly<br>
then you also suggest being able to handle a specific error-case with this<br>
line exactly. Or at the least this is how I understand the code example, where<br>
in the second you can omit one line right?</p>
<p>These are just some semi-random comments from me - as I wrote above, I really<br>
do not even have the slightest idea yet whether I may like, dislike or even<br>
just be neutral on it. :)</p> Ruby master - Feature #15936: on_error in lieu of rescue, raisehttps://redmine.ruby-lang.org/issues/15936?journal_id=786872019-06-19T01:56:14Zkylemacey (Kyle Macey)shout@kylemacey.com
<ul></ul><p>shevegen (Robert A. Heiler) wrote:</p>
<blockquote>
<p>Hmmm.</p>
<p>I have not made up my mind so I can not even say whether this may be interesting<br>
or not.</p>
<p>But I think just a few general thoughts:</p>
<ul>
<li>People may expect begin/rescue/end, more than any alternatives. They may wonder<br>
what on_error is or how it would be used (or any other name).</li>
</ul>
</blockquote>
<p>Yeah, this is a proposal to extend the available keywords in ruby core. So ideally, there would be release notes and documentation that would help guide people to this new feature.</p>
<blockquote>
<ul>
<li>Is it very common to use ensure/rescue/re-raise? I have no statistical data but<br>
in my own code, but also in code by other people, it seems as if simple begin<br>
rescue clauses are highly prevalent. This should not be assumed as a con opinion,<br>
I am just pointing this out in context as to whether on_error would be worth<br>
to be added (and I honestly do not know).</li>
</ul>
</blockquote>
<p>This is coming from a need that I personally face often on the utilities I work on, where I need to update state on an object if something unexpected happens. My company's linter gets upset when I use the <code>rescue StandardError</code> pattern, so I was hoping to have a way to be more explicit that I'm not trying to prevent the error from going up the stack, I just want to act upon the exception.</p>
<blockquote>
<p>As for potentially pro-points, if I understood one part of your issue correctly<br>
then you also suggest being able to handle a specific error-case with this<br>
line exactly. Or at the least this is how I understand the code example, where<br>
in the second you can omit one line right?</p>
</blockquote>
<p>Yeah, there's the nicety of being able to reduce a line, but even more enticing is how explicit this pattern feels, and how it can be less error-prone (by forgetting to reraise, or by accidentally casting too wide of a <code>rescue</code> net).</p>
<blockquote>
<p>These are just some semi-random comments from me - as I wrote above, I really<br>
do not even have the slightest idea yet whether I may like, dislike or even<br>
just be neutral on it. :)</p>
</blockquote> Ruby master - Feature #15936: on_error in lieu of rescue, raisehttps://redmine.ruby-lang.org/issues/15936?journal_id=786892019-06-19T02:31:27Zjeremyevans0 (Jeremy Evans)merch-redmine@jeremyevans.net
<ul></ul><p>kylemacey (Kyle Macey) wrote:</p>
<blockquote>
<p>What would be a thought on using another keyword that doesn't actually <em>rescue</em> an exception, but performs an operation in the event of an error? Similar to <code>ensure</code>, but only in the event of an error.</p>
<pre><code>begin
some_method
on_error StandardError
job.fail!
end
</code></pre>
</blockquote>
<p>Thankfully, Ruby already supports what you want:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="k">begin</span>
<span class="n">some_method</span>
<span class="k">ensure</span>
<span class="n">job</span><span class="p">.</span><span class="nf">fail!</span> <span class="k">if</span> <span class="vg">$!</span> <span class="c1"># or use case $! if you want to handle specific exception classes differently</span>
<span class="k">end</span>
</code></pre>
<p>As you can already accomplish this with current Ruby syntax, I do not think adding a keyword for it is warranted.</p> Ruby master - Feature #15936: on_error in lieu of rescue, raisehttps://redmine.ruby-lang.org/issues/15936?journal_id=786932019-06-19T07:15:33Zduerst (Martin Dürst)duerst@it.aoyama.ac.jp
<ul></ul><p>kylemacey (Kyle Macey) wrote:</p>
<blockquote>
<p>This is coming from a need that I personally face often on the utilities I work on, where I need to update state on an object if something unexpected happens. My company's linter gets upset when I use the <code>rescue StandardError</code> pattern, so I was hoping to have a way to be more explicit that I'm not trying to prevent the error from going up the stack, I just want to act upon the exception.</p>
</blockquote>
<p>What about getting the linter to recognize that you are using <code>raise</code> again in the <code>rescue</code> clause? That shouldn't be too difficult, at least for the simple cases.</p> Ruby master - Feature #15936: on_error in lieu of rescue, raisehttps://redmine.ruby-lang.org/issues/15936?journal_id=787542019-06-20T19:17:24Zkylemacey (Kyle Macey)shout@kylemacey.com
<ul></ul><p>jeremyevans0 (Jeremy Evans) wrote:</p>
<blockquote>
<p>kylemacey (Kyle Macey) wrote:</p>
<blockquote>
<p>What would be a thought on using another keyword that doesn't actually <em>rescue</em> an exception, but performs an operation in the event of an error? Similar to <code>ensure</code>, but only in the event of an error.</p>
<pre><code>begin
some_method
on_error StandardError
job.fail!
end
</code></pre>
</blockquote>
<p>Thankfully, Ruby already supports what you want:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="k">begin</span>
<span class="n">some_method</span>
<span class="k">ensure</span>
<span class="n">job</span><span class="p">.</span><span class="nf">fail!</span> <span class="k">if</span> <span class="vg">$!</span> <span class="c1"># or use case $! if you want to handle specific exception classes differently</span>
<span class="k">end</span>
</code></pre>
<p>As you can already accomplish this with current Ruby syntax, I do not think adding a keyword for it is warranted.</p>
</blockquote>
<p><code>ensure</code> fires even if there is no exception. This keyword would only fire when there is an exception. In your example, <code>job.fail!</code> would always be called.</p>
<p>Edit: Sorry, I missed the conditional, which lends my point that this code isn't as intentional.</p> Ruby master - Feature #15936: on_error in lieu of rescue, raisehttps://redmine.ruby-lang.org/issues/15936?journal_id=787552019-06-20T19:18:33Zkylemacey (Kyle Macey)shout@kylemacey.com
<ul></ul><p>duerst (Martin Dürst) wrote:</p>
<blockquote>
<p>kylemacey (Kyle Macey) wrote:</p>
<blockquote>
<p>This is coming from a need that I personally face often on the utilities I work on, where I need to update state on an object if something unexpected happens. My company's linter gets upset when I use the <code>rescue StandardError</code> pattern, so I was hoping to have a way to be more explicit that I'm not trying to prevent the error from going up the stack, I just want to act upon the exception.</p>
</blockquote>
<p>What about getting the linter to recognize that you are using <code>raise</code> again in the <code>rescue</code> clause? That shouldn't be too difficult, at least for the simple cases.</p>
</blockquote>
<p>Very true! I can certainly do that, I just thought this might have the added benefit of writing more explicit and intentional code, and would eliminate the need to re-raise the exception.</p>