4848import java .util .Locale ;
4949import java .util .Map ;
5050import java .util .NoSuchElementException ;
51- import java .util .logging .Level ;
5251import java .util .logging .Logger ;
5352import java .util .regex .Matcher ;
5453import java .util .regex .Pattern ;
@@ -642,6 +641,24 @@ private InputStream wrapStream(InputStream in) throws IOException {
642641 " handling exception " + e , e );
643642 throw e ;
644643 }
644+ InputStream es = wrapStream (uc .getErrorStream ());
645+ if (es != null ) {
646+ try {
647+ String error = IOUtils .toString (es , "UTF-8" );
648+ if (e instanceof FileNotFoundException ) {
649+ // pass through 404 Not Found to allow the caller to handle it intelligently
650+ e = (IOException ) new FileNotFoundException (error ).initCause (e );
651+ } else if (e instanceof HttpException ) {
652+ HttpException http = (HttpException ) e ;
653+ e = new HttpException (error , http .getResponseCode (), http .getResponseMessage (),
654+ http .getUrl (), e );
655+ } else {
656+ e = (IOException ) new IOException (error ).initCause (e );
657+ }
658+ } finally {
659+ IOUtils .closeQuietly (es );
660+ }
661+ }
645662 if (responseCode == HttpURLConnection .HTTP_UNAUTHORIZED ) // 401 / Unauthorized == bad creds
646663 throw e ;
647664
@@ -657,24 +674,7 @@ private InputStream wrapStream(InputStream in) throws IOException {
657674 return ;
658675 }
659676
660- InputStream es = wrapStream (uc .getErrorStream ());
661- try {
662- if (es !=null ) {
663- String error = IOUtils .toString (es , "UTF-8" );
664- if (e instanceof FileNotFoundException ) {
665- // pass through 404 Not Found to allow the caller to handle it intelligently
666- throw (IOException ) new FileNotFoundException (error ).initCause (e );
667- } else if (e instanceof HttpException ) {
668- HttpException http = (HttpException ) e ;
669- throw (IOException ) new HttpException (error , http .getResponseCode (), http .getResponseMessage (), http .getUrl (), e );
670- } else {
671- throw (IOException ) new IOException (error ).initCause (e );
672- }
673- } else
674- throw e ;
675- } finally {
676- IOUtils .closeQuietly (es );
677- }
677+ throw e ;
678678 }
679679
680680 private static final List <String > METHODS_WITHOUT_BODY = asList ("GET" , "DELETE" );
0 commit comments