Yes you can handle as following
By having the following javascript/jQuery code within the custom error page head section, it will check whether it's loading inside an iframe. If it's loading inside an iframe, then the custom error page will set as the top page.
<script>
$(function () {
if (window != window.top) { //Checks whether the custom error page is loading inside an iframe
window.top.location = window.location; //Set the top location to custom error page location
}
});
</script>