The correct way of doing it according to the docs is
--- if(notFound) { return Astro.rewrite("/404"); } ---