Skip to content

Commit 33a40d6

Browse files
committed
feat(disable-ssl-verification): log error for ssl error and others
1 parent cbaab0f commit 33a40d6

File tree

1 file changed

+35
-21
lines changed

1 file changed

+35
-21
lines changed

ibm_cloud_sdk_core/base_service.py

Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
from .api_exception import ApiException
2727
from .authenticators import Authenticator
2828
from http.cookiejar import CookieJar
29+
import logging
2930

3031
# Uncomment this to enable http debugging
3132
# import http.client as http_client
@@ -35,6 +36,9 @@
3536
class BaseService(object):
3637

3738
SDK_NAME = 'ibm-python-sdk-core'
39+
ERROR_MSG_DISABLE_SSL = 'If you\'re trying to call a service on ICP or Cloud Pak for Data, you may not have a valid SSL certificate. '\
40+
'If you need to access the service without setting that up, try using the disable_ssl_verification option in your authentication '\
41+
'configuration and/or setting set_disable_ssl_verification(True) on your service.'
3842

3943
def __init__(self,
4044
service_url=None,
@@ -125,28 +129,38 @@ def send(self, request, **kwargs):
125129
if self.disable_ssl_verification:
126130
kwargs['verify'] = False
127131

128-
response = requests.request(**request, cookies=self.jar, **kwargs)
129-
130-
if 200 <= response.status_code <= 299:
131-
if response.status_code == 204 or request['method'] == 'HEAD':
132-
# There is no body content for a HEAD request or a 204 response
133-
result = None
134-
elif not response.text:
135-
result = None
132+
try:
133+
response = requests.request(**request, cookies=self.jar, **kwargs)
134+
135+
if 200 <= response.status_code <= 299:
136+
if response.status_code == 204 or request['method'] == 'HEAD':
137+
# There is no body content for a HEAD request or a 204 response
138+
result = None
139+
elif not response.text:
140+
result = None
141+
else:
142+
try:
143+
result = response.json()
144+
except:
145+
result = response
146+
return DetailedResponse(result, response.headers,
147+
response.status_code)
136148
else:
137-
try:
138-
result = response.json()
139-
except:
140-
result = response
141-
return DetailedResponse(result, response.headers,
142-
response.status_code)
143-
else:
144-
error_message = None
145-
if response.status_code == 401:
146-
error_message = 'Unauthorized: Access is denied due to ' \
147-
'invalid credentials'
148-
raise ApiException(
149-
response.status_code, error_message, http_response=response)
149+
error_message = None
150+
if response.status_code == 401:
151+
error_message = 'Unauthorized: Access is denied due to ' \
152+
'invalid credentials'
153+
raise ApiException(
154+
response.status_code, error_message, http_response=response)
155+
except requests.exceptions.SSLError:
156+
logging.exception(self.ERROR_MSG_DISABLE_SSL)
157+
raise
158+
except ApiException as err:
159+
logging.exception(err.message)
160+
raise
161+
except:
162+
logging.exception('Error in service call')
163+
raise
150164

151165

152166
def prepare_request(self, method, url, headers=None,

0 commit comments

Comments
 (0)