Fix errno usage

errno should be captured immediately at error() beginning so that it is not modified by later operations, such as string concatenations. Fixes #8245.