Example of leaky abstraction

Everytime we abstract a layer, some detail will be hidden. When there is some error or bug inside the abstraction layer, only the engineer understand the implementation of abstracted layer can fix it.

Leaky abstraction is a necessary evil, here is some of my own experience.

Github API and npm ‘request’ module

I use npm ‘request’ module to query pull request of a private repo. I use the correct authetication method, but still fail to get data from the API.

Here is the code copy from the example

1
2
3
4
5
6
7
request.get('http://some.server.com/', {
'auth': {
'user': 'username',
'pass': 'password',
'sendImmediately': false
}
});

Solution

Set sendImmediately to true.

If sendImmediately is false, the first will not have the authentication data. Only if the server reply a 401 response, another request with authentication data will be made. But Github API return 404 instead of 401 for the unauthenticated request, so it does not work.

This bug can only be solved by the engineer who understand the sendImmediately option and the behaviour of Github API. By the way, unit test cannot catch this problem, as every individual parts are correct.

Passing console.log into callback does not work

Sometime we want to test the return value of an asynchronus function, say async(callback). We try to use console.log to echo the result directly. We may write some thing like this

1
async(console.log)

We expect it will output the result of async function, but it print nothing.

Use async(console.log.bind(console))

In the implementation of console.log, this is used. So when we pass the log function to other callback, the console scope is lost, so the function does not work. The solution is to bind the console to the function. Only the engineer understand the implementation of console.log and properties of javascript can understand this problem.

Call for example

If you have similar experience, please put the problem and solution in the comment and share with us. :D