Catch errors and transform them to 500, and cleanups