Skip to content
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

Fixed bug with calling next Express middleware after sending response #3707

Open
wants to merge 1 commit into
base: integration
Choose a base branch
from

Conversation

Ryszard-Trojnacki
Copy link

Description

There were few places in code when response was send (res.send) and then called next() middleware from ExpressJS.
This caused error in response, because 404 handler was executed and tried to send response (status header) once again.

The exception:

VM560:1 Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
    at ServerResponse.setHeader (node:_http_outgoing:659:11)
    at ServerResponse.header (/home/fruitapp/app/LiteFarm/packages/api/node_modules/express/lib/response.js:794:10)
    at ServerResponse.send (/home/fruitapp/app/LiteFarm/packages/api/node_modules/express/lib/response.js:174:12)
    at ServerResponse.json (/home/fruitapp/app/LiteFarm/packages/api/node_modules/express/lib/response.js:278:15)
    at errorHandler (file:///home/fruitapp/app/LiteFarm/packages/api/dist/api/src/server.js:326:9)
    at Layer.handle_error (/home/fruitapp/app/LiteFarm/packages/api/node_modules/express/lib/router/layer.js:71:5)
    at trim_prefix (/home/fruitapp/app/LiteFarm/packages/api/node_modules/express/lib/router/index.js:326:13)
    at /home/fruitapp/app/LiteFarm/packages/api/node_modules/express/lib/router/index.js:286:9
    at Function.process_params (/home/fruitapp/app/LiteFarm/packages/api/node_modules/express/lib/router/index.js:346:12)
    at next (/home/fruitapp/app/LiteFarm/packages/api/node_modules/express/lib/router/index.js:280:10)
    at file:///home/fruitapp/app/LiteFarm/packages/api/dist/api/src/server.js:336:5
    at Layer.handle [as handle_request] (/home/fruitapp/app/LiteFarm/packages/api/node_modules/express/lib/router/layer.js:95:5)
    at trim_prefix (/home/fruitapp/app/LiteFarm/packages/api/node_modules/express/lib/router/index.js:328:13)
    at /home/fruitapp/app/LiteFarm/packages/api/node_modules/express/lib/router/index.js:286:9
    at Function.process_params (/home/fruitapp/app/LiteFarm/packages/api/node_modules/express/lib/router/index.js:346:12)
    at next (/home/fruitapp/app/LiteFarm/packages/api/node_modules/express/lib/router/index.js:280:10)
    at /home/fruitapp/app/LiteFarm/packages/api/node_modules/express/lib/router/index.js:646:15
    at next (/home/fruitapp/app/LiteFarm/packages/api/node_modules/express/lib/router/index.js:265:14)
    at next (/home/fruitapp/app/LiteFarm/packages/api/node_modules/express/lib/router/route.js:141:14)
    at file:///home/fruitapp/app/LiteFarm/packages/api/dist/api/src/controllers/farmController.js:55:24
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

Code causing error:

// server.ts - error handler
// handle errors
const errorHandler: ErrorRequestHandler = (error, _req, res, _next) => {
  res.status(error.status || 500);
  res.json({
    error: {
      message: error.message,
    },
  });
};

app
  .use((_req, _res, next) => {
    const error: Error & { status?: number } = new Error('Not found');
    error.status = 404;
    next(error);
  })
  .use(errorHandler);

// farmControler.js
          res.status(400).send('No country selected');
          return next();

Status is set res.status(400) then next() is called which exececutes error handler, which trying once again status res.status(error.status || 500);

Jira link: N/A

Type of change

  • Bug fix (non-breaking change which fixes an issue)

How Has This Been Tested?

Tested on self hosted server before there was an error in console now this error not happens.

… in `farmController.js` and `fieldController.js`.
@Ryszard-Trojnacki Ryszard-Trojnacki requested review from a team as code owners March 6, 2025 12:00
@Ryszard-Trojnacki Ryszard-Trojnacki requested review from Duncan-Brain and kathyavini and removed request for a team March 6, 2025 12:00
@kathyavini kathyavini requested a review from antsgar March 7, 2025 23:47
Copy link
Collaborator

@Duncan-Brain Duncan-Brain left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Ryszard-Trojnacki

Sorry for the delayed review. To me this looks ok for farm controller. But field controller actually has a next function in the express routes.

I am not sure the purpose, or if its still working, but there is a weather station id check that looks as though it is supposed to be run after sending a response.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants