Zum Inhalt springen
Blog

Strapi v5 "Internal Server Error" instead of ValidationError

Strapi v5 shows a generic "Internal Server Error" when publish middleware throws ValidationError or ApplicationError. Causes, fixes, and where to validate.

The Liite team10 Min. Lesezeit

Short answer

Check the Strapi server log to see what was really thrown, and make sure you throw errors.ValidationError from @strapi/utils, not a plain Error. If a test middleware that only throws still returns a 500, you have hit the reported v5 issue (strapi/strapi#20343), so move the rule to the content type schema or to a layer that returns clean errors.

You replaced a v4 lifecycle hook with a v5 document service middleware, and you throw ApplicationError or ValidationError when someone publishes. You expected your message in the admin panel. You got a generic failure, and the response is a 500:

Internal Server Error

This article explains the likely causes, gives a fix order, and compares the places you can put a pre-publish rule in v5, since the right choice depends on where publishing comes from (admin panel, API, scripts).

Why Strapi v5 shows Internal Server Error on publish

In v4 you validated in lifecycle hooks such as beforeCreate. In v5 the document service middleware, registered with strapi.documents.use(), is the closest replacement. People on the Strapi subreddit reported this exact symptom in November 2025: custom validation before publish ends in a 500 with no message. The issue number they point to is strapi/strapi#20343.

Causes, most common first:

  1. The thrown value is not a Strapi error. A plain new Error('...') is treated as an unhandled failure and answered with a 500. This is by design and the most common mistake after a migration.
  2. A bug in your middleware runs before your validation does. A TypeError such as "cannot read properties of undefined" gives the same generic 500, and your check never executed. In v5 this often happens when a lookup returns null, or when the middleware runs for actions and content types you did not plan for.
  3. A known Strapi issue. Reports say that even a correct ValidationError thrown from a document middleware on publish comes back as a 500. We can't tell you the internal cause, and we haven't confirmed the issue's current status. We can't say whether a release has fixed it, so check the issue and the release notes for your version.
  4. Possibly, two copies of @strapi/utils. Strapi recognizes its error classes by checking the thrown object against the class it ships. If your project resolves a second copy of the package, your error may not match and could fall through to a 500. This is a plausible cause we have not confirmed as the reason behind the reports, but it is cheap to rule out.

What you can verify yourself is how Strapi answers a handled error. A recognized validation error normally comes back as a 400 with a JSON body whose error object has status, name and message. A 500 means the error was not handled that way.

How to fix it

Fix 1: Read what was actually thrown

  1. Read the terminal where Strapi is running. The browser hides the real error. If the log shows a TypeError, the bug is in your middleware and your validation never ran.
  2. Open the browser's network tab and select the failing publish request. A 400 with "name": "ValidationError" means Strapi handled your error and the admin shows it poorly. A 500 with a generic body matches the reported issue.
  3. Log and rethrow inside the middleware to see the error at the moment it passes through:
strapi.documents.use(async (context, next) => {
  try {
    return await next();
  } catch (error) {
    strapi.log.error(`[publish check] ${error.name}: ${error.message}`);
    throw error;
  }
});

If the log shows ValidationError with your message, your code is fine and the problem sits further up the chain.

Fix 2: Throw the right class

Import the error classes from @strapi/utils and throw one of those:

const { errors } = require('@strapi/utils');

throw new errors.ValidationError('Add a title before publishing');

Do not throw strings, plain objects or new Error(...). Then run this to check for duplicate copies of the package:

npm ls @strapi/utils

If it lists more than one version, align your dependencies so only the version Strapi itself uses is installed, then reinstall and restart.

Fix 3: Test with a middleware that only throws

Replace your logic with an unconditional throw for the publish action:

const { errors } = require('@strapi/utils');

module.exports = {
  register({ strapi }) {
    strapi.documents.use(async (context, next) => {
      if (context.uid === 'api::article.article' && context.action === 'publish') {
        throw new errors.ValidationError('test');
      }
      return next();
    });
  },
};

Click Publish on an article. If you still get a 500, you have reproduced the reported problem, and your own logic is not the cause. If you get a clean 400 with "test", the bug is in your code, so add your checks back one at a time.

Fix 4: Narrow the middleware and fetch the draft correctly

Check context.uid and context.action so the middleware runs only where you mean it to. One that runs on every document action can fail on actions you never tested.

Also note that in v5 a findOne without a status returns the published version. To validate the draft that is about to be published, ask for it explicitly:

const { errors } = require('@strapi/utils');

module.exports = {
  register({ strapi }) {
    strapi.documents.use(async (context, next) => {
      if (
        context.uid === 'api::article.article' &&
        context.action === 'publish'
      ) {
        const draft = await strapi
          .documents(context.uid)
          .findOne({ documentId: context.params.documentId, status: 'draft' });

        if (!draft?.title) {
          throw new errors.ValidationError('Add a title before publishing');
        }
      }
      return next();
    });
  },
};

Replace api::article.article and title with your content type and field. The middleware must return next() on the happy path, or nothing is published.

Don't swallow the error

Catching your validation error and returning a string or undefined does not block the publish. A returned value is not a thrown error, so the document goes live with no check.

Fix 5: Move the rule out of the middleware

If the throw-only test still returns a 500, stop fighting it and pick another layer. The table compares your options.

Where the rule livesRuns whenMessage in adminCatch
Content type schema (required, min/max length, unique, regex)Create, update and publishYes, field-levelOnly simple rules
Document service middlewareAny document service call, including scriptsReported as a 500 on publishThe issue in this article
Custom or wrapped core controllerRequests through your API routesYes, on API responsesAdmin Publish does not use your API controllers
Route policyRequests through your API routesYes, on API responsesSame limit as controllers
Database lifecycle hooksDatabase operationsDepends on the flowBehavior differs from v4 around publish, so test it
Your own admin-side check (a custom field, or a review step in your team's process)Before a human clicks PublishWherever you put itNot enforced by the server

The practical reading of the table:

  • Schema rules first. Required fields, lengths, unique fields and regex patterns in the content type are enforced by Strapi itself and return proper messages. Use code only for rules the schema can't express, such as "a cover image is required if the category is Feature".
  • Controller or policy for API traffic. If your content comes in through API routes (a frontend form, an import script), a controller can check and throw:
const { factories } = require('@strapi/strapi');
const { errors } = require('@strapi/utils');

module.exports = factories.createCoreController('api::article.article', () => ({
  async create(ctx) {
    if (!ctx.request.body?.data?.title) {
      throw new errors.ValidationError('title is required');
    }
    return super.create(ctx);
  },
}));
  • Keep the middleware for server-side guarantees. It sits closest to the data, so it is the right place for rules that must hold for every caller, even if the admin shows a poor message. If your editors are the main publishers, test that the 500 in the admin is acceptable, or pair it with a schema rule that gives a readable message.
  • Lifecycle hooks are not a drop-in. v5 still has database lifecycles, but publishing in v5 works differently from v4's published-state flag, so events may not fire the way your old hook expected. Test before you rely on them.

Admin Publish takes its own route

The admin panel's Publish button goes through the Content Manager's admin routes, not your API controllers. A controller check will not stop a publish clicked in the admin, so test that button separately.

Fix 6: Watch the issue

Follow strapi/strapi#20343 on GitHub and read the release notes for your version in Strapi's documentation. After every upgrade, repeat Fix 3 with a deliberately failing publish. If you have a minimal reproduction (a fresh project plus the throw-only middleware), add it to the issue, since maintainers act on those.

What won't help

  • Switching between ApplicationError and ValidationError. Reports say both produce the same 500, so the class you pick is not what breaks.
  • Wrapping next() in try/catch and returning a message. It hides the failure from the log and lets the publish proceed.
  • Restarting or clearing the admin build. The response is generated on the server, so a rebuilt admin shows the same thing.
  • Throwing from a register() call directly. The middleware only runs per request. An error at registration time stops Strapi from starting, which is a different problem.

Checklist before you try again

  • The Strapi log shows what was thrown, and it is not a TypeError.
  • You throw errors.ValidationError (or ApplicationError) imported from @strapi/utils.
  • npm ls @strapi/utils lists one version.
  • The middleware checks context.uid and context.action.
  • Draft lookups use status: 'draft'.
  • The middleware returns next() when the data is valid.
  • The throw-only test from Fix 3 gives either a 400 (your code is the bug) or a 500 (reported issue).

If nothing works

Contact Strapi through its GitHub issue tracker (comment on strapi/strapi#20343 if your reproduction matches, otherwise open a new issue) or through the support options listed on strapi.io if you have a paid plan. Include:

  • Your exact Strapi version and Node.js version.
  • The exact error text and the full response body from the network tab.
  • The server log lines around the failing publish, with timestamps.
  • The middleware code, cut down to the smallest version that still fails.
  • What you already tried (throw-only test, error class, npm ls @strapi/utils).

A message you can adapt:

Subject: Document service middleware: ValidationError on publish returns 500

Version: Strapi 5.x.x, Node 20.x, database: <your database>

I register a middleware with strapi.documents.use() that throws
errors.ValidationError('test') when context.action === 'publish'
(imported from @strapi/utils, only one copy installed).

Expected: 400 response with name "ValidationError" and my message.
Actual: 500 "Internal Server Error", no message in the admin panel.

Server log at <timestamp>: <paste the log lines>
Response body from the network tab: <paste>
Minimal reproduction: <repo link or code>

This looks related to strapi/strapi#20343. Is there a supported way to
return a validation message when publishing from the admin?

Common questions

Does this also happen on create and update?

The thread we found is about publish. For your version, throw a test ValidationError for each action and compare the responses, since behavior can differ per action.

Can I see the real error without changing code?

Yes. Trigger the failing publish and read the Strapi log, then compare it with the response body in the network tab. If you run Strapi as a service, read that service's log output instead of the terminal.

Is this fixed in the latest Strapi 5?

We can't confirm that. Our evidence is a November 2025 thread and the issue number, so check the issue's current state and your version's release notes, then run the throw-only test.

Should I use a middleware or a controller for validation?

Use a controller or policy when data arrives through your API, and the schema for simple rules. Use the middleware for rules that must hold for every caller, and accept that the admin message may be poor until the reported issue is resolved on your version.