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


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Let me reformulate this so it is crystal clear. “Using exceptions to handle edge cases is not the way to go”.</p>
<p>As for minority, let’s stay educated and not generalise based on some subjective experience.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="298695" 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/middleware-like-plug/57779/12">Post #11</a>
	                </div>
	            </div>
              <div id="likers-container-298695" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="298695"
                     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="298701" data-post-id="298701">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="whoiswentz" data-post="3" data-topic="57779">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/whoiswentz/48/15314_2.png" class="avatar"> whoiswentz:</div>
<blockquote>
<p>I already tried the Plug.ErrorHandler, but it re-raises the error as the <a href="https://hexdocs.pm/plug/Plug.ErrorHandler.html" rel="nofollow">documentation</a> points</p>
<p>I need a solution to enrich the error message and return a JSON</p>
</blockquote>
</aside>
<p>How did you try that? Because the http request should receive whatever you send as response in the handler. It shouldn’t matter that the error is reraised (e.g. to get properly logged).</p>
<p>See <a href="https://hexdocs.pm/phoenix/1.7.7/Phoenix.ConnTest.html#assert_error_sent/2" class="inline-onebox" rel="nofollow">Phoenix.ConnTest — Phoenix v1.7.7</a> for testing those.</p>
<aside class="quote no-group" data-username="D4no0" data-post="5" data-topic="57779" 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/d4no0/48/33624_2.png" class="avatar"> D4no0:</div>
<blockquote>
<p>Raising an error is not the way you do things in elixir.</p>
<p>Instead of raising, you can return a tuple in the format <code>{:error, :not_found}</code> and handle it in a <a href="https://hexdocs.pm/phoenix/Phoenix.Controller.html#action_fallback/1" rel="nofollow">fallback controller </a>.</p>
</blockquote>
</aside>
<p>While the latter is a viable option the former is not generally true. Exceptions are the only way to handle errors from LV right now (via Plug.ErrorHandler) and phoenix itself for a long time didn’t have <code>action_fallback</code> while already having means of handling exceptions.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="298701" 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/middleware-like-plug/57779/13">Post #12</a>
	                </div>
	            </div>
              <div id="likers-container-298701" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="298701"
                     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="298770" data-post-id="298770">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>My understanding (albeit potentially flawed) is that there are three methods for handling errors, and two places for defining the error response in Phoenix:</p>
<ol>
<li>Return an <code>:error</code> tuple in the controller and use the fallback controller, as <a class="mention" href="/u/d4no0" rel="nofollow">@D4no0</a> mentioned. Unfortunately, this is not helpful for you, as you’re dealing with a raised <code>Ecto.NoResultsError</code>.</li>
<li>Handle the exception one level up in the router using <code>Plug.ErrorHander</code>, as <a class="mention" href="/u/tfwright" rel="nofollow">@tfwright</a> mentioned. However, I believe that this is more designed for handling side effects, as the error is reraised to be handled by point 3 below.</li>
<li>Use the error view. Plug wraps up all exceptions that occur during the request/response, and these are <a href="https://github.com/phoenixframework/phoenix/blob/246ca7726e14ed94d3d878838cbd6add5a0df3e9/lib/phoenix/endpoint.ex#L645" rel="noopener nofollow ugc">caught by Phoenix in the Endpoint</a><img src="https://forum.elixirforum.com/images/emoji/apple/magic_wand.png?v=15" title=":magic_wand:" class="emoji" alt=":magic_wand:" loading="lazy" width="20" height="20">, which then <a href="https://github.com/phoenixframework/phoenix/blob/246ca7726e14ed94d3d878838cbd6add5a0df3e9/lib/phoenix/endpoint/render_errors.ex#L126C24-L126C24" rel="noopener nofollow ugc">renders a special error view</a><img src="https://forum.elixirforum.com/images/emoji/apple/magic_wand.png?v=15" title=":magic_wand:" class="emoji" alt=":magic_wand:" loading="lazy" width="20" height="20">. You can customise this to return the JSON response you like - I think this is the best solution. See the <a href="https://hexdocs.pm/phoenix/custom_error_pages.html#the-error-views" rel="noopener nofollow ugc">documentation</a>.</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="298770" 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/middleware-like-plug/57779/14">Post #13</a>
	                </div>
	            </div>
              <div id="likers-container-298770" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="298770"
                     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="298773" data-post-id="298773">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="adamu" data-post="14" data-topic="57779">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/adamu/48/31482_2.png" class="avatar"> adamu:</div>
<blockquote>
<p>Use the error view. Plug wraps up all exceptions that occur during the request/response, and these are <a href="https://github.com/phoenixframework/phoenix/blob/246ca7726e14ed94d3d878838cbd6add5a0df3e9/lib/phoenix/endpoint.ex#L645" rel="nofollow">caught by Phoenix in the Endpoint</a><img src="https://forum.elixirforum.com/images/emoji/apple/magic_wand.png?v=15" title=":magic_wand:" class="emoji" alt=":magic_wand:" loading="lazy" width="20" height="20">, which then <a href="https://github.com/phoenixframework/phoenix/blob/246ca7726e14ed94d3d878838cbd6add5a0df3e9/lib/phoenix/endpoint/render_errors.ex#L126C24-L126C24" rel="nofollow">renders a special error view</a><img src="https://forum.elixirforum.com/images/emoji/apple/magic_wand.png?v=15" title=":magic_wand:" class="emoji" alt=":magic_wand:" loading="lazy" width="20" height="20">. You can customise this to return the JSON response you like - I think this is the best solution. See the <a href="https://hexdocs.pm/phoenix/custom_error_pages.html#the-error-views" rel="nofollow">documentation</a>.</p>
</blockquote>
</aside>
<p>Phoenix does a bit more, but conceptionally it works exactly the same as <code>Plug.ErrorHandler</code>. It also reraises errors: <a href="https://github.com/phoenixframework/phoenix/blob/246ca7726e14ed94d3d878838cbd6add5a0df3e9/lib/phoenix/endpoint/render_errors.ex#L97" class="inline-onebox" rel="nofollow">phoenix/lib/phoenix/endpoint/render_errors.ex at 246ca7726e14ed94d3d878838cbd6add5a0df3e9 · phoenixframework/phoenix · GitHub</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="298773" 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/middleware-like-plug/57779/15">Post #14</a>
	                </div>
	            </div>
              <div id="likers-container-298773" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="298773"
                     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>