added checking for undefined errors and custom message