Improve error type conversion function