<turbo-stream action="append" target="posts_list"><template>    <div class="postbit" id="92649" data-post-id="92649">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="mischov" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/mischov/120/2489_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  mischov
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>That is a perfectly valid (and probably often good) approach, though regardless of whether you react to the error itself or the return value of a function that interrogates the error you have a point of coupling.</p>
<p>I find using an error/exception struct in <code>{:error, %Error{}}</code> tuples provides a good, <em>flexible</em> solution for representing errors that can be used simply in <code>with</code> or an <code>action_fallback</code> or any other error handling situation. Tuples or opaque objects or any similar concept could work just as well, I just presented what I’ve found to be a good solution.</p>
<p>I think the important part is that people should strive to return context rich <code>{:error, reason}</code> tuples instead of just returning <code>{:error, :not_found}</code> or even worse, just returning <code>:error</code>.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="92649" data-batch-url="/posts/batch_likers">
                        1
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/i-cant-get-elixir-error-handling-right/15843/13">Post #12</a>
	                </div>
	            </div>
              <div id="likers-container-92649" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="92649"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #12"></div>
  </section>
</div>
    <div class="postbit" id="92654" data-post-id="92654">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="peerreynders" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/peerreynders/120/5826_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  peerreynders
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="mischov" data-post="13" data-topic="15843">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/mischov/48/2489_2.png" class="avatar"> mischov:</div>
<blockquote>
<p>whether you react to the error itself or the return value of a function that interrogates the error you have a point of coupling.</p>
</blockquote>
</aside>
<p>Fair enough - though under particular circumstances coupling to functions can make more sense - example: protocols.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="92654" data-batch-url="/posts/batch_likers">
                        1
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/i-cant-get-elixir-error-handling-right/15843/14">Post #13</a>
	                </div>
	            </div>
              <div id="likers-container-92654" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="92654"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #13"></div>
  </section>
</div>
    <div class="postbit" id="134793" data-post-id="134793">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="ericridgeway" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/ericridgeway/120/17194_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  ericridgeway
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group quote-modified" data-username="peerreynders" data-post="7" data-topic="15843">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/peerreynders/48/5826_2.png" class="avatar"> peerreynders:</div>
<blockquote>
<p>… with {:ok, limit} ← parse_limit(request), {:ok, offset} ← parse_offset(request, limit), {:ok, filters} ← parse_filters(request, limit, offset) do # send the content back else error → handle_parse_error(error) end end def handle_parse_error({:error, {:parse_limit, message}}) do # handle parse_limit error … end def handle_parse_error({:error, {:parse_offset, message, limit}}) do # handle parse_offset error … end def handle_parse_error({:error, {:parse_filters, message, limit, offset}}) do # handle parse_filters error … end</p>
</blockquote>
</aside>
<p>But this doesn’t work, does it? <code>handle_parse_error</code> is assuming those 3 functions return tuples that include their own function name in an inner tuple?</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">def parse_limit(request) do
  ..
  {:error, {:parse_limit, :request_had_typo_or_something}}
end
</code></pre>
<p>Instead of the more usual (I thought?) <code>{:error, :request_had_typo_or_something}</code></p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="134793" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/i-cant-get-elixir-error-handling-right/15843/15">Post #14</a>
	                </div>
	            </div>
              <div id="likers-container-134793" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="134793"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #14"></div>
  </section>
</div>
    <div class="postbit" id="134796" data-post-id="134796">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="mischov" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/mischov/120/2489_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  mischov
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group quote-modified" data-username="ericridgeway" data-post="15" data-topic="15843" data-full="true">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/ericridgeway/48/17194_2.png" class="avatar"> ericridgeway:</div>
<blockquote>
<p>This doesn’t work, does it?</p>
</blockquote>
</aside>
<p>What doesn’t work?</p>
<aside class="quote no-group" data-username="ericridgeway" data-post="15" data-topic="15843" data-full="true">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/ericridgeway/48/17194_2.png" class="avatar"> ericridgeway:</div>
<blockquote>
<p>Instead of the more usual (I thought?) <code>{:error, :request_had_typo_or_something}</code></p>
</blockquote>
</aside>
<p>The problem with <code>{:error, :reason_atom}</code> is that it doesn’t have a lot of context. If you’re handling that error exactly at the point where the problem occurs then you probably have enough context, but if you’re dealing with the error somewhere else, such as a <code>FallbackController</code>, then you’ll generally want to make your errors richer.</p>
<p>For example, if you’re not at the point the error occurred the <code>:error</code> returned by <code>Map.fetch</code> is probably less useful than <code>{:error, required_parameter_not_found}</code>, which is in turn less useful than <code>{:error, {:required_parameter_not_found, :i_am_a_required_parameter}}</code>.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="134796" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/i-cant-get-elixir-error-handling-right/15843/16">Post #15</a>
	                </div>
	            </div>
              <div id="likers-container-134796" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="134796"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #15"></div>
  </section>
</div>
    <div class="postbit" id="134815" data-post-id="134815">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="peerreynders" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/peerreynders/120/5826_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  peerreynders
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="ericridgeway" data-post="15" data-topic="15843">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/ericridgeway/48/17194_2.png" class="avatar"> ericridgeway:</div>
<blockquote>
<p><code>handle_parse_error</code> is assuming those 3 functions return tuples that include their own function name in an inner tuple?</p>
</blockquote>
</aside>
<p>Another variation on the same theme (see <a href="https://forum.elixirforum.com/t/i-cant-get-elixir-error-handling-right/15843/3" rel="nofollow">previous post</a>):</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">...
  with {:parse_limit, {:ok, limit}} &lt;- {:parse_limit, parse_limit(request)},
       {:parse_offset, limit, {:ok, offset}} &lt;- {:parse_offset, limit, parse_offset(request)},
       {:parse_filters, limit, offset, {:ok, filters}} &lt;- {:parse_filters, limit, offset, parse_filters(request)} do
     # send the content back
  else
    error -&gt; handle_parse_error(error)
  end
end

def handle_parse_error({:parse_limit, {:error, message}}) do
  # handle parse_limit error
  ...
end
def handle_parse_error({:parse_offset, limit, {:error, message}}) do
  # handle parse_offset error
  ...
end
def handle_parse_error({:parse_filters, limit, offset, {:error, message}}) do
  # handle parse_filters error
  ...
end
</code></pre>
<aside class="quote quote-modified" data-post="1" data-topic="12955">
  <div class="title">
    <div class="quote-controls"></div>
    <img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/kurisu/48/14820_2.png" class="avatar">
    <div class="quote-title__text-content">
      <a href="https://forum.elixirforum.com/t/how-can-i-access-variable-defined-in-with-statement-from-its-else-block/12955" rel="nofollow">How can I access variable defined in "With" statement from its "else" block?</a> <a class="badge-category__wrapper " href="/c/questions-help/questions/53" rel="nofollow"><span data-category-id="53" style="--category-badge-color: #C14BFB; --category-badge-text-color: #000000; --parent-category-badge-color: #C14BFB;" data-parent-category-id="171" data-drop-close="true" class="badge-category --style-square --has-parent" title="Elixir Questions / Help"><span class="badge-category__name">Questions</span></span></a>
    </div>
  </div>
  <blockquote>
    Hello forum! 
I’m trying to retrieve an updated conn, returned in the first line of a “with” statement, when second line failed. But I’m getting some error that says new_conn variable doesn’t exist. In the “do” block it is fine though. 
Let me show you what my code looks like: 
def some_action(conn, params) do
    with {:ok1, some_result, new_conn} &lt;- some_func(conn, params),
      {:ok2, another_result} &lt;- another_func(some_result)
    do
      new_conn |&gt; redirect(to: some_path(conn, :index)) …
  </blockquote>
</aside>
 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="134815" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/i-cant-get-elixir-error-handling-right/15843/17">Post #16</a>
	                </div>
	            </div>
              <div id="likers-container-134815" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="134815"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-last-post cat-last-post" title="Last post!"></div>
  </section>
</div>
</template></turbo-stream><turbo-stream action="replace" target="load-more-container"><template><div id="load-more-container" class="load-more-container">
    <span class="all-loaded">— All posts loaded —</span>
</div></template></turbo-stream>