when i upload a file that is too large compared to the limit i have set in the “bodyParser” config, i have this message displayed client-side :
Something went wrong: CORS or Server unreachable while fetching METHOD: POST URL: [action url]
i set a simple middleware like this, and i see in the browser (in devTools > Network) that it correctly changes the response status. However the body of the response doesn’t seem to be passed properly, and i don’t get my custom message displayed. Here is my middleware :
app.use((err, req, res, next) => {
if (err && err.type === 'entity.too.large') {
res.status(500).send({ error: 'File is too large' })
return
}
return next(err)
})
Expected behavior
I expect to see my message “File is too large” instead of “Something went wrong…”
It works when i return an error in a smart action controller, i don’t see what’s different here.
Context
Project name: ma-domiciliation
Team name: all
Environment name: all
Database type: postgres
Recent changes made on your end if any: …
And, if you are self-hosting your agent:
Agent technology: node.js
Agent (forest package) name & version (from your .lock file): “forest-express-sequelize”: “^9.6.1”,
Thanks for your answer but it still doesn’t work with a 400 error.
in this documentation it’s the code of the smart action controller, if i return an error in the controller it indeed works (event if the status is not 400), but in my case the controller’s code is never reached,
i think the bodyParser throws an error, so it never reaches the controller, and then even if i add a middleware at the end of the chain that intercepts this error and changes the response, something in forest-express-sequelize somehow changes my response before it is sent.
in the end i changed the bodyParser limit to a larger value, i created a middleware that checks the content-length of the request and res.send an error if it is too large.
it still doesn’t work if i use it with “app.use(fileSizeMiddleware)”, but it works if i put it in the router declaration of the smart action controllers, like this :
Ha yes of course, my bad for the poor response. Indeed i understand now, you must put your middleware after the forest usage because the forest router add some header and stuff to discuss with the app.
So that’s why you have some CORS issue.
Happy to see it works now.