strapi/docs/3.0.0-beta.x/guides/error-handling.md
2019-10-23 14:36:51 +02:00

1.7 KiB

Error handling

In this guide we will see how you can handle errors to send it on the Application Monitoring / Error Tracking Software you want.

::: note In this example we will use Sentry. :::

Create a middleware

To handle errors, we will have to use a middleware that will catch errors and send them to Sentry.

  • Create a ./middlewares/sentry/index.js file.

Path — ./middlewares/sentry/index.js

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

Handle errors

Here is the Node.js client documentation

  • Now add the logic that will catch errors.

Path — ./middlewares/sentry/index.js

var Raven = require('raven');
Raven.config('https://<key>@sentry.io/<project>').install();

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

::: warning It's important to throw(error); to not stop the middleware stack. If you don't do that, Boom will not structure errors messages. :::

Configure the middleware

You will have to order this middleware at the end of the middleware stack.

Path — ./config/middleware.json

{
  ...
    "after": [
      "parser",
      "router",
      "sentry"
    ]
  }
}

And fianlly you have to enable the middleware.

Path — ./config/environments/**/middleware.json

{
  "sentry": {
    "enabled": true
  }
}