Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
bcx / docs / lib / bcx / response_error.html
Size: Mime:
<!DOCTYPE html>

<html>
<head>
  <title>response_error.rb</title>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <meta name="viewport" content="width=device-width, target-densitydpi=160dpi, initial-scale=1.0; maximum-scale=1.0; user-scalable=0;">
  <link rel="stylesheet" media="all" href="../../docco.css" />
</head>
<body>
  <div id="container">
    <div id="background"></div>
    
      <ul id="jump_to">
        <li>
          <a class="large" href="javascript:void(0);">Jump To &hellip;</a>
          <a class="small" href="javascript:void(0);">+</a>
          <div id="jump_wrapper">
          <div id="jump_page">
            
              
              <a class="source" href="configuration.html">
                lib/bcx/configuration.rb
              </a>
            
              
              <a class="source" href="response_error.html">
                lib/bcx/response_error.rb
              </a>
            
              
              <a class="source" href="version.html">
                lib/bcx/version.rb
              </a>
            
          </div>
        </li>
      </ul>
    
    <ul class="sections">
        
          <li id="title">
              <div class="annotation">
                  <h1>response_error.rb</h1>
              </div>
          </li>
        
        
        
        <li id="section-1">
            <div class="annotation">
              
              <div class="pilwrap for-h2">
                <a class="pilcrow" href="#section-1">&#182;</a>
              </div>
              <h2>Response Error</h2>
<p>If the response whilst fetching a resource is a 4xx or 5xx, Bcx will raise a <code>Bcx::ResponseError</code> exception.</p>
<p>Examples:</p>
<pre><code>client.projects.create!(<span class="symbol">name:</span> <span class="string">''</span>)
=&gt; <span class="constant">Bcx::ResponseError</span><span class="symbol">:</span> <span class="number">422</span> <span class="constant">POST</span> <span class="symbol">https:</span>/<span class="regexp">/basecamp.com/</span><span class="number">2274488</span>/api/v1/projects.json | <span class="constant">Errors</span><span class="symbol">:</span> name can<span class="string">'t be blank</code></pre>
<p>You can rescue this exception to grab the status, method, URL and errors individually.</p>
<pre><code><span class="keyword">begin</span>
  client.projects.create!(<span class="symbol">name:</span> <span class="string">''</span>)
<span class="keyword">rescue</span> <span class="constant">Bcx::ResponseError</span> =&gt; response
  response.method <span class="comment"># =&gt; "POST"</span>
  response.status <span class="comment"># =&gt; 422</span>
  response.url <span class="comment"># =&gt; "https://basecamp.com/2274488/api/v1/projects.json"</span>
  response.errors <span class="comment"># =&gt; ["name can't be blank"]</span>
<span class="keyword">end</span></code></pre>

            </div>
            
            <div class="content"><div class='highlight'><pre><span class="class"><span class="keyword">module</span> <span class="title">Bcx</span></span>
  <span class="class"><span class="keyword">class</span> <span class="title">ResponseError</span> <span class="inheritance">&lt; <span class="parent">Rapidash::ResponseError</span></span></span>

    <span class="function"><span class="keyword">def</span> <span class="title">errors</span></span>
      <span class="keyword">return</span> body <span class="keyword">if</span> body.kind_of?(<span class="constant">String</span>)

      messages = []

      body.each_pair <span class="keyword">do</span> |attribute, msgs|
        msgs.each { |msg| messages.push <span class="string">"<span class="subst">#{attribute}</span> <span class="subst">#{msg}</span>"</span> }
      <span class="keyword">end</span>

      messages.join(<span class="string">', '</span>)
    <span class="keyword">end</span>

  <span class="keyword">end</span>
<span class="keyword">end</span></pre></div></div>
            
        </li>
        
    </ul>
  </div>
</body>
</html>