Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to catch and save all errors errors in strapi? #4071

Closed
shankara403 opened this issue Sep 22, 2019 · 6 comments
Closed

How to catch and save all errors errors in strapi? #4071

shankara403 opened this issue Sep 22, 2019 · 6 comments

Comments

@shankara403
Copy link

I'm trying to integrate my strapi application with sentry, for which I will need to write a middleware. Use the following documentation: https://strapi.io/documentation/3.0.0-beta.x/advanced/middlewares.html I was able to create a custom middleware with the following:

module.exports = strapi => {
  return {
    initialize() {
      strapi.app.use(async (ctx, next) => {
        try {
          await next();
        } catch (error) {
          Sentry.captureException(error)
        }

      });
    }
  };
};

However, doing so is preventing strapi to print out the errors to console the usual way but the error is captured by sentry application.

So, my question is: How do I capture the error "seamlessly" and send it to a third party application, at the same time not hinder with the default functioning and error logging of the strapi to console.

Any help would be greatly appreciated!

Thanks :)

EDIT: I figured out that all strapi errors are accessible at the "boom" middleware as pointed out in this file:

,but how do I write a separate custom add-on middleware without touching this file?

@lauriejim
Copy link
Contributor

Hello @saikrishnadeep !

You will have to throw your error to continue in the middleware stack.
And that will format the error with boom.

module.exports = strapi => {
  return {
    initialize() {
      strapi.app.use(async (ctx, next) => {
        try {
          await next();
        } catch (error) {
          Sentry.captureException(error);
          throw(error);
        }
      });
    }
  };
};

@leonardyhuang
Copy link

Hi, what is the sequence of the middlewares? Is this correct (see errorReporting)?

{
  "timeout": 100,
  "load": {
    "before": [
      "responseTime",
      "logger",
      "cors",
      "responses",
      "gzip"
    ],
    "order": [
      "Define the middlewares' load order by putting their name in this array is the right order"
    ],
    "after": [
      "errorReporting",
      "parser",
      "router"
    ]
  }
}

p.s: Strapi v3.0.0-beta.16.5.

Thanks.

@shankara403
Copy link
Author

@leonardyhuang You gotta but your error reporting at the ending of everything. Something like this:

{
  "timeout": 100,
  "load": {
    "before": [
      "responseTime",
      "logger",
      "cors",
      "responses",
      "gzip"
    ],
    "order": [
      "Define the middlewares' load order by putting their name in this array is the right order"
    ],
    "after": [
      "parser",
      "router",
      "sentry"
    ]
  }
}

@ceefour
Copy link

ceefour commented May 4, 2020

@gregg-cbs
Copy link

It would be nice to know how to do this in Strapi V4

@derrickmehaffy
Copy link
Member

It would be nice to know how to do this in Strapi V4

If you run into issues with the logger not logging something, in the middlewares file move it to the top of the array.

I've been meaning to make a PR to do that by default

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants