Return if raising error in optimize

Had a situation where a lossy optimisation unavailable error was
triggering err to be filled, but then data.contents was being checked
while it was null. I think the function should exit early if there is
an error, but feel free to close if wrong (though in that case checking
data.contents is valid first would be good).