Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

134
Visualizações
express-ws is silencing errors

I am use express and express-ws in a node application. However, express-ws seems to be silencing errors making it very difficult to debug my code.

I've reduced my code down to just the following which still reproduces the problem:

// configure express
var express = require('express');
var app = express();
var expressWs = require('express-ws')(app);

// set up routes

app.get('/', function(req, res) {
    console.log("GET");
    nonExistingFunction(); // Called to demonstrate a traceback is provided.
});

app.ws('/', function(ws, req){    
    console.log("WS");
    nonExistingFunction(); // Called to demonstrate this error is silenced.
});

// start service
app.listen(90);

When I GET from a browser, the console shows the 'GET' log message followed by the traceback (as expected). I.e;

GET
ReferenceError: nonExistingFunction is not defined
    at C:\(project dir)\server.js:11:9
    at Layer.handle [as handle_request] (C:\(project dir)\node_modules\express\lib\router\layer.js:95:5)
    at next (C:\(project dir)\node_modules\express\lib\router\route.js:137:13)
    at Route.dispatch (C:\(project dir)\node_modules\express\lib\router\route.js:112:3)
    at Layer.handle [as handle_request] (C:\(project dir)\node_modules\express\lib\router\layer.js:95:5)
    at C:\(project dir)\node_modules\express\lib\router\index.js:281:22
    at Function.process_params (C:\(project dir)\node_modules\express\lib\router\index.js:335:12)
    at next (C:\(project dir)\node_modules\express\lib\router\index.js:275:10)
    at expressInit (C:\(project dir)\node_modules\express\lib\middleware\init.js:40:5)
    at Layer.handle [as handle_request] (C:\(project dir)\node_modules\express\lib\router\layer.js:95:5)

When I connect via Websocket, the console just shows the 'WS' log message - no traceback. I.e;

WS

Does express-ws silence errors by default? Is there a way to switch it off?

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

I have a solution.

Express has its own default error handler which outputs the traceback to the console but allows the application to continue running. Express-ws uses a try-catch nest to do the same in wrap-middleware.js.

  try {
    /* Unpack the `.ws` property and call the actual handler. */
    middleware(req.ws, req, next);
  } catch (err) {
    /* If an error is thrown, let's send that on to any error handling */
    next(err);
  }

However, this doesn't seem to get passed down to the default error handler. So instead, you can specify a error handler middleware for app. This must be the last middleware in the chain. The following code is a little hacky, but will output tracebacks to the console regardless of whether the error occurred within a HTTP or a WS route function.

// configure express
var express = require('express');
var app = express();
var expressWs = require('express-ws')(app);

// set up routes

app.get('/', function(req, res) {
    console.log("GET");
    nonExistingFunction(); // Called to demonstrate a traceback is provided.
});

app.ws('/', function(ws, req){    
    console.log("WS");
    nonExistingFunction(); // Called to demonstrate this error is no longer silenced :)
});

// set up error handler

function errorHandler (err, req, res, next) {
    if(req.ws){
        console.error("ERROR from WS route - ", err);
    } else {
        console.error(err);
        res.setHeader('Content-Type', 'text/plain');
        res.status(500).send(err.stack);
    }
}
app.use(errorHandler);

// start service
app.listen(90);
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda