Implemented proper error handling for the UiContext constructor