From 59a3a62c45f04e65f94d7b454a422b2b1217c42b Mon Sep 17 00:00:00 2001 From: Dilan Perera Date: Thu, 21 Nov 2024 16:27:45 +0530 Subject: [PATCH] Fix panicking in ClientOAuth2 initialization --- ballerina/modules/bulk/client.bal | 11 ++++++++--- ballerina/modules/soap/client.bal | 12 +++++++++--- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/ballerina/modules/bulk/client.bal b/ballerina/modules/bulk/client.bal index 732c2666..fb857c55 100644 --- a/ballerina/modules/bulk/client.bal +++ b/ballerina/modules/bulk/client.bal @@ -43,14 +43,19 @@ public isolated client class Client { (authConfig is http:BearerTokenConfig ? authConfig : {...authConfig}); self.clientConfig = auth.cloneReadOnly(); - http:ClientOAuth2Handler|http:ClientBearerTokenAuthHandler httpHandlerResult; + http:ClientOAuth2Handler|http:ClientBearerTokenAuthHandler|error httpHandlerResult; if auth is http:OAuth2RefreshTokenGrantConfig { - httpHandlerResult = new http:ClientOAuth2Handler(auth); + httpHandlerResult = trap new http:ClientOAuth2Handler(auth); } else { httpHandlerResult = new http:ClientBearerTokenAuthHandler(auth); } - self.clientHandler = httpHandlerResult; + + if httpHandlerResult is http:ClientOAuth2Handler|http:ClientBearerTokenAuthHandler { + self.clientHandler = httpHandlerResult; + } else { + return error(utils:CLIENT_INIT_ERROR_MSG + httpHandlerResult.message(), httpHandlerResult); + } http:Client|http:ClientError httpClientResult; httpClientResult = new (config.baseUrl, httpClientConfig); diff --git a/ballerina/modules/soap/client.bal b/ballerina/modules/soap/client.bal index d5e4652c..e7f703fd 100644 --- a/ballerina/modules/soap/client.bal +++ b/ballerina/modules/soap/client.bal @@ -43,13 +43,19 @@ public isolated client class Client { (authConfig is http:BearerTokenConfig ? authConfig : {...authConfig}); self.clientConfig = auth.cloneReadOnly(); - http:ClientOAuth2Handler|http:ClientBearerTokenAuthHandler httpHandlerResult; + http:ClientOAuth2Handler|http:ClientBearerTokenAuthHandler|error httpHandlerResult; + if auth is http:OAuth2RefreshTokenGrantConfig { - httpHandlerResult = new http:ClientOAuth2Handler(auth); + httpHandlerResult = trap new http:ClientOAuth2Handler(auth); } else { httpHandlerResult = new http:ClientBearerTokenAuthHandler(auth); } - self.clientHandler = httpHandlerResult; + + if httpHandlerResult is http:ClientOAuth2Handler|http:ClientBearerTokenAuthHandler { + self.clientHandler = httpHandlerResult; + } else { + return error(utils:CLIENT_INIT_ERROR_MSG + httpHandlerResult.message(), httpHandlerResult); + } http:Client|http:ClientError httpClientResult = new (config.baseUrl, httpClientConfig);