<h3 dir="auto">Summary</h3>
<p dir="auto">When transforming response data via AxiosRequestConfig.transformResponse, there is no way to drive transformation logic differently depending on the HTTP status of the response.</p>
<p dir="auto">This is a problem for me because I want to implement a transform that transforms only successful responses. Successful responses (in 200 range) are guaranteed to contain data in a well-defined format that I want to transform, but unsuccessful responses may contain a variety of response data structures (dependent on the type of error) that I want to pass-through my transform without modification so that they can be later processed by a more generic error response handler.</p>
<p dir="auto">Implementing my transform to detect what type of structure is in the response based on the response data alone is impractical. The API I am working with clearly defines the structure of the response for each HTTP status code it may respond with, so there's no reason that I should have to reverse-engineer what kind of response it is by inspecting the response data (and it may not even be possible to do so unambiguously).</p>
<p dir="auto">Some options:</p>
<h4 dir="auto">Pass HTTP status code to AxiosRequestConfig.transformResponse (most useful option IMO)</h4>
<p dir="auto">Pros:</p>
<ul dir="auto">
<li>Allows for granularity of transforming data differently based on status code.</li>
</ul>
<p dir="auto">Cons:</p>
<ul dir="auto">
<li>Requires duplicating logic to decide whether response code is success vs error if that's all you care about (maybe this could be solved by also passing a "success" boolean?).</li>
<li>Introduces a difference in signature for transformResponse vs transformRequest, which may complicate some of the existing design/code that essentially treats them both the same.</li>
</ul>
<h4 dir="auto">Add 2 new options to AxiosRequestConfig:</h4>
<ul dir="auto">
<li>transformSuccessResponse: transforms response data ONLY for success responses</li>
<li>transformErrorResponse: transforms response data ONLY for error responses.</li>
</ul>
<p dir="auto">Pros:</p>
<ul dir="auto">
<li>Makes use of existing determination of which status codes are considered success vs error.</li>
</ul>
<p dir="auto">Cons:</p>
<ul dir="auto">
<li>Missing granularity for converting different response data structures for different error status codes.</li>
<li>Requires backward compatibility for continuing to use transformResponse for all responses.</li>
</ul>
<h3 dir="auto">Context</h3>
<ul dir="auto">
<li>axios version: <em>e.g.: v0.17.1</em></li>
<li>Environment: *e.g.: irrelevant</li>
</ul>
<h4 dir="auto">Summary</h4>
<p dir="auto">Recently <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="291855598" data-permission-text="Title is private" data-url="https://github.com/axios/axios/issues/1323" data-hovercard-type="issue" data-hovercard-url="/axios/axios/issues/1323/hovercard" href="https://github.com/axios/axios/issues/1323">#1323</a> has been fixed, however <code class="notranslate">follow-redirects</code> is still not correctly implemented: current version does not handle errors correctly.<br>
Indeed, if <code class="notranslate">maxBodyLength</code> is 10MB and the resource fetched after the redirect is bigger, <code class="notranslate">follow-redirects</code> will emit an error but that error will occur outside of axios promise chain.</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="Error: Request body larger than maxBodyLength limit
      at RedirectableRequest.write (./node_modules/follow-redirects/index.js:66:24)
      at FormData.CombinedStream.write (./node_modules/combined-stream/lib/combined_stream.js:118:8)
      at FormData.CombinedStream._pipeNext (./node_modules/combined-stream/lib/combined_stream.js:106:8)
      at FormData.CombinedStream._getNext (./node_modules/combined-stream/lib/combined_stream.js:79:10)
      at FormData.CombinedStream._pipeNext (./node_modules/combined-stream/lib/combined_stream.js:107:8)
      at FormData.CombinedStream._getNext (./node_modules/combined-stream/lib/combined_stream.js:79:10)
      at FormData.CombinedStream.resume (./node_modules/combined-stream/lib/combined_stream.js:134:10)
      at FormData.CombinedStream.pipe (./node_modules/combined-stream/lib/combined_stream.js:64:8)
      at dispatchHttpRequest (./node_modules/axios/lib/adapters/http.js:227:12)
      at httpAdapter (./node_modules/axios/lib/adapters/http.js:18:10)
      at dispatchRequest (./node_modules/axios/lib/core/dispatchRequest.js:59:10)"><pre class="notranslate"><code class="notranslate">Error: Request body larger than maxBodyLength limit
      at RedirectableRequest.write (./node_modules/follow-redirects/index.js:66:24)
      at FormData.CombinedStream.write (./node_modules/combined-stream/lib/combined_stream.js:118:8)
      at FormData.CombinedStream._pipeNext (./node_modules/combined-stream/lib/combined_stream.js:106:8)
      at FormData.CombinedStream._getNext (./node_modules/combined-stream/lib/combined_stream.js:79:10)
      at FormData.CombinedStream._pipeNext (./node_modules/combined-stream/lib/combined_stream.js:107:8)
      at FormData.CombinedStream._getNext (./node_modules/combined-stream/lib/combined_stream.js:79:10)
      at FormData.CombinedStream.resume (./node_modules/combined-stream/lib/combined_stream.js:134:10)
      at FormData.CombinedStream.pipe (./node_modules/combined-stream/lib/combined_stream.js:64:8)
      at dispatchHttpRequest (./node_modules/axios/lib/adapters/http.js:227:12)
      at httpAdapter (./node_modules/axios/lib/adapters/http.js:18:10)
      at dispatchRequest (./node_modules/axios/lib/core/dispatchRequest.js:59:10)
</code></pre></div>
<p dir="auto">Axios should intercept that error and reject the promise</p>
<h4 dir="auto">Context</h4>
<ul dir="auto">
<li>axios version: v0.17.1 + master</li>
<li>Environment: node v8.4.0</li>
</ul>