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


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>You can also check this interesting post from <a class="mention" href="/u/michalmuskala" rel="nofollow">@michalmuskala</a> - <a href="https://michal.muskala.eu/post/error-handling-in-elixir-libraries/" class="inline-onebox" rel="noopener nofollow ugc">Error Handling in Elixir Libraries | Michał Muskała</a></p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="219382" data-batch-url="/posts/batch_likers">
                        4
                      </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/how-to-handle-error-handling-boilerplate/40898/12">Post #11</a>
	                </div>
	            </div>
              <div id="likers-container-219382" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="219382"
                     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 #11"></div>
  </section>
</div>
    <div class="postbit" id="219386" data-post-id="219386">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="verkhovin" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/verkhovin/120/23451_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  verkhovin
                    <span class="op-star" title="Thread Starter">
                      <img alt="OP" class="op-star-icon" src="/assets/thread-icons/thread-icon-thread-starter-df91e872.png" />
                    </span>
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Nice post too, thank you!</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="219386" 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/how-to-handle-error-handling-boilerplate/40898/13">Post #12</a>
	                </div>
	            </div>
              <div id="likers-container-219386" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="219386"
                     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="219387" data-post-id="219387">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="verkhovin" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/verkhovin/120/23451_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  verkhovin
                    <span class="op-star" title="Thread Starter">
                      <img alt="OP" class="op-star-icon" src="/assets/thread-icons/thread-icon-thread-starter-df91e872.png" />
                    </span>
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>So I’ve read through the articles <a class="mention" href="/u/soup" rel="nofollow">@soup</a> and <a class="mention" href="/u/joaquinalcerro" rel="nofollow">@joaquinalcerro</a> referred.</p>
<p>The main idea I took from these articles that in general raising an exception is not a bad practice. Moreover, you <strong>should</strong> raise an exception if the error breaks your application invariants so you don’t have to bother with unnecessary error handling on each application level (see the example I provided here: <a href="https://forum.elixirforum.com/t/how-to-handle-error-handling-boilerplate/40898/9" class="inline-onebox" rel="nofollow">How to handle error handling boilerplate - #9 by verkhovin</a>).</p>
<p><code>{:ok, result}</code>/ <code>{:error / reason}</code> way is still very useful when errors are part of your business logic and can somehow determine the application’s execution flow.<br>
They are also useful if you do not know exactly how your module will be used by a callers side. i.e. It is the case when you develop some API for a library.</p>
<p>Basically, it is what <a class="mention" href="/u/hauleth" rel="nofollow">@hauleth</a> said in the first reply to this topic.</p>
<p>Thank you all for your help! The community seems really friendly, it was a real pleasure to read all your answers!</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="219387" data-batch-url="/posts/batch_likers">
                        2
                      </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/how-to-handle-error-handling-boilerplate/40898/14">Post #13</a>
	                </div>
	            </div>
              <div id="likers-container-219387" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="219387"
                     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="219390" data-post-id="219390">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Hi! Here is how I handle errors in the MVC application.</p>
<ol>
<li>You first get Ecto errors in Business Layer. Do not just case error in order to pass it to the upper layer. If in a specific layer you can not do anything about this error, do not pattern match on it. It will be propagated to the upper layer.</li>
<li>Do pattern match on errors in the controller layer. Handle it by passing some meaningful error to the user of the controller.</li>
</ol> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="219390" 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/how-to-handle-error-handling-boilerplate/40898/15">Post #14</a>
	                </div>
	            </div>
              <div id="likers-container-219390" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="219390"
                     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="219467" data-post-id="219467">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="amnu3387" src="/assets/icons/user-9f439610.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  amnu3387
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I personally am very inclined to force explicitness and use more verbosity to describe flows. When using <code>with</code> I normally resort to tagging the branches, but sometimes you do need to pass through the underlying returns from the failing branch. When writing API endpoints I’ve settled in just having a <code>%Response{}</code>/<code>%Output{}</code> struct with <code>:errors</code>, <code>:notices</code>, <code>:payload</code> fields that the frontend knows how to interpret - this approach requires you to do it almost from the beginning and be in control also of how the front-end interacts. It’s also not completely feasible for <code>html</code> pipelines, as you can have redirects and so on, but could probably be made so.</p>
<p>Controllers/channels call only into context functions, and these entry points in the contexts either return those payload structs, or they return normal results and I have a translation module/functions for the returns. Again, it’s more boilerplaty.</p>
<p>But the issue that I think deserves a bit more thought is not that raising exceptions in themselves is bad (outside of things that really require raising, like a db disconnect, or inaccessibility of a resource, or inside a loop/recursion that no longer makes sense, but usually there you use <code>throw</code>), when you look at the flow of a single entry point, it can usually look clearer and in a way less verbose. The problem comes when all your code base applies this style. Then it basically becomes a mine field because everything can raise somewhere and you’re left having to keep in mind every detail of all functions you’re calling and what those themselves are calling.</p>
<p>When I have too much time to think about this nonsense I imagine I would like to see a variation of <code>with</code> (a special form named <code>weave</code> or <code>steps</code>) that would look like this:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">weave # notice the new line 
   [not_found: %User{} = user] &lt;- Db.Repo.get(User, user_id),
   [not_authorized: true] &lt;- Contexts.Authorization.is_authorized?(user, some_action),
   [changeset: {:ok, %User{} = new_user}] &lt;- Contexts.Users.update(user, some_action)
do
   {:ok, user}
else
  [changeset: changeset] -&gt; {:error, changeset}
  [{error, _}] -&gt; {:error, error}
end
</code></pre>
<p>Perhaps it could even be that when the <code>do</code> block was ommitted it would pass the last value, without the keyword.</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">weave 
   [not_found: %User{} = user] &lt;- Db.Repo.get(User, user_id),
   [not_authorized: true] &lt;- Contexts.Authorization.is_authorized?(user, some_action),
   [changeset: {:ok, %User{} = new_user}] &lt;- Contexts.Users.update(user, some_action)
else
  [changeset: changeset] -&gt; {:error, changeset}
  [{error, _}] -&gt; {:error, error}
end
</code></pre>
<p>This can be confusing the first time you encounter it (<code>not_authorized: true</code>) but afterwards it’s simple to understand.</p>
<p>Other option would be:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">weave
  %User{} = user &lt;- Db.Repo.get(User, user_id) -&gt; :not_found,
  true &lt;- Contexts.Authorization.is_authorized?(user, some_action) -&gt; :not_authorized,
  {:ok, %User{} = new_user} &lt;- Contexts.Users.update(user, some_action) -&gt; :changeset
else
  [changeset: changeset] -&gt; {:error, changeset}
  [{error, _}] -&gt; {:error, error}
end</code></pre> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="219467" data-batch-url="/posts/batch_likers">
                        3
                      </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/how-to-handle-error-handling-boilerplate/40898/16">Post #15</a>
	                </div>
	            </div>
              <div id="likers-container-219467" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="219467"
                     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>