os: Factor out error checking to a separate function.