adjusted exception handling to set the correct header based on the exception.