The error in that case seems to be because the upgrade exceeded the maximum amount of time allowed for scripts in your server. Preventing that error message isn't an easy task as we can't know beforehand how long it will take to perform the upgrade. I think that a better solution would be to perform the upgrade in small steps, so that a lower timeout limit would be enough for the upgrade to work, but still some queries would be hard to split into smaller ones and could still exceed the timeout limit. Once the timeout limit is exceeded there's nothing we can do in the application to show some error message, that is up to the server.
Improving how the upgrade works is something that I very much want to do, but it is quite some work.