Check the error name instead of number.

Also use the lib function instead of repeating the code.