How to Avoid throwing 400 errors from ExpressJS

Kevin Danikowski

Background: I am getting 400 errors (and a blank 400 page response) from a malformed cookie on my site (coming from one of my affiliate pages). I have wrote a code to delete the cookie, but some of my customers still get an error because it's in their cache. Once they clear their cache the problem is fixed.

This issue is circumvented all together in my .NET code base by simply ignoring a 400 error, I would like to do the same in Express JS.

Ask: I would like to have express ignore all cookies on a 400 error (often caused by defective cookies) or to at least ignore the error all together. Is there any way to do this?

My ClearDefectiveCookies function for reference (functioning correctly)

const clearDefectiveCookies = (req, res, next) => {
  const cookies = req.cookies;
  const defectiveCookies = Object.keys(cookies).filter(key => typeof cookies[key] === 'string' && (cookies[key].charAt(0) === '='));

  if (defectiveCookies.length > 0) {
    defectiveCookies.forEach(defectiveCookie => {
      res.clearCookie(defectiveCookie);
    });
  }

  next();
};

Problematic Cookie for Reference

Name: xyz_123 and value: =NaN=xyz=123

Kevin Danikowski

So the answer is actually that node had a breaking change in November 2018, it isn't the cookie that is the issue, it's that node has a max cookie size causing the 400 errors. It isn't actually express..

See answer here: Why am I getting 400 errors? Node, Express, ReactJS

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to avoid throwing exception from finally block

How to handle specific errors with expressjs?

Elastic dump from 8.9 to 2.3 throwing errors

Handle MediaPlayer exceptions from throwing infinite errors

How can I avoid ExpressJS misinterpreting routes?

How to avoid returning an optional after throwing an exception?

How to redirect 404 errors to a page in ExpressJS?

How to prevent NumPy's standard deviation from throwing errors during extreme-volume computations?

How can I prevent throwing errors in errorhandler?

How to test errors throwing in redux actions

How to avoid floating point errors?

How to avoid Dialyzer errors for protocols?

How to I make sure that throwing a HttpClientErrorException causes an HTTP 400 response?

How do I stop Aurelia from throwing JS Errors, when clicking in page links (Fragment Identifiers) when it sees a # in the URL & attempts to reroute?

throwing redux-form submissions errors from handleSubmit functions

node js callback from native C++ code is throwing errors

Bash config file throwing errors when sourced from Perl script

How to avoid CORS errors ("Failed to fetch" or "Server not found or an error occurred") when making requests from Swagger Editor?

Perl: How to avoid regex UTF-8 errors when reading from middle of file

How does MagicMock avoid throwing AttributeError when a random method is called?

How to avoid duplicate entries in a MySQL database without throwing an error

How to avoid 'undefined' errors in nested objects

How to avoid 'cannot read property of undefined' errors?

How to avoid errors when dictionary is empty?

How to use omit with Ansible and avoid any errors?

How could avoid these terminal errors for ionic installation?

Angular how to avoid that errors break valueChanges

How can openat avoid TOCTTOU errors?

How to avoid std::filesystem linker errors with Qt?