Strapi backend create custom error message auth login

System Information
  • Strapi Version: 4.7.1
  • Operating System: linux ubuntu 22.04
  • Database: PostgreSQL
  • Node Version: v16.18.0
  • NPM Version: 8.19.2

Hi I need to create custom error message on auth login endpoint. My solution is to create
middleware such like

// path to my file: src/middlewares/badRequestError.js
module.exports = () => {
  return async (ctx, next) => { 
    if (ctx.request.url === "/api/auth/local" && ctx.response.status === 400) {
      return ctx.badRequest("Email or password - invalid. Please try again");
    }
    await next();
  };
};

And Also I set up my middleware to file config/middleware.ts
module.exports = [
  "strapi::errors",
  "strapi::security",
  "strapi::cors",
  "strapi::poweredBy",
  "strapi::logger",
  "strapi::query",
  "strapi::body",
  "strapi::session",
  "strapi::favicon",
  "strapi::public",
  "global::badRequestErorr",
  {
    resolve: "../src/middlewares/badRequestErorr.js",
    config: {},
  },
];

But when I write invalid credentails it always give me default error text (“Invalid identifier or password”)
and ctx.response.status is always 404 instead of 400. How can I catch right error and rewrite text inside of if ?

try this

module.exports = () => {
return async (ctx, next) => {
try{
await next();
}catch{
if (ctx.request.url === “/api/auth/local” && ctx.response.status === 404) {
return ctx.badRequest(“Email or password - invalid. Please try again”);
};
}
};
};