A Break is Allowed

Sometimes, all you need is a break or patience. Many a time, I have had to rack my brain trying to remove a bug. Most times, I hit myself for making that little but terrible mistake in the first place.

Well, I must admit that we all have our different ways we react to problems and also ways we get them solved. What works for me is to take a break from it for few minutes and come back refreshed. I am always surprised at the little time I spend fixing this bug after my break. We have our ‘elastic limits’.

One of the misconceptions we have is the more time we spend on our seat with headphones and hood over our heads, the ‘geekier’ we look. But I am sorry, sitting in one spot and achieving little is not a good one, at least for me.

Some time ago, I had this issue I had to fight. It had to do with HttpUrlConnection in android. I was getting FileNotFoundException consistently. The sad part was when I tested this API on postman or my browser, I got the desired JSON response. I did some things that didn’t seem to lead me anywhere close to my solution.

After some Googling and SOing, I found out that this exception is always thrown when getResponseCode() returns 4xx. I felt somewhat angry with Google for not putting this in the documentation (and if it exists, for not reading it earlier).

I solved this by adding some try-and-catch statements and some control structures where needed. To me, that small thing didn’t deserve the whole stress I gave it.

Here is what my final snippet looked like:

    if (urlConnection.getResponseCode() == 401 || urlConnection.getResponseCode() == 403) {
    iStream = urlConnection.getErrorStream();
    } else {
    iStream = urlConnection.getInputStream();
    }

OR

    if (urlConnection.getResponseCode() != 200) {
    iStream = urlConnection.getErrorStream();
    } else {
    iStream = urlConnection.getInputStream();
    }


I limited my check to 401 and 403 because I knew the responses I could get already (I was the author of the API). A wider check is allowed.
That ends my short story.

  • Segun

    This sound more like my story. You are right.