Giter Site home page Giter Site logo

Comments (7)

enisdenjo avatar enisdenjo commented on May 21, 2024 1

Ah, ok, I misread - the error is thrown properly, but you have problems writing the headers.

Writing a 200: OK is necessary in order to establish an SSE connection; meaning, headers are already written and flushed at the point where onNext is reached because there's an active and accepted SSE connection.

from graphql-sse.

enisdenjo avatar enisdenjo commented on May 21, 2024 1

Since re-writing the header is not possible, you can catch the error in onNext and populate the errors GraphQL result field to transmit to the client through the next message.

Something around the edges:

import http from 'http';
import { createHandler } from 'graphql-sse';

const handler = createHandler({
  // ... your other options ...
  onNext: () => {
    try {
      mightThrowAnError();
    } catch (err) {
      return {
        errors: [new GraphQLError(err)],
      };
    }
  },
});

http.createServer(async (req, res) => {
  try {
    await handler(req, res);
  } catch (err) {
    if (!res.headersSent) {
      res.writeHead(500, 'Internal Server Error').end();
    } else {
      console.error('Internal Server Error', err);
    }
  }
});

But beware, I do NOT recommend this. You should absolutely make sure that nothing is thrown from onNext. Execution errors, errors that are a part of the next message, are not terminal - the client will not end and the operation will proceed blindly.

from graphql-sse.

asadhazara avatar asadhazara commented on May 21, 2024 1

Thank you @enisdenjo! The example I have used is directly copied from the JSDoc for the Handler. I have submitted a PR for this.

from graphql-sse.

enisdenjo avatar enisdenjo commented on May 21, 2024

Hmm, I couldn't manage to replicate your issue, I've added tests describing your case, and they pass.

Can you elaborate further? Which version are you using? How are you connecting to the server? Which client are you using?

Ideally a full repro would be perfect and much appreciated!

from graphql-sse.

asadhazara avatar asadhazara commented on May 21, 2024

If I want to write a different head, what are the options? For instance, I would like to send a 500 when something goes wrong in a resolver.

from graphql-sse.

enisdenjo avatar enisdenjo commented on May 21, 2024

You cannot write a different header. As mentioned before, it is already written and flushed at that point.

You can only leave a log on your server and fix the onNext handler.

from graphql-sse.

enisdenjo avatar enisdenjo commented on May 21, 2024

🎉 This issue has been resolved in version 1.1.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

from graphql-sse.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.