As requested, if no onError then default handling

did as you said. slightly changed your code to spare me a var ;)
take care, no block declaration for var in js. scope is the whole function