Prevent console error with Mongoose, node and graphql

384
March 25, 2017, at 06:20 AM

I'm making an API server with Express, Graphql (Apollo server) and mongoose.

I'm testing the user creation. When the email is duplicated mongoose throws an error (Validation error. Unique = true) and graphql handles very good. But the console (terminal) shows the error too. How can i prevent that issue?

Resolver:

const MODEL_PATH = '../../models';
const User = require(MODEL_PATH + '/User');
const { register } = require('../../controllers/auth/RegisterController');
module.exports = {
  RootQuery: {
    users() {
      return User.find({});
    }
  },
  Mutation: {
    registerUser(_, data) {
      return register(data);
    }
  }
};

RegisterController (register function)

exports.register = function(data) {
  const { email, password } = data;
  const user = new User({
    email,
    password
  });
  return new Promise((resolve, reject) => {
    user.save().then((user) => {
      resolve(user);
    }).catch((err) => {
      reject(err);
    });
  });
};

And the error in the console (I DON'T WANT THAT. I HANDLED THE ERROR IN THE CONTROLLER. I WANT THE ERROR ONLY IN THE GRAPHQL RESPONSE)

MongoError: E11000 duplicate key error collection: y.users index: email_1 dup key: { : "test@example.com" }
    at Function.MongoError.create (/Volumes/Datos/x/code/y/server/node_modules/mongodb-core/lib/error.js:31:11)
    at toError (/Volumes/Datos/x/code/y/server/node_modules/mongodb/lib/utils.js:114:22)
   ....

Response in Graphiql (That is ok)

{
  "data": {
    "registerUser": null
  },
  "errors": [
    {
      "message": "E11000 duplicate key error collection: y.users index: email_1 dup key: { : \"test@example.com\" }",
      "locations": [
        {
          "line": 9,
          "column": 3
        }
      ],
      "path": [
        "registerUser"
      ]
    }
  ]
}

Thank you

Answer 1

check out apollo-errors:

https://github.com/thebigredgeek/apollo-errors

and apollo-resolvers:

https://github.com/thebigredgeek/apollo-resolvers

These packages, together, were made to handle issues like the one you describe

Answer 2

According to: http://dev.apollodata.com/tools/graphql-server/setup.html

in server

app.use('/graphql', bodyParser.json(), graphqlExpress({ schema: myschema, debug: false }));

debug false did the trick.

Now. I'm check the packages for better error handling

Thanks

READ ALSO
NodeJS from txt to JSON [on hold]

NodeJS from txt to JSON [on hold]

I have a question to ask yourself: I have a structuredtxt file:

341
How to build an online compiler using Node.js?

How to build an online compiler using Node.js?

Actually, I am building a web application that let users to compile their C, C++, Java, Python, C# and VB code for a given problem using Nodejs

367
How to run jquery-chat on AWS

How to run jquery-chat on AWS

I'm first time to use AWS, and I'm trying to install the jquery-chat from https://githubcom/lovelle/jquery-chat to test the demo live chat

556
Paginating a Root Query field using join-monster library (GraphQL)

Paginating a Root Query field using join-monster library (GraphQL)

I'm struggling to implement Application Layer Paging with the join-monster library following the docs: http://join-monsterreadthedocs

326