Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FastifyWarning not an instance of Error #2

Closed
jamestalton opened this issue Jul 8, 2020 · 2 comments · Fixed by #3
Closed

FastifyWarning not an instance of Error #2

jamestalton opened this issue Jul 8, 2020 · 2 comments · Fixed by #3

Comments

@jamestalton
Copy link

jamestalton commented Jul 8, 2020

I am running into an issue using typescript, fastify, and jest.
A plugin I am using is throwing a FastifyWarning.
Jest blows up with:

    TypeError [ERR_INVALID_ARG_TYPE]: The "warning" argument must be of type string or an instance of Error. Received an instance of FastifyWarning
        at process.emitWarning (internal/process/warning.js)
@jamestalton
Copy link
Author

jamestalton commented Jul 8, 2020

On further investigation... it seems that this is thrown when anything other than a string is passed to process.emitWarning
process.emitWarning(new Error('test'))

This might be a JEST thing...

@jamestalton
Copy link
Author

jamestalton commented Jul 8, 2020

https://github.com/nodejs/node-auto-test/blob/b067260648eb34c5553856729db47482cc7b412d/lib/internal/process/warning.js

if (typeof warning === 'string') {
    warning = createWarningObject(warning, type, code, ctor, detail);
  } else if (!(warning instanceof Error)) {
    throw new ERR_INVALID_ARG_TYPE('warning', ['Error', 'string'], warning);
  }
  if (warning.name === 'DeprecationWarning') {
    if (process.noDeprecation)
      return;
    if (process.throwDeprecation) {
      // Delay throwing the error to guarantee that all former warnings were
      // properly logged.
      return process.nextTick(() => {
        throw warning;
      });
    }
  }

Apparently if the warning name in nodejs is 'DeprecationWarning' it handles differently.
Since many of the fastify warnings are deprecations, they should be updated to that style?
that way they can be ignored with process.noDeprecation or turned into thrown errors using process.throwDeprecation.

This works in my environment:

function emitWarning (code) {
  if (codes[code] === undefined) throw new Error(`The code '${code}' does not exist`)
  if (emittedWarnings.has(code) === true) return
  emittedWarnings.set(code, true)
  const error = new codes[code]()
  process.emitWarning(error.message, "DeprecationWarning", error.code)
}

(node:9771) [FSTDEP003] DeprecationWarning: You are using the legacy Content Type Parser function signature. Use the one suggested in the documentation instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant