How to change status code for response?

How can I can change the status code for a response?
For example when requesting an invalid ressource I want to return 404.
I just found some error related stuff for async calls