Repository: mercurius-js/mercurius
Pull request #314 (2020-10-11 08:28:59)

Improve error messages
I noticed odd wording in one of the error messages which lead to few other places too.
I updated the messages based on my best judgement, the changes are open for debate if there is a disagreement.

mcollina: (The changes are fine, good work!)
aldis-ameriks: >While we are touching this, it would be great to start using fastify-errors and error codes.
I could introduce the lib and refactor the errors in a separate PR.
mcollina: Go for a separate PR!

Patch:
diff --git a/index.js b/index.js
index 91217bb..7317a35 100644
--- a/index.js
+++ b/index.js
@@ -123,7 +123,7 @@ const plugin = fp(async function (app, opts) {
   }
 
   if (gateway && (schema || opts.resolvers || opts.loaders)) {
-    throw new Error('Adding "schema", "resolvers", "loaders" or to plugin options when plugin is running in gateway mode is not allowed')
+    throw new Error('Adding "schema", "resolvers" or "loaders" to plugin options when plugin is running in gateway mode is not allowed')
   }
 
   if (typeof schema === 'string') {
@@ -163,7 +163,7 @@ const plugin = fp(async function (app, opts) {
           }
         }, gateway.pollingInterval)
       } else {
-        app.log.warn('\'gateway.pollingInterval\' shoud have a number type')
+        app.log.warn(`Expected a number for 'gateway.pollingInterval', received: ${typeof gateway.pollingInterval}`)
       }
     }
 
@@ -242,7 +242,7 @@ const plugin = fp(async function (app, opts) {
 
   fastifyGraphQl.extendSchema = function (s) {
     if (gateway) {
-      throw new Error('Calling extendSchema method is not allowed when plugin is running in gateway mode is not allowed')
+      throw new Error('Calling extendSchema method when plugin is running in gateway mode is not allowed')
     }
 
     if (typeof s === 'string') {
@@ -256,7 +256,7 @@ const plugin = fp(async function (app, opts) {
 
   fastifyGraphQl.defineResolvers = function (resolvers) {
     if (gateway) {
-      throw new Error('Calling defineResolvers method is not allowed when plugin is running in gateway mode is not allowed')
+      throw new Error('Calling defineResolvers method when plugin is running in gateway mode is not allowed')
     }
 
     for (const name of Object.keys(resolvers)) {
@@ -303,7 +303,7 @@ const plugin = fp(async function (app, opts) {
 
   fastifyGraphQl.defineLoaders = function (loaders) {
     if (gateway) {
-      throw new Error('Calling defineLoaders method is not allowed when plugin is running in gateway mode is not allowed')
+      throw new Error('Calling defineLoaders method when plugin is running in gateway mode is not allowed')
     }
 
     // set up the loaders factory
@@ -417,7 +417,7 @@ const plugin = fp(async function (app, opts) {
       const operationAST = getOperationAST(document, operationName)
       if (operationAST.operation !== 'query') {
         const err = new MethodNotAllowed()
-        err.errors = [new Error('Operation cannot be perfomed via a GET request')]
+        err.errors = [new Error('Operation cannot be performed via a GET request')]
         throw err
       }
     }


Test patch:
diff --git a/tap-snapshots/test-routes.js-TAP.test.js b/tap-snapshots/test-routes.js-TAP.test.js
index f943bbc..9c18269 100644
--- a/tap-snapshots/test-routes.js-TAP.test.js
+++ b/tap-snapshots/test-routes.js-TAP.test.js
@@ -95,7 +95,7 @@ exports['test/routes.js TAP mutation with GET errors > must match snapshot 1'] =
   "data": null,
   "errors": [
     {
-      "message": "Operation cannot be perfomed via a GET request"
+      "message": "Operation cannot be performed via a GET request"
     }
   ]
 }
diff --git a/test/gateway/config.js b/test/gateway/config.js
index dfbe607..957d2dd 100644
--- a/test/gateway/config.js
+++ b/test/gateway/config.js
@@ -4,7 +4,7 @@ const { test } = require('tap')
 const Fastify = require('fastify')
 const GQL = require('../..')
 
-test('"schema" option not allowed in gateway moode', async (t) => {
+test('"schema" option not allowed in gateway mode', async (t) => {
   const app = Fastify()
   const schema = `
     type Query {
@@ -22,11 +22,11 @@ test('"schema" option not allowed in gateway moode', async (t) => {
   try {
     await app.ready()
   } catch (err) {
-    t.is(err.message, 'Adding "schema", "resolvers", "loaders" or to plugin options when plugin is running in gateway mode is not allowed')
+    t.is(err.message, 'Adding "schema", "resolvers" or "loaders" to plugin options when plugin is running in gateway mode is not allowed')
   }
 })
 
-test('"resolvers" option not allowed in gateway moode', async (t) => {
+test('"resolvers" option not allowed in gateway mode', async (t) => {
   const app = Fastify()
 
   app.register(GQL, {
@@ -39,11 +39,11 @@ test('"resolvers" option not allowed in gateway moode', async (t) => {
   try {
     await app.ready()
   } catch (err) {
-    t.is(err.message, 'Adding "schema", "resolvers", "loaders" or to plugin options when plugin is running in gateway mode is not allowed')
+    t.is(err.message, 'Adding "schema", "resolvers" or "loaders" to plugin options when plugin is running in gateway mode is not allowed')
   }
 })
 
-test('"loaders" option not allowed in gateway moode', async (t) => {
+test('"loaders" option not allowed in gateway mode', async (t) => {
   const app = Fastify()
 
   app.register(GQL, {
@@ -56,6 +56,6 @@ test('"loaders" option not allowed in gateway moode', async (t) => {
   try {
     await app.ready()
   } catch (err) {
-    t.is(err.message, 'Adding "schema", "resolvers", "loaders" or to plugin options when plugin is running in gateway mode is not allowed')
+    t.is(err.message, 'Adding "schema", "resolvers" or "loaders" to plugin options when plugin is running in gateway mode is not allowed')
   }
 })
diff --git a/test/gateway/methods.js b/test/gateway/methods.js
index 8bd647a..024bbef 100644
--- a/test/gateway/methods.js
+++ b/test/gateway/methods.js
@@ -54,7 +54,7 @@ test('calling defineLoaders throws an error in gateway mode', async (t) => {
       }
     })
   } catch (err) {
-    t.is(err.message, 'Calling defineLoaders method is not allowed when plugin is running in gateway mode is not allowed')
+    t.is(err.message, 'Calling defineLoaders method when plugin is running in gateway mode is not allowed')
   }
 })
 
@@ -93,7 +93,7 @@ test('calling defineResolvers throws an error in gateway mode', async (t) => {
       }
     })
   } catch (err) {
-    t.is(err.message, 'Calling defineResolvers method is not allowed when plugin is running in gateway mode is not allowed')
+    t.is(err.message, 'Calling defineResolvers method when plugin is running in gateway mode is not allowed')
   }
 })
 
@@ -132,7 +132,7 @@ test('calling extendSchema throws an error in gateway mode', async (t) => {
       }
     `)
   } catch (err) {
-    t.is(err.message, 'Calling extendSchema method is not allowed when plugin is running in gateway mode is not allowed')
+    t.is(err.message, 'Calling extendSchema method when plugin is running in gateway mode is not allowed')
     t.end()
   }
 })