Handle "Body exceeded X mb limit." errors.
Answered
Saluki posted this in #help-forum
SalukiOP
Hi.
I have a problem with handling errors caused by passing files (images in my case) that are too large to server actions. I am aware of the
Currently, when a user submits a file larger than
I would like to be able to catch this error and return a nicely formatted response to the client, but I don't know where I should put my error handler.
Thanks.
I have a problem with handling errors caused by passing files (images in my case) that are too large to server actions. I am aware of the
bodySizeLimit config option, but I'm not interested in changing this limit, I want to change what happens when this limit is reached.Currently, when a user submits a file larger than
bodySizeLimit using a server action, on the server side I get uncaughtException: v [Error]: Body exceeded 5mb limit. and on the client I get Unhandled Runtime Error TypeError: Failed to fetch ??I would like to be able to catch this error and return a nicely formatted response to the client, but I don't know where I should put my error handler.
Thanks.
Answered by B33fb0n3
That’s not a good pratice. Just increasing the limits is a bad practice. As said: you can check it when the user submits his file (before the server action will be called).
Another thing you might are interested about is: clientside uploads. So the path, where you upload the file is changed:
Now:
Client -> Nextjs Server (Server Action) -> File Storage Server
Future:
Client -> File Storage Server
Like that, you won’t hit any limit, because the nextjs server won’t over get the file. To still be able to limit the user actions, you can create policies and tokens (from your file storage server) to limit what the user can upload and how much
Another thing you might are interested about is: clientside uploads. So the path, where you upload the file is changed:
Now:
Client -> Nextjs Server (Server Action) -> File Storage Server
Future:
Client -> File Storage Server
Like that, you won’t hit any limit, because the nextjs server won’t over get the file. To still be able to limit the user actions, you can create policies and tokens (from your file storage server) to limit what the user can upload and how much
4 Replies
@Saluki Hi.
I have a problem with handling errors caused by passing files (images in my case) that are too large to server actions. I am aware of the `bodySizeLimit` config option, but I'm not interested in changing this limit, I want to change what happens when this limit is reached.
Currently, when a user submits a file larger than `bodySizeLimit` using a server action, on the server side I get `uncaughtException: v [Error]: Body exceeded 5mb limit.` and on the client I get `Unhandled Runtime Error TypeError: Failed to fetch` ??
I would like to be able to catch this error and return a nicely formatted response to the client, but I don't know where I should put my error handler.
Thanks.
I would check it, when the user submits his file. Yes, I would check it clientside, else the server would throw the error. You can also try catch the error and parse it to handle it nicely and display the user the specific (and beautiful) message. You can check the filesize using „file.size“ and then check if it’s above 5 mb
SalukiOP
The problem is that my server action would never be called if the file was larger than
bodySizeLimit. My quick workaround for it is to increase bodySizeLimit to a very large value, so no matter the file size the server action is called, and then validate the file.size inside the action. This works but I don't know if it's the best way to do it.@Saluki The problem is that my server action would never be called if the file was larger than `bodySizeLimit`. My quick workaround for it is to increase `bodySizeLimit` to a very large value, so no matter the file size the server action is called, and then validate the `file.size` inside the action. This works but I don't know if it's the best way to do it.
That’s not a good pratice. Just increasing the limits is a bad practice. As said: you can check it when the user submits his file (before the server action will be called).
Another thing you might are interested about is: clientside uploads. So the path, where you upload the file is changed:
Now:
Client -> Nextjs Server (Server Action) -> File Storage Server
Future:
Client -> File Storage Server
Like that, you won’t hit any limit, because the nextjs server won’t over get the file. To still be able to limit the user actions, you can create policies and tokens (from your file storage server) to limit what the user can upload and how much
Another thing you might are interested about is: clientside uploads. So the path, where you upload the file is changed:
Now:
Client -> Nextjs Server (Server Action) -> File Storage Server
Future:
Client -> File Storage Server
Like that, you won’t hit any limit, because the nextjs server won’t over get the file. To still be able to limit the user actions, you can create policies and tokens (from your file storage server) to limit what the user can upload and how much
Answer
@Saluki solved?