question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

onError is NOT being called when an Error is thrown

See original GitHub issue

Expected Behaviour When an Error is thrown in a resolver onError passed to useServer should be called.

Actual Behaviour When an Error is thrown in a resolver onNext is called, instead of onError.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
enisdenjocommented, Dec 22, 2021

I won’t add a customFormatErrorFn because graphql-ws does not (and should not) care about the contents of an operation result.

Nevertheless, you can format your errors directly in the onNext hook as well as the onError hook, roughly:

import { GraphQLObjectType, GraphQLSchema, GraphQLString } from 'graphql';
import { useServer } from 'graphql-ws/lib/use/ws';
import { WebSocketServer } from 'ws';

const Query = new GraphQLObjectType({
  name: 'Query',
  fields: {
    hello: {
      type: GraphQLString,
      resolve() {
        throw new Error('Something went wrong');
      },
    },
  },
});

const schema = new GraphQLSchema({
  query: Query,
});

useServer(
  {
    schema,
    onError: (ctx, _msg, errors) => {
      return yourErrorFormatter(ctx, errors);
    },
    onNext: (ctx, _msg, _args, result) => {
      if (result.errors) {
        // if there are errors in the operation result, format them and return the altered result
        result.errors = yourErrorFormatter(ctx, result.errors);
        return result;
      }
      // otherwise do nothing
    },
  },
  new WebSocketServer({ port: 4000 }),
);
1reaction
asadhazaracommented, Dec 22, 2021

Okay, thank you @enisdenjo for the quick replies!

Read more comments on GitHub >

github_iconTop Results From Across the Web

window.onerror not working in chrome
window.onerror is not triggered when the console directly generates an error. It can be triggered via setTimeout though, e.g., setTimeout(function() ...
Read more >
invoke.onError transition not taken when error is thrown #696
onError gets invoked, no error is reported to the console, and I decide what to do with the thrown error. Actual Result.
Read more >
Window: error event - Web APIs - MDN Web Docs - Mozilla
The error event is fired on a Window object when a resource failed to load or couldn't be used — for example if...
Read more >
Better Error Handling With window.onerror
While window.onerror is an event handler, there is no error event being fired: instead, when there is an uncaught exception or compile-time ...
Read more >
Error Handling — The Swift Programming Language ...
A throwing function propagates errors that are thrown inside of it to the scope from which it's called. Note. Only throwing functions can...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found