[gui] Move error handling into a separate function.