{"id":"f87fd1847dbf9f31","repo":"fastify/fastify","slug":"fst-err-schema-error-formatter-not-fn","errorCode":"FST_ERR_SCHEMA_ERROR_FORMATTER_NOT_FN","errorMessage":"schemaErrorFormatter option should be a non async function. Instead got '%s'.","messagePattern":"schemaErrorFormatter option should be a non async function\\. Instead got '(.+?)'\\.","errorType":"exception","errorClass":"TypeError","httpStatus":500,"severity":"critical","filePath":"fastify.js","lineNumber":1005,"sourceCode":"  }\n\n  // Most devs do not know what to do with this error.\n  // In the vast majority of cases, it's a network error and/or some\n  // config issue on the load balancer side.\n  this.log.trace({ err }, `client ${errorLabel}`)\n  // Copying standard node behavior\n  // https://github.com/nodejs/node/blob/6ca23d7846cb47e84fd344543e394e50938540be/lib/_http_server.js#L666\n\n  // If the socket is not writable, there is no reason to try to send data.\n  if (socket.writable) {\n    socket.write(`HTTP/1.1 ${errorCode} ${errorStatus}\\r\\nContent-Length: ${body.length}\\r\\nContent-Type: application/json\\r\\n\\r\\n${body}`)\n  }\n  socket.destroy(err)\n}\n\nfunction validateSchemaErrorFormatter (schemaErrorFormatter) {\n  if (typeof schemaErrorFormatter !== 'function') {\n    throw new FST_ERR_SCHEMA_ERROR_FORMATTER_NOT_FN(typeof schemaErrorFormatter)\n  } else if (schemaErrorFormatter.constructor.name === 'AsyncFunction') {\n    throw new FST_ERR_SCHEMA_ERROR_FORMATTER_NOT_FN('AsyncFunction')\n  }\n}\n\n/**\n * These export configurations enable JS and TS developers\n * to consume fastify in whatever way best suits their needs.\n * Some examples of supported import syntax includes:\n * - `const fastify = require('fastify')`\n * - `const { fastify } = require('fastify')`\n * - `import * as Fastify from 'fastify'`\n * - `import { fastify, TSC_definition } from 'fastify'`\n * - `import fastify from 'fastify'`\n * - `import fastify, { TSC_definition } from 'fastify'`\n */\nmodule.exports = fastify\nmodule.exports.errorCodes = errorCodes","sourceCodeStart":987,"sourceCodeEnd":1023,"githubUrl":"https://github.com/fastify/fastify/blob/7299a57d3fdce7da49f2c6d19ba08dc673e53f22/fastify.js#L987-L1023","documentation":"Thrown by validateSchemaErrorFormatter (fastify.js:1004-1005), called both at construction (fastify.js:356) and from setSchemaErrorFormatter (fastify.js:735). It fires when `schemaErrorFormatter` is defined but `typeof schemaErrorFormatter !== 'function'`. The formatter must be a regular (non-async) function Fastify can call synchronously to turn validation errors into a single Error.","triggerScenarios":"Calling `fastify({ schemaErrorFormatter: {} })`, `{ schemaErrorFormatter: 'fmt' }`, or `fastify.setSchemaErrorFormatter(undefined)`. Also when the formatter is imported as undefined.","commonSituations":"Passing a configuration object describing a formatter rather than the function; misspelled import; copy-pasting a config snippet that referenced an unbound symbol.","solutions":["Pass a synchronous function: `fastify({ schemaErrorFormatter: (errors, dataVar) => new Error(...) })`.","Omit the option to use the default formatter.","If importing the formatter, verify the export name and that it resolves to a function."],"exampleFix":"// before\nconst app = fastify({ schemaErrorFormatter: { format: myFormat } })\n\n// after\nconst app = fastify({ schemaErrorFormatter: (errors) => new Error(errors.map(e => e.message).join(',')) })","handlingStrategy":"type-guard","validationCode":"function validateFormatter(fn) {\n  if (typeof fn !== 'function') {\n    throw new TypeError('schemaErrorFormatter must be a function, got ' + typeof fn)\n  }\n  return fn\n}\nconst app = fastify(opts.schemaErrorFormatter\n  ? { schemaErrorFormatter: validateFormatter(opts.schemaErrorFormatter) }\n  : {})","typeGuard":"const isFormatter = (fn) => typeof fn === 'function' && fn.constructor.name !== 'AsyncFunction'","tryCatchPattern":"try {\n  app.setSchemaErrorFormatter(formatter)\n} catch (err) {\n  if (err.code === 'FST_ERR_SCHEMA_ERROR_FORMATTER_NOT_FN') {\n    throw new TypeError('schemaErrorFormatter must be a non-async function, got ' + typeof formatter)\n  }\n  throw err\n}","preventionTips":["Provide a synchronous function for schemaErrorFormatter.","If importing the formatter, confirm the export resolves to a function before passing it."],"tags":["configuration","startup","schema","type-error","fastify"],"analyzedSha":"7299a57d3fdce7da49f2c6d19ba08dc673e53f22","analyzedAt":"2026-08-03T17:43:01.300Z","schemaVersion":2}