Fix possible TypeError when printing an error message

See #1021.