Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Thowing "Unauthorised" error as plain text #3

@iit2011081

Description

@iit2011081

When I am not passing token or passing invalid token while accessing protected url, it is giving me 401 error with "Unauthorised" message as plain text. I want that it should show error message as json.

app.use(async (ctx, next) => {
  try {
    await next();
  } catch (err) {
    logerr(err);
    ctx.status = err.status || err.code;
    ctx.body = {
      status: false,
      message: err.message,
    };
  }
});


It is not getting catched in the above function, but it is getting captured in below function.

app.on('error', (err, ctx) => {
  logerr('onerr ', err);
  if (err.name === "UnauthorizedError") {
      ctx.status = 401;
      ctx.body = "invalid token...";
   }
});

But it is not showing me "Invalid token" message but it is throwing "Unauthorised" error as plain text.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions