-
Notifications
You must be signed in to change notification settings - Fork 62
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
There is not good way to catch "File not found" errors on static assets #139
Comments
Have you tried creating a global exception filter? |
Yes. Sorry if that was not clear from the original issue. My exception filter aimed at catching these 500 errors in order to convert them to 404s. The global exception filter does not get hit. I know this because my log statements did not come across the console. I can confirm that an instance of the exception filter was created, just not used. To create the global exception filter, I added a provider to my module using the documentation specified at docs.nestjs.com. |
Please, provide a minimal repository which reproduces your issue. |
Just ran into the same issue. Even if an exception filter would be working, I think, a 404 would be the correct HTTP response code. |
I'll see about getting a reproducible repo tonight. |
@kamilmysliwiec Run into the same issue. Was able to catch the error and handle it appropriately here:
Main gotcha for me was the Catch decorator, since the error thrown is not an HttpException, be sure to leave catch everything. |
@cyrilselasi have you found a good way to use the filter only on the route where you are serving static content? |
Not yet, I have it set as a global filter at the moment. That's working fine for my case |
@kamilmysliwiec @tristan957 here is a repo that reproduces the issue https://github.com/micaelmbagira/nestjs-service-static-139. |
Same issue here. Returning a 404 status code instead of catching the error 500 with a global exception filter is definitely the best option. |
PRs are more than welcome (if anyone is interested) :) |
@kamilmysliwiec I think maybe all I will try later today. Does not seem too hard to add at least (if I understand the code correctly) for the Express side of things which I use. |
Is it possible to disable index fallback to avoid this exception?
|
I have same issue |
@raphaelsoul I don't think so due to the following:
serve-static/lib/loaders/abstract.loader.ts Lines 13 to 15 in 72dc754
|
serve-static/lib/loaders/express.loader.ts Lines 44 to 59 in d0f4a89
I think it is the best practice. we dont need a global exeception filter
For SPAs it is never a problem cause you will always have an For serving static file in traditional frontend project, just give a non-empty
UPDATE:in some occasions, the solution above(give non empty serveRoot) still not works. For Example, you want to serve
code below does not throw exceptions to built-in ExceptionsHandler class
you can check the reason here(for express) every request url start It is very tricky. Seems we need robust code in @nestjs/serve-static library |
A other use case I have have is serving a static SPA where we need to inject data into the index.html. It needs to be routed through a controller. Currently i can have a controller that handles /index.html but /some-other-route would serve the index.html unparsed. |
It is possible to use async await in nestjs fastify to solve this problem app.get(renderPath, async (req, res) => {
await new Promise((resolve, reject) => {
const stream = fs.createReadStream(indexFilePath);
stream.on("data", (chunk) => {
res.type('text/html').send(chunk);
resolve();
});
stream.on("error", () => {
reject(new NotFoundException()))
});
});
}); |
* fix error with [this link](https://github.com/nestjs/serve-static/issues/139\#issuecomment-1042809342) * Sidebar work in progress * Sidebar work finished * Navbar issues modif * Navbar done * navbar change button sign in and add sign up * responsive bug * Sidebar and navbar with searchbar done * Add assets directory * Fix after review --------- Co-authored-by: Siguenza92 <[email protected]>
I'm currently encountering a "404 File Not Found" error when using the HTTP custom exception filter. This poses an issue because we have static files. Ideally, we'd like to provide a more user-friendly experience than just a JSON response or header status code. Is there a way to implement a colorful custom 404 error page using static files in NestJS? I haven't been able to find a method within NestJS itself, so I resorted to serving static files through Nginx and configuring error handling there. |
@tuanpq1998 you can do something like this: import { ArgumentsHost, Catch, ExceptionFilter } from "@nestjs/common";
import { NextFunction } from "express";
import path, { join } from "path";
import { fileURLToPath } from "url";
@Catch()
export class NotFoundExceptionFilter implements ExceptionFilter {
catch(exception : any, host: ArgumentsHost) {
const ctx = host.switchToHttp();
const response = ctx.getResponse();
const next = ctx.getNext<NextFunction>();
if ( exception.code === "ENOENT" ) {
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
response.sendFile(join(__dirname, '..', '..', 'public', '404_page.html'));
} else {
next(exception);
}
}
} And then in your app.module.ts add the provider:
So something like:
This will serve /public/404_page.html when a file is not found. |
I'm submitting a...
Current behavior
So if you setup the ServeStaticModule and provide serveStaticOptions: { fallthrough: false }, the module reports a 500 (Internal Server) error.
I don't know why this is not coming clearly to me, but I want to change the 500 error to a 404, because the file was not found.
I tried to use a global Internal Server filter and match the route in the case of 500 errors, but that wasn't possible because for some reason the filter doesn't get hit. I must be missing something higher level about NestJS.
Expected behavior
I expect there to be an ergonomic way to handle errors when fallthrough is set to false.
Minimal reproduction of the problem with instructions
Use the example in the nest repo: https://github.com/nestjs/nest/blob/master/sample/24-serve-static/src/app.module.ts#L8
and configure your ServeStaticModule to also serve your home directory (just an example) include
serveStaticOptions: { fallthrough: false }
. Then after you have set up the mount point, start the server and enter a URL under the mount point that would not exist in your home directory. Your browser should report a 500 error.What is the motivation / use case for changing the behavior?
It currently seems either impossible or not easily discoverable on how a developer can catch the serve-static 500 error and change it to a 404 for when a file is not found.
Environment
The text was updated successfully, but these errors were encountered: