DEV Community

Cover image for Alone against a bug.
Davide de Paolis
Davide de Paolis

Posted on

Alone against a bug.

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

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  ] }
Enter fullscreen mode Exit fullscreen mode

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.

Unfortunately without an answer.

Whenever this happens, I can't help but thinking at this comic from XKCD

helplessly alone

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"
}
Enter fullscreen mode Exit fullscreen mode

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 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 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 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


Photo by Christian Erfurt on Unsplash

Top comments (7)

Collapse
 
neilyonzon1 profile image
Neil Yonzon

This helped out so much! Thank you for this.

Collapse
 
jonasbraga profile image
Jonas Braga

Thanks man
You saved me, I never would have guessed that a console.log could break my code πŸ˜†

Collapse
 
braekevelt profile image
Alexander Braekevelt

I created an account just to upvote your post. I had a similar issue today, thanks!

Collapse
 
dvddpl profile image
Davide de Paolis

Glad it helped!
And now that you have a dev account you could also start sharing your snippets or experiences. We all grow as a community by sharing 😊

Collapse
 
sebto profile image
Vahid Sebto

Thank you. You saved my day.

Collapse
 
andreyddk profile image
Andrey

Today I face same problem)

Collapse
 
dvddpl profile image
Davide de Paolis

I hope this post helped you somehow :-)