2323
2424import com .fasterxml .jackson .databind .ObjectMapper ;
2525import com .g42cloud .sdk .core .auth .ICredential ;
26+ import com .g42cloud .sdk .core .exception .DefaultExceptionHandler ;
27+ import com .g42cloud .sdk .core .exception .ExceptionHandler ;
2628import com .g42cloud .sdk .core .exception .HostUnreachableException ;
2729import com .g42cloud .sdk .core .exception .SdkException ;
2830import com .g42cloud .sdk .core .exception .ServerResponseException ;
@@ -89,6 +91,8 @@ public static Logger get() {
8991
9092 private final AtomicInteger endpointIndex = new AtomicInteger (0 );
9193
94+ private ExceptionHandler exceptionHandler = new DefaultExceptionHandler ();
95+
9296 private List <String > endpoints ;
9397
9498 private ICredential credential ;
@@ -97,11 +101,13 @@ public static Logger get() {
97101
98102 private Map <String , String > extraHeader ;
99103
100- private HcClient (HttpConfig httpConfig , HttpClient httpClient , ICredential credential , List <String > endpoints ) {
101- this .httpConfig = httpConfig ;
102- this .httpClient = httpClient ;
103- this .credential = credential ;
104- this .endpoints = endpoints ;
104+ private HcClient (HcClient hcClient ) {
105+ this .extraHeader = hcClient .extraHeader ;
106+ this .httpClient = hcClient .httpClient ;
107+ this .endpoints = hcClient .endpoints ;
108+ this .credential = hcClient .credential ;
109+ this .httpConfig = hcClient .httpConfig ;
110+ this .exceptionHandler = hcClient .exceptionHandler ;
105111 }
106112
107113 HcClient (HttpConfig httpConfig , HttpClient httpClient ) {
@@ -124,6 +130,11 @@ public HcClient withCredential(ICredential credential) {
124130 return this ;
125131 }
126132
133+ protected HcClient withExceptionHandler (ExceptionHandler exceptionHandler ) {
134+ this .exceptionHandler = exceptionHandler ;
135+ return this ;
136+ }
137+
127138 public ICredential getCredential () {
128139 return this .credential ;
129140 }
@@ -133,16 +144,15 @@ public HttpConfig getHttpConfig() {
133144 }
134145
135146 public HcClient overrideEndpoints (List <String > endpoints ) {
136- return new HcClient (this . httpConfig , this . httpClient , this . credential , endpoints );
147+ return new HcClient (this ). withEndpoints ( endpoints );
137148 }
138149
139150 public HcClient overrideCredential (ICredential credential ) {
140-
141- return new HcClient (this .httpConfig , this .httpClient , credential , this .endpoints );
151+ return new HcClient (this ).withCredential (credential );
142152 }
143153
144154 public HcClient preInvoke (Map <String , String > extraHeader ) {
145- HcClient client = new HcClient (this . httpConfig , this . httpClient , this . credential , this . endpoints );
155+ HcClient client = new HcClient (this );
146156 client .extraHeader = extraHeader ;
147157 return client ;
148158 }
@@ -189,7 +199,7 @@ public <ReqT, ResT> ResT syncInvokeHttp(ReqT request, HttpRequestDef<ReqT, ResT>
189199 }
190200 }
191201 printAccessLog (httpRequest , httpResponse , exchange );
192- handleException (httpRequest , httpResponse );
202+ exceptionHandler . handleException (httpRequest , httpResponse );
193203 return extractResponse (httpRequest , httpResponse , reqDef );
194204 } finally {
195205 SdkExchangeCache .removeExchange (exchangeId );
@@ -237,7 +247,7 @@ public <ReqT, ResT> CompletableFuture<ResT> asyncInvokeHttp(ReqT request, HttpRe
237247
238248 return httpClient .asyncInvokeHttp (validHttpRequest ).thenApplyAsync (httpResponse -> {
239249 printAccessLog (finalValidHttpRequest , httpResponse , exchange );
240- handleException (finalValidHttpRequest , httpResponse );
250+ exceptionHandler . handleException (finalValidHttpRequest , httpResponse );
241251 return extractResponse (finalValidHttpRequest , httpResponse , reqDef );
242252 }, httpConfig .getExecutorService ()).whenCompleteAsync ((r , e ) ->
243253 SdkExchangeCache .removeExchange (exchangeIdRef .get ()), httpConfig .getExecutorService ());
0 commit comments