Printing errors through a function