Repository: fastify/fastify
File: fastify.js (JavaScript)

Code before:
    this.getSchema = schemaController.getSchema.bind(schemaController)
    this.getSchemas = schemaController.getSchemas.bind(schemaController)
    return this
  }

  function setReplySerializer (replySerializer) {
    throwIfAlreadyStarted('Cannot call "setReplySerializer"!')

    this[kReplySerializerDefault] = replySerializer
    return this
  }

  // wrapper that we expose to the user for configure the custom error handler
  function setErrorHandler (func) {
    throwIfAlreadyStarted('Cannot call "setErrorHandler"!')

    if (typeof func !== 'function') {
      throw new FST_ERR_ERROR_HANDLER_NOT_FN()
    }

    if (!options.allowErrorHandlerOverride && this[kErrorHandlerAlreadySet]) {
      throw new FST_ERR_ERROR_HANDLER_ALREADY_SET()
    } else if (this[kErrorHandlerAlreadySet]) {
      this.log.warn('It seems that you are overriding an errorHandler in the same scope, which can lead to subtle bugs.')
      this.log.warn("To disable this behavior, set 'allowErrorHandlerOverride' to false or ignore this message. For more information, visit: https://fastify.dev/docs/latest/Reference/Server/#allowerrorhandleroverride")
    }

    this[kErrorHandlerAlreadySet] = true
    this[kErrorHandler] = buildErrorHandler(this[kErrorHandler], func.bind(this))
    return this
  }

  function setChildLoggerFactory (factory) {
    throwIfAlreadyStarted('Cannot call "setChildLoggerFactory"!')

    this[kChildLoggerFactory] = factory
    return this
  }

  function printRoutes (opts = {}) {
    // includeHooks:true - shortcut to include all supported hooks exported by fastify.Hooks
    opts.includeMeta = opts.includeHooks ? opts.includeMeta ? supportedHooks.concat(opts.includeMeta) : supportedHooks : opts.includeMeta
    return router.printRoutes(opts)
  }

  function wrapRouting (router, { rewriteUrl, logger }) {
    let isAsync
    return function preRouting (req, res) {
      // only call isAsyncConstraint once
      if (isAsync === undefined) isAsync = router.isAsyncConstraint()
      if (rewriteUrl) {

Review comment (bug):
> Emitting a warning breaks a lot of tests, forcing us to update every impacted instance to allowErrorHandlerOverride: false. I don’t think we should update all the tests every time we want to log a deprecation warning.

I tested this diff and it does not break:

<img width="823" alt="image" src="https://github.com/user-attachments/assets/a9215602-2503-4836-beac-104b570bbd85" />


```diff
diff --git a/fastify.js b/fastify.js
index 141e34f9..8237580e 100644
--- a/fastify.js
+++ b/fastify.js
@@ -51,6 +51,7 @@ const build404 = require('./lib/fourOhFour')
 const getSecuredInitialConfig = require('./lib/initialConfigValidation')
 const override = require('./lib/pluginOverride')
 const noopSet = require('./lib/noop-set')
+const { FSTWRN004 } = require('./lib/warnings')
 const {
   appendStackTrace,
   AVVIO_ERRORS_MAP,
@@ -865,8 +866,8 @@ function fastify (options) {
     if (!options.allowErrorHandlerOverride && this[kErrorHandlerAlreadySet]) {
       throw new FST_ERR_ERROR_HANDLER_ALREADY_SET()
     } else if (this[kErrorHandlerAlreadySet]) {
-      this.log.warn('It seems that you are overriding an errorHandler in the same scope, which can lead to subtle bugs.')
-      this.log.warn("To disable this behavior, set 'allowErrorHandlerOverride' to false or ignore this message. For more information, visit: https://fastify.dev/docs/latest/Reference/Server/#allowerrorhandleroverride")
+      FSTWRN004('listen method') // ('It seems that you are overriding an errorHandler in the same scope, which can lead to subtle bugs.')
+      FSTWRN004("To disable this behavior, set 'allowErrorHandlerOverride' to false or ignore this message. For more information, visit: https://fastify.dev/docs/latest/Reference/Server/#allowerrorhandleroverride")
     }
 
     this[kErrorHandlerAlreadySet] = true
diff --git a/lib/warnings.js b/lib/warnings.js
index a9cb27f9..e0aeea06 100644
--- a/lib/warnings.js
+++ b/lib/warnings.js
@@ -25,6 +25,13 @@ const FSTWRN003 = createWarning({
   unlimited: true
 })
 
+const FSTWRN004 = createWarning({
+  name: 'FastifyWarning',
+  code: 'FSTWRN003',
+  message: 'The %s mixes async and callback styles that may lead to unhandled rejections. Please use only one of them.',
+  unlimited: true
+})
+
 const FSTSEC001 = createWarning({
   name: 'FastifySecurity',
   code: 'FSTSEC001',
@@ -35,5 +42,6 @@ const FSTSEC001 = createWarning({
 module.exports = {
   FSTWRN001,
   FSTWRN003,
+  FSTWRN004,
   FSTSEC001
 }
```

Note the error message is just a copy-paste

Code after:
    this[kSchemaController] = schemaController
    this.getSchema = schemaController.getSchema.bind(schemaController)
    this.getSchemas = schemaController.getSchemas.bind(schemaController)
    return this
  }

  function setReplySerializer (replySerializer) {
    throwIfAlreadyStarted('Cannot call "setReplySerializer"!')

    this[kReplySerializerDefault] = replySerializer
    return this
  }

  // wrapper that we expose to the user for configure the custom error handler
  function setErrorHandler (func) {
    throwIfAlreadyStarted('Cannot call "setErrorHandler"!')

    if (typeof func !== 'function') {
      throw new FST_ERR_ERROR_HANDLER_NOT_FN()
    }

    if (!options.allowErrorHandlerOverride && this[kErrorHandlerAlreadySet]) {
      throw new FST_ERR_ERROR_HANDLER_ALREADY_SET()
    } else if (this[kErrorHandlerAlreadySet]) {
      FSTWRN004("To disable this behavior, set 'allowErrorHandlerOverride' to false or ignore this message. For more information, visit: https://fastify.dev/docs/latest/Reference/Server/#allowerrorhandleroverride")
    }

    this[kErrorHandlerAlreadySet] = true
    this[kErrorHandler] = buildErrorHandler(this[kErrorHandler], func.bind(this))
    return this
  }

  function setChildLoggerFactory (factory) {
    throwIfAlreadyStarted('Cannot call "setChildLoggerFactory"!')

    this[kChildLoggerFactory] = factory
    return this
  }

  function printRoutes (opts = {}) {
    // includeHooks:true - shortcut to include all supported hooks exported by fastify.Hooks
    opts.includeMeta = opts.includeHooks ? opts.includeMeta ? supportedHooks.concat(opts.includeMeta) : supportedHooks : opts.includeMeta
    return router.printRoutes(opts)
  }

  function wrapRouting (router, { rewriteUrl, logger }) {
    let isAsync
    return function preRouting (req, res) {
      // only call isAsyncConstraint once
      if (isAsync === undefined) isAsync = router.isAsyncConstraint()
      if (rewriteUrl) {