Alone against a bug.
Tags: debugging, beginners, productivity, learning
Published: 2020-08-10 06:04:23

---
title: Alone against a bug. 
published: true
description: how to solve problems when you are alone and clueless?
tags: #debugging, #beginners, #productivity, #learning
cover_image: https://dev-to-uploads.s3.amazonaws.com/i/6bea2vx0swa3hxov50ga.png
---

Some days ago I got stuck with a bug which left me clueless. 
I was working remotely and one of the most experienced colleagues in my team was on vacation and the others were fighting with another product and its deadline. 

**Debugging and trying to fix a bug alone can be stressful** but it is important to remain calm - often, even if it´s counterintuitive, it is really better to **take a break!** - and try approaching the problem from a different perspective, and explaining it to someone, even if it´s not technical at all: it could be your partner or even an unanimated object like a **Rubber Duck**: it doesn´t matter, what matters is _rephrasing the context_ and eventually shed some light!

![Rubber duck debugging](https://dev-to-uploads.s3.amazonaws.com/i/ajk8d4quv2li0f1nuadb.png)

In my case, the error seemed clear, but the reason behind was not:

```
{ "errorType": "Error", 
"errorMessage": "Unable to stringify response body", 
"stack": [ "Error: Unable to stringify response body", 
" at _trySerializeResponse (/var/runtime/RAPIDClient.js:138:11)", " at RAPIDClient.postInvocationResponse (/var/runtime/RAPIDClient.js:42:22)", " at complete (/var/runtime/CallbackContext.js:34:12)", " at done  ] }
```
My lambda was returning successfully and all my unit tests were working fine.  
But once deployed and invoked, the Lambda was failing and logging that error. 

As I was expecting googling for that Error message was not really helpful, the search was returning mostly about invalid objects that could not properly stringified, but that was not really my case. Or at least, the object that could not be stringified seemed apparently whatever was returned by the lambda to Gateway API, not in my code, I had no idea what that RAPIDclient was either!!

By restricting the search using _RAPIDclient_ I found a [question on StackOverflow](https://stackoverflow.com/questions/57790563/unable-to-export-connection).  

Unfortunately without an answer.


Whenever this happens, I can't help but thinking at this comic [from XKCD](https://xkcd.com/979/)

![helplessly alone](https://imgs.xkcd.com/comics/wisdom_of_the_ancients.png)

As I tried - without much hope - to manually JSON.stringify my object to reproduce the error locally, I was getting another error
```
{
  "errorMessage": "Unable to stringify body as json:
Converting circular structure to JSON",
  "errorType": "TypeError"
}
```

That somehow made sense, the handler result was somehow invalid and not stringifyable because it contained a **circular reference**, but which?

Googling again for **RAPIDClient** i finally found a [gist](https://gist.github.com/brianleroux/26e4d9795350047c6ae2efcbe57e1291) containing the code of the lambda runtime showing exactly where the issue was occurring.  

Still, not much help.

I then tried to use [util.inspect](https://nodejs.org/api/util.html#util_util_inspect_object_options) which allowed me to console.log the object as a string and therefore show it without falling into the `Unable to stringify response body` error nor the `Converting circular structure to JSON` one. 

Finally a hint. The result of my handler was not really what I was expecting.  I indeed forgot to destructure the response from my Axios request and instead of returning the data I was interested in, I was returning the entire thing. 

Problem is that [Axios Response](https://github.com/axios/axios#response-schema) contains a circular reference of the Request, Response, HTTPMessage, etc and therefore it could not be stringified. 
As soon as I made that tiny change everything worked like a charm.

I am pretty sure the original question on StackOverflow was caused by something stupid and tricky like this and solved by the author without much trouble, unfortunately, it was never updated with the solution.  

To avoid this helpless loneliness then, whenever you struggle and find a solution to some nasty bug, **document it publicly**,  it might help someone else in the near or far future!

Hope it helps


---
<span>Photo by <a href="https://unsplash.com/@christnerfurt?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Christian Erfurt</a> on <a href="https://unsplash.com/s/photos/desperate?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></span>