Comments (3)
Looks like the issue got solved when I change the log calls in the event handlers to
fastify.get('/ws', { websocket: true }, (connection, request) => {
fastify.log.info(`Opening websocket connection. No. of clients connected: ${fastify.websocketServer.clients.size}`);
connection.on('close', () => {
fastify.log.info(`Closing websocket connection. No. of clients connected: ${fastify.websocketServer.clients.size}`);
});
So eventually an exception in the handler is the root cause of the problem....
from fastify-websocket.
Good!
from fastify-websocket.
Can we reopen this? Errors inside the handler are currently swallowed, which for me is unexpected behavior and inconsistent with the behavior for regular non-ws routes.
'use strict'
const {SqliteError} = require('better-sqlite3');
const fastify = require('fastify')({
logger: true
})
fastify.register(require('fastify-websocket'))
fastify.get('/ws', { websocket: true }, () => {
throw new Error('demonstration')
})
// Declare a route
fastify.get('/', (request, reply) => {
throw new Error('demonstration')
})
fastify.listen(3000, err => {
if (err) {
fastify.log.error(err)
process.exit(1)
}
})
Connecting to the ws results in
{
"level": 40,
"time": 1615569367599,
"pid": 28959,
"hostname": "alex-desktop",
"reqId": "req-1",
"err": {
"type": "FastifyError",
"message": "Reply was already sent.",
"stack": "FastifyError: Reply was already sent.\n at _Reply.Reply.send (/home/alex/src/issues/issue-256-websocket-swallows-error/node_modules/fastify/lib/reply.js:118:26)\n at preHandlerCallback (/home/alex/src/issues/issue-256-websocket-swallows-error/node_modules/fastify/lib/handleRequest.js:126:11)\n at preValidationCallback (/home/alex/src/issues/issue-256-websocket-swallows-error/node_modules/fastify/lib/handleRequest.js:107:5)\n at handler (/home/alex/src/issues/issue-256-websocket-swallows-error/node_modules/fastify/lib/handleRequest.js:70:7)\n at handleRequest (/home/alex/src/issues/issue-256-websocket-swallows-error/node_modules/fastify/lib/handleRequest.js:18:5)\n at runPreParsing (/home/alex/src/issues/issue-256-websocket-swallows-error/node_modules/fastify/lib/route.js:421:5)\n at Object.routeHandler [as handler] (/home/alex/src/issues/issue-256-websocket-swallows-error/node_modules/fastify/lib/route.js:379:7)\n at Router.lookup (/home/alex/src/issues/issue-256-websocket-swallows-error/node_modules/find-my-way/index.js:339:14)\n at Server.<anonymous> (/home/alex/src/issues/issue-256-websocket-swallows-error/node_modules/fastify-websocket/index.js:50:15)\n at Server.emit (events.js:315:20)",
"name": "FastifyError",
"code": "FST_ERR_REP_ALREADY_SENT",
"statusCode": 500
},
"msg": "Reply already sent"
}
Making an HTTP request
{
"level": 50,
"time": 1615569798536,
"pid": 30071,
"hostname": "alex-desktop",
"reqId": "req-1",
"req": {
"method": "GET",
"url": "/",
"hostname": "localhost:3000",
"remoteAddress": "127.0.0.1",
"remotePort": 50970
},
"res": { "statusCode": 500 },
"err": {
"type": "Error",
"message": "demonstration",
"stack": "Error: demonstration\n at Object.<anonymous> (/home/alex/src/issues/issue-256-websocket-swallows-error/index.js:16:9)\n at Object.routeOptions.handler (/home/alex/src/issues/issue-256-websocket-swallows-error/node_modules/fastify-websocket/index.js:125:24)\n at preHandlerCallback (/home/alex/src/issues/issue-256-websocket-swallows-error/node_modules/fastify/lib/handleRequest.js:124:28)\n at preValidationCallback (/home/alex/src/issues/issue-256-websocket-swallows-error/node_modules/fastify/lib/handleRequest.js:107:5)\n at handler (/home/alex/src/issues/issue-256-websocket-swallows-error/node_modules/fastify/lib/handleRequest.js:70:7)\n at handleRequest (/home/alex/src/issues/issue-256-websocket-swallows-error/node_modules/fastify/lib/handleRequest.js:18:5)\n at runPreParsing (/home/alex/src/issues/issue-256-websocket-swallows-error/node_modules/fastify/lib/route.js:421:5)\n at Object.routeHandler [as handler] (/home/alex/src/issues/issue-256-websocket-swallows-error/node_modules/fastify/lib/route.js:379:7)\n at Router.lookup (/home/alex/src/issues/issue-256-websocket-swallows-error/node_modules/find-my-way/index.js:339:14)\n at Server.emit (events.js:315:20)"
},
"msg": "demonstration"
}
For the ws handler the information about the original new Error('demonstration')
is completely lost. I've basically spent the entire day tracking down a bug and this is one of the reason it took so long.
from fastify-websocket.
Related Issues (20)
- TS2314: Generic type 'FastifyInstance ' requires 5 type argument(s). HOT 4
- Node server crashes with ERR_INTERNAL_ASSERTION error HOT 11
- Unable to connect to WS, status remains connecting on Postman HOT 3
- req.query is undefined HOT 2
- WebSocket type is resolved to any using moduleResolution: "bundler" and ESM HOT 1
- Allow specifying alternative preClose / modifying close flow HOT 2
- Cannot connect to WebSocket server HOT 1
- typebox typings tests fail HOT 1
- connections is of type _Request while request is of type Reply HOT 1
- fix a typescript generic type error
- Fix Websocket Types: TS1259 because "ws" package using "export ="
- preClose not typed HOT 1
- WebSocket stream backpressure causes socket to become paused which results in the socket to no longer emitting 'message' events. HOT 2
- Minimal example of fastify + apollo graphql server + fastify websocket grapqhl subscriptions
- 👋 I would like to contribute to fastify/fastify-websocket HOT 4
- Upgrade to v10 breaks because SocketStream is no longer an exported member HOT 2
- The close event callback function is not triggered. HOT 1
- [bug] Fastify-websocket does not support route prefixes
- `injectWS` throws on node >= 20 HOT 2
- `@types/ws` is missing which results in loosing typing withing handler HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from fastify-websocket.