Skip to content

Getting "Internal error occurred during message handling. Please check your implementation." after following recipe #535

Answered by enisdenjo
jhalborg asked this question in Q&A
Discussion options

You must be logged in to vote

For graceful handling without implementing your own server, you could just use a try/catch when validating the token and return false from onConnect. Something like:

import { useServer } from 'graphql-ws/lib/use/ws';
import { validateAuth } from './my-auth';

const server = useServer(
  {
    schema,
    onConnect: async (ctx) => {
      try {
        await validateAuth(ctx.connectionParams);
      } catch {
        // returning false indicates that the user has been forbidden access
        // the server will close the socket with a `4403: Forbidden`
        return false;
      }
    },
  },
  wsServer,
);

This is also showcased in the "ws server and client auth usage with token expirati…

Replies: 1 comment 10 replies

Comment options

You must be logged in to vote
10 replies
@jhalborg
Comment options

@jhalborg
Comment options

@jhalborg
Comment options

@enisdenjo
Comment options

Answer selected by jhalborg
@jhalborg
Comment options

@enisdenjo
Comment options

@jhalborg
Comment options

@enisdenjo
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants
Converted from issue

This discussion was converted from issue #533 on February 12, 2024 17:19.