관리-도구
편집 파일: ShowExceptions.html
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>class Rack::ShowExceptions - rack-2.2.10 Documentation</title> <script type="text/javascript"> var rdoc_rel_prefix = "../"; var index_rel_prefix = "../"; </script> <script src="../js/navigation.js" defer></script> <script src="../js/search.js" defer></script> <script src="../js/search_index.js" defer></script> <script src="../js/searcher.js" defer></script> <script src="../js/darkfish.js" defer></script> <link href="../css/fonts.css" rel="stylesheet"> <link href="../css/rdoc.css" rel="stylesheet"> <body id="top" role="document" class="class"> <nav role="navigation"> <div id="project-navigation"> <div id="home-section" role="region" title="Quick navigation" class="nav-section"> <h2> <a href="../index.html" rel="home">Home</a> </h2> <div id="table-of-contents-navigation"> <a href="../table_of_contents.html#pages">Pages</a> <a href="../table_of_contents.html#classes">Classes</a> <a href="../table_of_contents.html#methods">Methods</a> </div> </div> <div id="search-section" role="search" class="project-section initially-hidden"> <form action="#" method="get" accept-charset="utf-8"> <div id="search-field-wrapper"> <input id="search-field" role="combobox" aria-label="Search" aria-autocomplete="list" aria-controls="search-results" type="text" name="search" placeholder="Search" spellcheck="false" title="Type to search, Up and Down to navigate, Enter to load"> </div> <ul id="search-results" aria-label="Search Results" aria-busy="false" aria-expanded="false" aria-atomic="false" class="initially-hidden"></ul> </form> </div> </div> <div id="class-metadata"> <div id="parent-class-section" class="nav-section"> <h3>Parent</h3> <p class="link">Object </div> <!-- Method Quickref --> <div id="method-list-section" class="nav-section"> <h3>Methods</h3> <ul class="link-list" role="directory"> <li ><a href="#method-c-new">::new</a> <li ><a href="#method-i-accepts_html-3F">#accepts_html?</a> <li ><a href="#method-i-call">#call</a> <li ><a href="#method-i-dump_exception">#dump_exception</a> <li ><a href="#method-i-prefers_plaintext-3F">#prefers_plaintext?</a> <li ><a href="#method-i-pretty">#pretty</a> <li ><a href="#method-i-template">#template</a> </ul> </div> </div> </nav> <main role="main" aria-labelledby="class-Rack::ShowExceptions"> <h1 id="class-Rack::ShowExceptions" class="class"> class Rack::ShowExceptions </h1> <section class="description"> <p><a href="ShowExceptions.html"><code>Rack::ShowExceptions</code></a> catches all exceptions raised from the app it wraps. It shows a useful backtrace with the sourcefile and clickable context, the whole <a href="../Rack.html"><code>Rack</code></a> environment and the request data.</p> <p>Be careful when you use this on public-facing sites as it could reveal information helpful to attackers.</p> </section> <section id="5Buntitled-5D" class="documentation-section"> <section class="constants-list"> <header> <h3>Constants</h3> </header> <dl> <dt id="CONTEXT">CONTEXT <dd> </dl> </section> <section id="public-class-5Buntitled-5D-method-details" class="method-section"> <header> <h3>Public Class Methods</h3> </header> <div id="method-c-new" class="method-detail "> <div class="method-heading"> <span class="method-name">new</span><span class="method-args">(app)</span> <span class="method-click-advice">click to toggle source</span> </div> <div class="method-description"> <div class="method-source-code" id="new-source"> <pre><span class="ruby-comment"># File lib/rack/show_exceptions.rb, line 18</span> <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">initialize</span>(<span class="ruby-identifier">app</span>) <span class="ruby-ivar">@app</span> = <span class="ruby-identifier">app</span> <span class="ruby-keyword">end</span></pre> </div> </div> </div> </section> <section id="public-instance-5Buntitled-5D-method-details" class="method-section"> <header> <h3>Public Instance Methods</h3> </header> <div id="method-i-call" class="method-detail "> <div class="method-heading"> <span class="method-name">call</span><span class="method-args">(env)</span> <span class="method-click-advice">click to toggle source</span> </div> <div class="method-description"> <div class="method-source-code" id="call-source"> <pre><span class="ruby-comment"># File lib/rack/show_exceptions.rb, line 22</span> <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">call</span>(<span class="ruby-identifier">env</span>) <span class="ruby-ivar">@app</span>.<span class="ruby-identifier">call</span>(<span class="ruby-identifier">env</span>) <span class="ruby-keyword">rescue</span> <span class="ruby-constant">StandardError</span>, <span class="ruby-constant">LoadError</span>, <span class="ruby-constant">SyntaxError</span> <span class="ruby-operator">=></span> <span class="ruby-identifier">e</span> <span class="ruby-identifier">exception_string</span> = <span class="ruby-identifier">dump_exception</span>(<span class="ruby-identifier">e</span>) <span class="ruby-identifier">env</span>[<span class="ruby-constant">RACK_ERRORS</span>].<span class="ruby-identifier">puts</span>(<span class="ruby-identifier">exception_string</span>) <span class="ruby-identifier">env</span>[<span class="ruby-constant">RACK_ERRORS</span>].<span class="ruby-identifier">flush</span> <span class="ruby-keyword">if</span> <span class="ruby-identifier">accepts_html?</span>(<span class="ruby-identifier">env</span>) <span class="ruby-identifier">content_type</span> = <span class="ruby-string">"text/html"</span> <span class="ruby-identifier">body</span> = <span class="ruby-identifier">pretty</span>(<span class="ruby-identifier">env</span>, <span class="ruby-identifier">e</span>) <span class="ruby-keyword">else</span> <span class="ruby-identifier">content_type</span> = <span class="ruby-string">"text/plain"</span> <span class="ruby-identifier">body</span> = <span class="ruby-identifier">exception_string</span> <span class="ruby-keyword">end</span> [ <span class="ruby-value">500</span>, { <span class="ruby-constant">CONTENT_TYPE</span> <span class="ruby-operator">=></span> <span class="ruby-identifier">content_type</span>, <span class="ruby-constant">CONTENT_LENGTH</span> <span class="ruby-operator">=></span> <span class="ruby-identifier">body</span>.<span class="ruby-identifier">bytesize</span>.<span class="ruby-identifier">to_s</span>, }, [<span class="ruby-identifier">body</span>], ] <span class="ruby-keyword">end</span></pre> </div> </div> </div> <div id="method-i-dump_exception" class="method-detail "> <div class="method-heading"> <span class="method-name">dump_exception</span><span class="method-args">(exception)</span> <span class="method-click-advice">click to toggle source</span> </div> <div class="method-description"> <div class="method-source-code" id="dump_exception-source"> <pre><span class="ruby-comment"># File lib/rack/show_exceptions.rb, line 57</span> <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">dump_exception</span>(<span class="ruby-identifier">exception</span>) <span class="ruby-identifier">string</span> = <span class="ruby-node">"#{exception.class}: #{exception.message}\n"</span>.<span class="ruby-identifier">dup</span> <span class="ruby-identifier">string</span> <span class="ruby-operator"><<</span> <span class="ruby-identifier">exception</span>.<span class="ruby-identifier">backtrace</span>.<span class="ruby-identifier">map</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">l</span><span class="ruby-operator">|</span> <span class="ruby-node">"\t#{l}"</span> }.<span class="ruby-identifier">join</span>(<span class="ruby-string">"\n"</span>) <span class="ruby-identifier">string</span> <span class="ruby-keyword">end</span></pre> </div> </div> </div> <div id="method-i-prefers_plaintext-3F" class="method-detail "> <div class="method-heading"> <span class="method-name">prefers_plaintext?</span><span class="method-args">(env)</span> <span class="method-click-advice">click to toggle source</span> </div> <div class="method-description"> <div class="method-source-code" id="prefers_plaintext-3F-source"> <pre><span class="ruby-comment"># File lib/rack/show_exceptions.rb, line 48</span> <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">prefers_plaintext?</span>(<span class="ruby-identifier">env</span>) <span class="ruby-operator">!</span><span class="ruby-identifier">accepts_html?</span>(<span class="ruby-identifier">env</span>) <span class="ruby-keyword">end</span></pre> </div> </div> </div> <div id="method-i-pretty" class="method-detail "> <div class="method-heading"> <span class="method-name">pretty</span><span class="method-args">(env, exception)</span> <span class="method-click-advice">click to toggle source</span> </div> <div class="method-description"> <div class="method-source-code" id="pretty-source"> <pre><span class="ruby-comment"># File lib/rack/show_exceptions.rb, line 63</span> <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">pretty</span>(<span class="ruby-identifier">env</span>, <span class="ruby-identifier">exception</span>) <span class="ruby-identifier">req</span> = <span class="ruby-constant">Rack</span><span class="ruby-operator">::</span><span class="ruby-constant">Request</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">env</span>) <span class="ruby-comment"># This double assignment is to prevent an "unused variable" warning.</span> <span class="ruby-comment"># Yes, it is dumb, but I don't like Ruby yelling at me.</span> <span class="ruby-identifier">path</span> = <span class="ruby-identifier">path</span> = (<span class="ruby-identifier">req</span>.<span class="ruby-identifier">script_name</span> <span class="ruby-operator">+</span> <span class="ruby-identifier">req</span>.<span class="ruby-identifier">path_info</span>).<span class="ruby-identifier">squeeze</span>(<span class="ruby-string">"/"</span>) <span class="ruby-comment"># This double assignment is to prevent an "unused variable" warning.</span> <span class="ruby-comment"># Yes, it is dumb, but I don't like Ruby yelling at me.</span> <span class="ruby-identifier">frames</span> = <span class="ruby-identifier">frames</span> = <span class="ruby-identifier">exception</span>.<span class="ruby-identifier">backtrace</span>.<span class="ruby-identifier">map</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">line</span><span class="ruby-operator">|</span> <span class="ruby-identifier">frame</span> = <span class="ruby-constant">OpenStruct</span>.<span class="ruby-identifier">new</span> <span class="ruby-keyword">if</span> <span class="ruby-identifier">line</span> <span class="ruby-operator">=~</span> <span class="ruby-regexp">/(.*?):(\d+)(:in `(.*)')?/</span> <span class="ruby-identifier">frame</span>.<span class="ruby-identifier">filename</span> = <span class="ruby-node">$1</span> <span class="ruby-identifier">frame</span>.<span class="ruby-identifier">lineno</span> = <span class="ruby-node">$2</span>.<span class="ruby-identifier">to_i</span> <span class="ruby-identifier">frame</span>.<span class="ruby-identifier">function</span> = <span class="ruby-node">$4</span> <span class="ruby-keyword">begin</span> <span class="ruby-identifier">lineno</span> = <span class="ruby-identifier">frame</span>.<span class="ruby-identifier">lineno</span> <span class="ruby-operator">-</span> <span class="ruby-value">1</span> <span class="ruby-identifier">lines</span> = <span class="ruby-operator">::</span><span class="ruby-constant">File</span>.<span class="ruby-identifier">readlines</span>(<span class="ruby-identifier">frame</span>.<span class="ruby-identifier">filename</span>) <span class="ruby-identifier">frame</span>.<span class="ruby-identifier">pre_context_lineno</span> = [<span class="ruby-identifier">lineno</span> <span class="ruby-operator">-</span> <span class="ruby-constant">CONTEXT</span>, <span class="ruby-value">0</span>].<span class="ruby-identifier">max</span> <span class="ruby-identifier">frame</span>.<span class="ruby-identifier">pre_context</span> = <span class="ruby-identifier">lines</span>[<span class="ruby-identifier">frame</span>.<span class="ruby-identifier">pre_context_lineno</span><span class="ruby-operator">...</span><span class="ruby-identifier">lineno</span>] <span class="ruby-identifier">frame</span>.<span class="ruby-identifier">context_line</span> = <span class="ruby-identifier">lines</span>[<span class="ruby-identifier">lineno</span>].<span class="ruby-identifier">chomp</span> <span class="ruby-identifier">frame</span>.<span class="ruby-identifier">post_context_lineno</span> = [<span class="ruby-identifier">lineno</span> <span class="ruby-operator">+</span> <span class="ruby-constant">CONTEXT</span>, <span class="ruby-identifier">lines</span>.<span class="ruby-identifier">size</span>].<span class="ruby-identifier">min</span> <span class="ruby-identifier">frame</span>.<span class="ruby-identifier">post_context</span> = <span class="ruby-identifier">lines</span>[<span class="ruby-identifier">lineno</span> <span class="ruby-operator">+</span> <span class="ruby-value">1</span><span class="ruby-operator">..</span><span class="ruby-identifier">frame</span>.<span class="ruby-identifier">post_context_lineno</span>] <span class="ruby-keyword">rescue</span> <span class="ruby-keyword">end</span> <span class="ruby-identifier">frame</span> <span class="ruby-keyword">else</span> <span class="ruby-keyword">nil</span> <span class="ruby-keyword">end</span> }.<span class="ruby-identifier">compact</span> <span class="ruby-identifier">template</span>.<span class="ruby-identifier">result</span>(<span class="ruby-identifier">binding</span>) <span class="ruby-keyword">end</span></pre> </div> </div> </div> <div id="method-i-template" class="method-detail "> <div class="method-heading"> <span class="method-name">template</span><span class="method-args">()</span> <span class="method-click-advice">click to toggle source</span> </div> <div class="method-description"> <div class="method-source-code" id="template-source"> <pre><span class="ruby-comment"># File lib/rack/show_exceptions.rb, line 99</span> <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">template</span> <span class="ruby-constant">TEMPLATE</span> <span class="ruby-keyword">end</span></pre> </div> </div> </div> </section> <section id="private-instance-5Buntitled-5D-method-details" class="method-section"> <header> <h3>Private Instance Methods</h3> </header> <div id="method-i-accepts_html-3F" class="method-detail "> <div class="method-heading"> <span class="method-name">accepts_html?</span><span class="method-args">(env)</span> <span class="method-click-advice">click to toggle source</span> </div> <div class="method-description"> <div class="method-source-code" id="accepts_html-3F-source"> <pre><span class="ruby-comment"># File lib/rack/show_exceptions.rb, line 52</span> <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">accepts_html?</span>(<span class="ruby-identifier">env</span>) <span class="ruby-constant">Rack</span><span class="ruby-operator">::</span><span class="ruby-constant">Utils</span>.<span class="ruby-identifier">best_q_match</span>(<span class="ruby-identifier">env</span>[<span class="ruby-string">"HTTP_ACCEPT"</span>], <span class="ruby-node">%w[text/html]</span>) <span class="ruby-keyword">end</span></pre> </div> </div> </div> </section> </section> </main> <footer id="validator-badges" role="contentinfo"> <p><a href="https://validator.w3.org/check/referer">Validate</a> <p>Generated by <a href="https://ruby.github.io/rdoc/">RDoc</a> 6.2.1.1. <p>Based on <a href="http://deveiate.org/projects/Darkfish-RDoc/">Darkfish</a> by <a href="http://deveiate.org">Michael Granger</a>. </footer>