Start a conversation

Exception Handling while consuming CustomerSuccessBox’s APIs

Objective

Long story short! Synchronously consuming our APIs is bad as it can result in downtime of your own processes and internal systems. This can be avoided by de-coupling CustomerSuccessBox API usage from your application by consuming our APIs asynchronously.

Who is this for?

Exception handling is in place for JavaScript client library by default, and this article is only applicable for developers consuming our APIs directly.

Consuming APIs Asynchronously

There are basically two ways to approach and fix this problem at customer’s end. Our engineering recommends both to be on the safe side

  • Exception Handling

  • Consume our APIs asynchronously or use queueing

Exception Handling

Information about Exception Handling can be found in the wikipedia article here

Exception Handling is available in every programming language. Try/Catch is the basic example of how exception handling can be implemented. Here is a very generic codebase example:

try {
// Your code goes here which might throw exception
} catch {
  // This section will be called in case exception occurs
  // So compiler won't stop right there, it will put you in this block
  // So you can manage that error/exception as you want
}

We are sharing small code snippets in different programming languages to avoid any blockage in case something does not work as intended at CustomerSuccessBox's end.

Exception Handling using PHP

We are sharing two different scripts in PHP which logs a page on CustomerSuccessBox. The first script has been written without Exception Handling and the second script is written with Exception Handling. 

Script without Exception Handling

echo "Started";
echo "\n\n";
makeCurl("http://local.customersuccessbox.test/api_js/v1_1/page", $dat
echo "\n\n";
echo "Finished";

Note: makeCurl is not a PHP function, this is a function that we have written to make CURL request easier in PHP.

Output if CustomerSuccessBox servers are up:
Started
Finished
Output when CustomerSuccessBox servers are down:
Started

PHP Fatal error:  Uncaught Exception: Failed to connect to local.customersuccessbox.test port 80: Connection refused in /Users/simaranjitsingh/Desktop/temp/analytics.php:24
Stack trace:
............
.................
......................
..........................

So when CustomerSuccessBox servers are down,  no further code is executed due to the exception.

Script with Exception Handling

echo "started";
echo "\n\n";
try {
        $res = makeCurl("http://local.customersuccessbox.test/api_js/v1_1/page", $data);
} catch (Exception $E) {
        echo "Something went wrong but execution won't stop since we're using try/catch";
        // Pass or put some logic to requeue later
}
echo "\n\n";
echo "Finished";
Output if CustomerSuccessBox servers are up:
Started
Finished

So, the output is good if API endpoint returns success.

Output when CustomerSuccessBox servers are down:
Started
Something went wrong but execution won't stop since we're using try/catch
Finished

Note that execution doesn’t stop when we put HTTP call in the try block and handle exception using catch

Exception Handling using Python

We are sharing two different scripts in Python which logs a page on CustomerSuccessBox. The first script has been written without Exception Handling and the second script is written with Exception Handling. 

Script without Exception Handling

print "Started"

conn = httplib.HTTPConnection("domain.customersuccessbox.test/api_js/v1_1/page")
conn.request("POST", "", payload, headers)
response = conn.getresponse()

print "Finished"
Output if CSB server is up and running fine
Started
Finished
Output when CSB server is down due to any reason
Started
Traceback (most recent call last):
  File "analytics.py", line 10, in <module>
    conn.request("POST", "", payload, headers)
  File "/usr/local/Cellar/python@2/2.7.15_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 1042, in request
    self._send_request(method, url, body, headers)
  File "/usr/local/Cellar/python@2/2.7.15_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 1082, in _send_request
    self.endheaders(body)
  File "/usr/local/Cellar/python@2/2.7.15_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 1038, in endheaders
    self._send_output(message_body)
  File "/usr/local/Cellar/python@2/2.7.15_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 882, in _send_output
    self.send(msg)
  File "/usr/local/Cellar/python@2/2.7.15_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 844, in send
    self.connect()
  File "/usr/local/Cellar/python@2/2.7.15_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 821, in connect
    self.timeout, self.source_address)
  File "/usr/local/Cellar/python@2/2.7.15_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 557, in create_connection
    for res in getaddrinfo(host, port, 0, SOCK_STREAM):
socket.gaierror: [Errno 8] nodename nor servname provided, or not known

So when CustomerSuccessBox servers are down,  no further code is executed due to the exception.

Script with Exception Handling

print "Started"

try:
        conn = httplib.HTTPConnection("domain.customersuccessbox.test/api_js/v1_1/page")
        conn.request("POST", "", payload, headers)

        response = conn.getresponse()
except Exception, e:
        pass

print "Finished"
Output if CSB server is up and running fine
Started
Finished

So, the output is good if API endpoint returns success.

Output when CSB server is down due to any reason
Started
Something went wrong but execution won't stop since we're using try/catch
Finished

Note that execution doesn’t stop when we put HTTP call in try block and handle exception using catch.

Consume our APIs asynchronously or use queueing

You can send and log a call in a queuing service which is responsible for executing the API call or call out to us asynchronously, thereby de-coupling your primary product with CustomerSuccessBox’s API availability.

For more details on Synchronous versus asynchronous service, you may refer to this article.

Have more questions? Reach out to us at support@customersuccessbox.com

Choose files or drag and drop files
Was this article helpful?
Yes
No
  1. Anadi Raj Tiwari

  2. Posted
  3. Updated

Comments