@ -22,12 +22,12 @@ import java.nio.ByteBuffer;
@@ -22,12 +22,12 @@ import java.nio.ByteBuffer;
import java.nio.CharBuffer ;
import java.nio.charset.Charset ;
import java.util.Collection ;
import java.util.Collections ;
import java.util.Map ;
import java.util.Optional ;
import java.util.regex.Matcher ;
import java.util.regex.Pattern ;
import static feign.Util.UTF_8 ;
import static feign.Util.checkNotNull ;
import static feign.Util.* ;
import static java.lang.String.format ;
/ * *
@ -40,6 +40,7 @@ public class FeignException extends RuntimeException {
@@ -40,6 +40,7 @@ public class FeignException extends RuntimeException {
private static final long serialVersionUID = 0 ;
private int status ;
private byte [ ] responseBody ;
private Map < String , Collection < String > > responseHeaders ;
private Request request ;
protected FeignException ( int status , String message , Throwable cause ) {
@ -48,10 +49,12 @@ public class FeignException extends RuntimeException {
@@ -48,10 +49,12 @@ public class FeignException extends RuntimeException {
this . request = null ;
}
protected FeignException ( int status , String message , Throwable cause , byte [ ] responseBody ) {
protected FeignException ( int status , String message , Throwable cause , byte [ ] responseBody ,
Map < String , Collection < String > > responseHeaders ) {
super ( message , cause ) ;
this . status = status ;
this . responseBody = responseBody ;
this . responseHeaders = caseInsensitiveCopyOf ( responseHeaders ) ;
this . request = null ;
}
@ -61,10 +64,12 @@ public class FeignException extends RuntimeException {
@@ -61,10 +64,12 @@ public class FeignException extends RuntimeException {
this . request = null ;
}
protected FeignException ( int status , String message , byte [ ] responseBody ) {
protected FeignException ( int status , String message , byte [ ] responseBody ,
Map < String , Collection < String > > responseHeaders ) {
super ( message ) ;
this . status = status ;
this . responseBody = responseBody ;
this . responseHeaders = caseInsensitiveCopyOf ( responseHeaders ) ;
this . request = null ;
}
@ -75,10 +80,11 @@ public class FeignException extends RuntimeException {
@@ -75,10 +80,11 @@ public class FeignException extends RuntimeException {
}
protected FeignException ( int status , String message , Request request , Throwable cause ,
byte [ ] responseBody ) {
byte [ ] responseBody , Map < String , Collection < String > > responseHeaders ) {
super ( message , cause ) ;
this . status = status ;
this . responseBody = responseBody ;
this . responseHeaders = caseInsensitiveCopyOf ( responseHeaders ) ;
this . request = checkRequestNotNull ( request ) ;
}
@ -88,10 +94,12 @@ public class FeignException extends RuntimeException {
@@ -88,10 +94,12 @@ public class FeignException extends RuntimeException {
this . request = checkRequestNotNull ( request ) ;
}
protected FeignException ( int status , String message , Request request , byte [ ] responseBody ) {
protected FeignException ( int status , String message , Request request , byte [ ] responseBody ,
Map < String , Collection < String > > responseHeaders ) {
super ( message ) ;
this . status = status ;
this . responseBody = responseBody ;
this . responseHeaders = caseInsensitiveCopyOf ( responseHeaders ) ;
this . request = checkRequestNotNull ( request ) ;
}
@ -126,6 +134,13 @@ public class FeignException extends RuntimeException {
@@ -126,6 +134,13 @@ public class FeignException extends RuntimeException {
return Optional . of ( ByteBuffer . wrap ( this . responseBody ) ) ;
}
public Map < String , Collection < String > > responseHeaders ( ) {
if ( this . responseHeaders = = null ) {
return Collections . emptyMap ( ) ;
}
return responseHeaders ;
}
public Request request ( ) {
return this . request ;
}
@ -148,7 +163,8 @@ public class FeignException extends RuntimeException {
@@ -148,7 +163,8 @@ public class FeignException extends RuntimeException {
format ( "%s reading %s %s" , cause . getMessage ( ) , request . httpMethod ( ) , request . url ( ) ) ,
request ,
cause ,
request . body ( ) ) ;
request . body ( ) ,
request . headers ( ) ) ;
}
public static FeignException errorStatus ( String methodKey , Response response ) {
@ -166,20 +182,21 @@ public class FeignException extends RuntimeException {
@@ -166,20 +182,21 @@ public class FeignException extends RuntimeException {
. withMethodKey ( methodKey )
. withBody ( body ) . build ( ) ;
return errorStatus ( response . status ( ) , message , response . request ( ) , body ) ;
return errorStatus ( response . status ( ) , message , response . request ( ) , body , response . headers ( ) ) ;
}
private static FeignException errorStatus ( int status ,
String message ,
Request request ,
byte [ ] body ) {
byte [ ] body ,
Map < String , Collection < String > > headers ) {
if ( isClientError ( status ) ) {
return clientErrorStatus ( status , message , request , body ) ;
return clientErrorStatus ( status , message , request , body , headers ) ;
}
if ( isServerError ( status ) ) {
return serverErrorStatus ( status , message , request , body ) ;
return serverErrorStatus ( status , message , request , body , headers ) ;
}
return new FeignException ( status , message , request , body ) ;
return new FeignException ( status , message , request , body , headers ) ;
}
private static boolean isClientError ( int status ) {
@ -189,32 +206,33 @@ public class FeignException extends RuntimeException {
@@ -189,32 +206,33 @@ public class FeignException extends RuntimeException {
private static FeignClientException clientErrorStatus ( int status ,
String message ,
Request request ,
byte [ ] body ) {
byte [ ] body ,
Map < String , Collection < String > > headers ) {
switch ( status ) {
case 400 :
return new BadRequest ( message , request , body ) ;
return new BadRequest ( message , request , body , headers ) ;
case 401 :
return new Unauthorized ( message , request , body ) ;
return new Unauthorized ( message , request , body , headers ) ;
case 403 :
return new Forbidden ( message , request , body ) ;
return new Forbidden ( message , request , body , headers ) ;
case 404 :
return new NotFound ( message , request , body ) ;
return new NotFound ( message , request , body , headers ) ;
case 405 :
return new MethodNotAllowed ( message , request , body ) ;
return new MethodNotAllowed ( message , request , body , headers ) ;
case 406 :
return new NotAcceptable ( message , request , body ) ;
return new NotAcceptable ( message , request , body , headers ) ;
case 409 :
return new Conflict ( message , request , body ) ;
return new Conflict ( message , request , body , headers ) ;
case 410 :
return new Gone ( message , request , body ) ;
return new Gone ( message , request , body , headers ) ;
case 415 :
return new UnsupportedMediaType ( message , request , body ) ;
return new UnsupportedMediaType ( message , request , body , headers ) ;
case 429 :
return new TooManyRequests ( message , request , body ) ;
return new TooManyRequests ( message , request , body , headers ) ;
case 422 :
return new UnprocessableEntity ( message , request , body ) ;
return new UnprocessableEntity ( message , request , body , headers ) ;
default :
return new FeignClientException ( status , message , request , body ) ;
return new FeignClientException ( status , message , request , body , headers ) ;
}
}
@ -225,20 +243,21 @@ public class FeignException extends RuntimeException {
@@ -225,20 +243,21 @@ public class FeignException extends RuntimeException {
private static FeignServerException serverErrorStatus ( int status ,
String message ,
Request request ,
byte [ ] body ) {
byte [ ] body ,
Map < String , Collection < String > > headers ) {
switch ( status ) {
case 500 :
return new InternalServerError ( message , request , body ) ;
return new InternalServerError ( message , request , body , headers ) ;
case 501 :
return new NotImplemented ( message , request , body ) ;
return new NotImplemented ( message , request , body , headers ) ;
case 502 :
return new BadGateway ( message , request , body ) ;
return new BadGateway ( message , request , body , headers ) ;
case 503 :
return new ServiceUnavailable ( message , request , body ) ;
return new ServiceUnavailable ( message , request , body , headers ) ;
case 504 :
return new GatewayTimeout ( message , request , body ) ;
return new GatewayTimeout ( message , request , body , headers ) ;
default :
return new FeignServerException ( status , message , request , body ) ;
return new FeignServerException ( status , message , request , body , headers ) ;
}
}
@ -252,127 +271,145 @@ public class FeignException extends RuntimeException {
@@ -252,127 +271,145 @@ public class FeignException extends RuntimeException {
}
public static class FeignClientException extends FeignException {
public FeignClientException ( int status , String message , Request request , byte [ ] body ) {
super ( status , message , request , body ) ;
public FeignClientException ( int status , String message , Request request , byte [ ] body ,
Map < String , Collection < String > > headers ) {
super ( status , message , request , body , headers ) ;
}
}
public static class BadRequest extends FeignClientException {
public BadRequest ( String message , Request request , byte [ ] body ) {
super ( 400 , message , request , body ) ;
public BadRequest ( String message , Request request , byte [ ] body ,
Map < String , Collection < String > > headers ) {
super ( 400 , message , request , body , headers ) ;
}
}
public static class Unauthorized extends FeignClientException {
public Unauthorized ( String message , Request request , byte [ ] body ) {
super ( 401 , message , request , body ) ;
public Unauthorized ( String message , Request request , byte [ ] body ,
Map < String , Collection < String > > headers ) {
super ( 401 , message , request , body , headers ) ;
}
}
public static class Forbidden extends FeignClientException {
public Forbidden ( String message , Request request , byte [ ] body ) {
super ( 403 , message , request , body ) ;
public Forbidden ( String message , Request request , byte [ ] body ,
Map < String , Collection < String > > headers ) {
super ( 403 , message , request , body , headers ) ;
}
}
public static class NotFound extends FeignClientException {
public NotFound ( String message , Request request , byte [ ] body ) {
super ( 404 , message , request , body ) ;
public NotFound ( String message , Request request , byte [ ] body ,
Map < String , Collection < String > > headers ) {
super ( 404 , message , request , body , headers ) ;
}
}
public static class MethodNotAllowed extends FeignClientException {
public MethodNotAllowed ( String message , Request request , byte [ ] body ) {
super ( 405 , message , request , body ) ;
public MethodNotAllowed ( String message , Request request , byte [ ] body ,
Map < String , Collection < String > > headers ) {
super ( 405 , message , request , body , headers ) ;
}
}
public static class NotAcceptable extends FeignClientException {
public NotAcceptable ( String message , Request request , byte [ ] body ) {
super ( 406 , message , request , body ) ;
public NotAcceptable ( String message , Request request , byte [ ] body ,
Map < String , Collection < String > > headers ) {
super ( 406 , message , request , body , headers ) ;
}
}
public static class Conflict extends FeignClientException {
public Conflict ( String message , Request request , byte [ ] body ) {
super ( 409 , message , request , body ) ;
public Conflict ( String message , Request request , byte [ ] body ,
Map < String , Collection < String > > headers ) {
super ( 409 , message , request , body , headers ) ;
}
}
public static class Gone extends FeignClientException {
public Gone ( String message , Request request , byte [ ] body ) {
super ( 410 , message , request , body ) ;
public Gone ( String message , Request request , byte [ ] body ,
Map < String , Collection < String > > headers ) {
super ( 410 , message , request , body , headers ) ;
}
}
public static class UnsupportedMediaType extends FeignClientException {
public UnsupportedMediaType ( String message , Request request , byte [ ] body ) {
super ( 415 , message , request , body ) ;
public UnsupportedMediaType ( String message , Request request , byte [ ] body ,
Map < String , Collection < String > > headers ) {
super ( 415 , message , request , body , headers ) ;
}
}
public static class TooManyRequests extends FeignClientException {
public TooManyRequests ( String message , Request request , byte [ ] body ) {
super ( 429 , message , request , body ) ;
public TooManyRequests ( String message , Request request , byte [ ] body ,
Map < String , Collection < String > > headers ) {
super ( 429 , message , request , body , headers ) ;
}
}
public static class UnprocessableEntity extends FeignClientException {
public UnprocessableEntity ( String message , Request request , byte [ ] body ) {
super ( 422 , message , request , body ) ;
public UnprocessableEntity ( String message , Request request , byte [ ] body ,
Map < String , Collection < String > > headers ) {
super ( 422 , message , request , body , headers ) ;
}
}
public static class FeignServerException extends FeignException {
public FeignServerException ( int status , String message , Request request , byte [ ] body ) {
super ( status , message , request , body ) ;
public FeignServerException ( int status , String message , Request request , byte [ ] body ,
Map < String , Collection < String > > headers ) {
super ( status , message , request , body , headers ) ;
}
}
public static class InternalServerError extends FeignServerException {
public InternalServerError ( String message , Request request , byte [ ] body ) {
super ( 500 , message , request , body ) ;
public InternalServerError ( String message , Request request , byte [ ] body ,
Map < String , Collection < String > > headers ) {
super ( 500 , message , request , body , headers ) ;
}
}
public static class NotImplemented extends FeignServerException {
public NotImplemented ( String message , Request request , byte [ ] body ) {
super ( 501 , message , request , body ) ;
public NotImplemented ( String message , Request request , byte [ ] body ,
Map < String , Collection < String > > headers ) {
super ( 501 , message , request , body , headers ) ;
}
}
public static class BadGateway extends FeignServerException {
public BadGateway ( String message , Request request , byte [ ] body ) {
super ( 502 , message , request , body ) ;
public BadGateway ( String message , Request request , byte [ ] body ,
Map < String , Collection < String > > headers ) {
super ( 502 , message , request , body , headers ) ;
}
}
public static class ServiceUnavailable extends FeignServerException {
public ServiceUnavailable ( String message , Request request , byte [ ] body ) {
super ( 503 , message , request , body ) ;
public ServiceUnavailable ( String message , Request request , byte [ ] body ,
Map < String , Collection < String > > headers ) {
super ( 503 , message , request , body , headers ) ;
}
}
public static class GatewayTimeout extends FeignServerException {
public GatewayTimeout ( String message , Request request , byte [ ] body ) {
super ( 504 , message , request , body ) ;
public GatewayTimeout ( String message , Request request , byte [ ] body ,
Map < String , Collection < String > > headers ) {
super ( 504 , message , request , body , headers ) ;
}
}