Displaying custom error message in Admin UI in Strapi 4.8.x

System Information
  • Strapi Version: 4.8.2

Hello,

in Strapi v4.7.x I could display a custom error message in the Admin UI by throwing an ApplicationError:

throw new utils.errors.ApplicationError(`Custom error message`);

In Strapi v4.8.2 this doesn’t work anymore. The above code results in a generic error message (e.g. “Warning: Request failed with status code 400”).

In the response I can still see the custom error message:

error: {
    status:		400
    name:		"ApplicationError"
    message:	"Custom error message"
    details:	{ ... }
}

How can I display this custom error message in Strapi v4.8.?

Thank and best regards

1 Like

Nvm, it was fixed in Strapi v4.8.3 right away: useAPIErrorHandler: Reverse error handling (API -> Axios) by gu-stav · Pull Request #16162 · strapi/strapi · GitHub

Problem is here again. Just upgraded to latest Strapi v4.10.6.

package.json

    "@strapi/strapi": "4.10.6",
    "@strapi/plugin-users-permissions": "4.10.6",
    "@strapi/provider-email-sendgrid": "4.10.6",
    "@strapi/provider-upload-cloudinary": "4.10.6"

Code:
throw new utils.errors.ApplicationError("Custom error message");

Network shows:

{
"data":null,
"error":{
 "status":400,
 "name":"ApplicationError",
 "message":"Custom error message",
 "details":{}
 }
}

But UI shows: “Warning: Request failed with status code 400” 🥲

@Mehoff are you able to fix this error?

I got the solution-
just install the @strapi/utils package and then its working fine

@Abhinegi2
I just tried it with v4.11.1 after installing @strapi/utils package but it is not working for me. In the Admin UI it shows internal server error.

I’m on v4.13.1 and its still not working. Would be happy if this gets fixed. @DMehaffy any news on that maybe?

1 Like

I’m on version 4.11.1 and it is still not working for me.

    "@strapi/plugin-i18n": "^4.11.1",
    "@strapi/plugin-users-permissions": "^4.11.1",
    "@strapi/strapi": "^4.11.1",
    "@strapi/utils": "^4.11.1",

I set up a new project yesterday on v4.17.1 and it doesn’t seem to display any errors in the Admin UI at all, no matter what I throw. They all just get silently ignored.

i am using strapi 4.15 and below code works for me and even if we mention filed name in path key it also highlight field in admin ui form

const { errors } = require("@strapi/utils");
const { ApplicationError } = errors;      
throw new ApplicationError("Title already exist", {
        errors: [
          {
            path: ["title"],
            message: "This attribute must be unique",
            name: "ValidationError",
          },
        ],
      });
1 Like

Hope it hepls someone)

  module.exports = {
    beforeCreate: async (event) => {
      console.log(event);

      const ctx = strapi.requestContext.get();

      ctx.throw(400, {
        details: {
          errors: [
            {
              path: ["slug"],
              message: "This attribute must be unique",
              name: "ValidationError",
            },
          ],
        },
        message: "This attribute must be unique",
        name: "ValidationError"
      });
      return ctx;
    },
    beforeUpdate: async (event) => {

    },
  };

@jangxx @Tirth_Work @0x273464

2 Likes

This one actually works flawlessly in my case (strapi 4.15.5).

Thank you a lot

Works with 4.20.5)