set 500 status code only when no headers sent