npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@zeytech/error-reporting-adonisjs

v1.0.3

Published

Configurable error reporting for Adonisjs applications.

Downloads

3

Readme

@zeytech/error-reporting-adonisjs

An Error reporting module for AdonisJS

npm-image license-image typescript-image

This module provides a pluggable error reporting service for use with Adonis's built-in error reporting feature. One or more drivers can be configured to report errors to various destinations. By default this plugin comes with a Logger driver. Additional drivers can be installed on top to make use of other destinations such as files or network services.

Installation

npm install @zeytech/error-reporting-adonis

node ace configure @zeytech/error-reporting-adonis

Usage

Handler Setup

The plugin comes with an error reporting utility service that can be used in your exception handler's report method, as shown in the example below. Please note the following:

  • The error reporting service is injected into the constructor of the handler. Its type is imported dynamically to avoid resolution errors during startup.
  • The Handler's report function calls the error reporting service's report function.
import Logger from '@ioc:Adonis/Core/Logger'
import HttpExceptionHandler from '@ioc:Adonis/Core/HttpExceptionHandler'
import { inject } from '@adonisjs/core/build/standalone'
import { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'

@inject(['@ioc:Adonis/Addons/Zeytech/ErrorReporting'])
export default class ExceptionHandler extends HttpExceptionHandler {
  constructor (
    private errorReportingService: import('@ioc:Adonis/Addons/Zeytech/ErrorReporting').ErrorReportingServiceContract) {
    super(Logger)
  }

  public report (error: any, ctx: HttpContextContract): void {
    this.errorReportingService.report(error, ctx)
  }
}

Config

With the handler complete, you'll need to configure one or more drivers to actually report to. This is done via the error-reporting.ts config file (installed when configuring the plugin). If multiple drivers are configured, errors will be reported to each driver. In extreme cases this could cause performance problems, so use common sense when deciding where to report to.

The config file contains an array of driver configurations. Each configuration contains the name of the driver and any options the driver expects. See the example file below.

import { ErrorReportingConfig } from '@ioc:Adonis/Addons/Zeytech/ErrorReporting'

const errorReportingConfig: ErrorReportingConfig = {
  /**
   * Drivers to use in error reporting
   */
  drivers: [
    {
      name: 'logger',
      enabled: true,
      options: {},
    },
    {
      name: 'file',
      enabled: true,
      options: {
        path: './logs/errors.out',
        includeTimestamp: true,
      },
    },
  ],
}

export default errorReportingConfig

The example above includes a coniguration for the built in logger driver as well as a fictitious file driver.

Drivers

Logger Driver

The logger driver is very minimal and simply performs a logger.error(err) call to log the error to the built-in logging system. It is mainly provided for example purposes.

Custom Drivers

You can create your own custom driver by implementing the DriverContract contract (interface). A simple example is shown below. Note that these drivers are constructed during AdonisJS's boot sequence, so beware of what dependencies you import directly vs injecting via the constructor.

import { DriverContract } from '@ioc:Adonis/Addons/Zeytech/ErrorReporting'

export class MyDriver implements DriverContract {

  // You can pass in the config if needed so that you'll have access to any options your driver requires.
  constructor(private config: DriverConfig) {}

  public name = 'my-driver' // the name the driver is registered under in your app(s)

  public async report(error: Error, _ctx: HttpContextContract): Promise<void> {
    // handle the error message as needed to report it
  }
}

Once created, you'll need to register your new driver during AdonisJS's boot sequence. To do so, make use of the extend function included in the error reporting service.

import {
  ErrorReportingServiceContract,
  ExtendDriverCallback,
} from '@ioc:Adonis/Addons/Zeytech/ErrorReporting'

export default class AppProvider {
  constructor(protected application: ApplicationContract) {}
  //...

  public async register() {
    const ErrorReportingService: ErrorReportingServiceContract =
      this.application.container.resolveBinding('@ioc:Adonis/Addons/Zeytech/ErrorReporting')
    const { MyDriver } = await import('../path/to/MyDriver')
    const callback: ExtendDriverCallback = async (config: DriverConfig) => {
      return new MyDriver(config)
    }
    ErrorReportingService.extend('my-driver', callback)
  }

  //...
}

With the driver registered, then add your configuration as shown in Config above.