handle an error and preserve cursor position