Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@beforeyoubid/error-adapter

Package Overview
Dependencies
Maintainers
8
Versions
36
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@beforeyoubid/error-adapter

A module to standardize error handling across the BYB platform

  • 1.0.12
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
268
increased by15.52%
Maintainers
8
Weekly downloads
 
Created
Source

BYB Error Adapter

This is an error handling module that supports multiple error types and the handling of each type accordingly using Sentry.

The module does NOT set up a Sentry client, therefore a client must already be set up and initialized.

The module provides functions to handle the formatting of GraphQL errors and sending errors to Sentry based on the error type.

Supported Error types

Error TypeAlert Raised in Sentry
Server ErrorYes
Not AuthorizedYes
Not AuthenticatedNo
Not FoundNo
Validation ErrorNo
Payment ErrorNo
User Input ErrorNo
Conflict ErrorNo

Setup

Installation

  yarn add @beforeyoubid/error-adapter

Usages

formatError

Using formatError function to format errors in preparation for Sentry by passing formatError function into GraphQL handler.

import { ApolloServer } from 'apollo-server-lambda';
import withSentry from 'serverless-sentry-lib';
import schema from '../graphql';
import { formatError } from '@beforeyoubid/error-adapter';

const server = new ApolloServer({
  schema,
  formatError,
  context: async ({ event, context }): Promise<ApplicationContext> => {
    const headers = {};
    let gqcontext = {};
    if (event.headers) {
      const sourceUserAgent =
        _.get(event, 'headers.x-source-user-agent') || _.get(event, 'headers.X-Source-User-Agent');

      gqcontext = {
        sourceUserAgent,
      };
    }
    return {
      // cache,
      functionName: context.functionName,
      headers,
      ...gqcontext,
    };
  },
});

const graphqlHandler = server.createHandler({
  cors: {
    origin: '*',
    methods: ['POST'],
  },
});

export default withSentry(graphqlHandler);

handleErrorSentryOptions

Using with GraphQL

Using handleErrorSentryOptions function to send errors to Sentry by passing handleErrorSentryOptions function into lambda handler.

import { ApolloServer } from 'apollo-server-lambda';
import withSentry from 'serverless-sentry-lib';
import schema from '../graphql';
import { formatError, handleErrorSentryOptions } from '@beforeyoubid/error-adapter';

const server = new ApolloServer({
  schema,
  formatError,
  context: async ({ event, context }): Promise<ApplicationContext> => {
    const headers = {};
    let gqcontext = {};
    if (event.headers) {
      const sourceUserAgent =
        _.get(event, 'headers.x-source-user-agent') || _.get(event, 'headers.X-Source-User-Agent');

      gqcontext = {
        sourceUserAgent,
      };
    }
    return {
      // cache,
      functionName: context.functionName,
      headers,
      ...gqcontext,
    };
  },
});

const graphqlHandler = server.createHandler({
  cors: {
    origin: '*',
    methods: ['POST'],
  },
});

export default withSentry(handleErrorSentryOptions, graphqlHandler);

Using without GraphQL

import withSentry from 'serverless-sentry-lib';
import { handleErrorSentryOptions, NotFound } from '@beforeyoubid/error-adapter';

export const cronHandler = withSentry(handleErrorSentryOptions, async (event, context) => {
  console.log('EVENT: \n' + JSON.stringify(event, null, 2));
  throw new Error('This error will be raised in Sentry');
  throw new NotFound('This error will NOT be raised in Sentry');
  return context.logStreamName;
});

Roadmap

  • Extend this module to support centralised copy on error messages (useful for business users seeing the error, as well as developers investigating the error).

FAQs

Package last updated on 20 Oct 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc