Socket
Socket
Sign inDemoInstall

@jetti/jetti.problem

Package Overview
Dependencies
113
Maintainers
1
Versions
20
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

@jetti/jetti.problem

Error handling for Jetti


Version published
Maintainers
1
Weekly downloads
5,333
increased by70.33%

Weekly downloads

Readme

Source

Library for propagating 7807 errors, based on api-problem (https://github.com/ahmadnassri/node-api-problem). You'll need to import into the repository you want to trigger the errors form. This should replace all throw new Error() calls with the following code:

    import { ApiProblem } from '@jetti/jetti.problem';
    throw new ApiProblem({
        code: 'api.channels.process_sale.missing',
        details: {
            externalId,
        },
    });

For errors related to Sequelize models:

    import { InstanceProblem } from '@jetti/jetti.problem';
    const instanceError = new InstanceProblem({
        instance,
        action: 'export',
        // or module.filename,
        // Optional
        content: 'empty,'
        // Optional
        details: {
            externalId: '1234',
        },
    });

For errors related to integrations:

    import { IntegrationProblem } from '@jetti/jetti.problem';
    const integrationError = new IntegrationProblem({
        integration: 'wooCommerce',
        resource: 'orders',
        action: 'import',
        context: 'no_attributes',
        details: {
            externalId: '1234',
        },
    });

Although not a requirement, you can create a Contentful entry under the error content type, with a corresponding code. This will then merge the title into the error message. The details array should contain any useful details about the error, in alignment with the 7807 spec.

You'll need to add an .env file for the build process:

CONTENTFUL_SPACE={{space}}
CONTENTFUL_TOKEN={{token}}

Decorators

For convenience, it's also possible to wrap class methods to capture any unhandled exceptions. This will coerce any errors into the problem 7807 standard.

    class Postmen {
        // Or CatchApi()
        @CatchIntegration({ resource: 'rates', ...options })
        rates() {
            throw new Error('Something unexpected');
        }
    }

Often, integrations will pass back errors in different formats. For example, have an errors array, or maybe a meta object that lists the errors. It's useful to get these varying error messages and consolidate down to a common format. For a jetti.problem, this will be detailed in the details object. To handle this conversion, the calling / catcher of the problem should pass a parser function that takes a given request response and allows mapping map to a common format.

const throwMe = {
    variant: {
        message: 'not found!',
    },
};

const parser = ({ response: toPrase }) => ({
    messages: Object.entries(toPrase).reduce((obj, [code, { message }]) => ({
        [code]: [message],
        ...obj,
    }), {}),
});

@IntegrationCatch({ parser })

This will take a request object, map the data and pass back in a format below. It should return an array of messages, in the format below.

{
	"type": "https://problems.jetti.io/details#integrations.postmen.rates.rates",
	"title": "integrations.postmen.rates.rates",
	"status": 418,
	"details": {
		"message": "Response code 418 (I'm a Teapot)",
		"response": {
			"variant": {
				"message": "not found!"
			}
		},
		"messages": {
			"variant": ["not found!"]
		},
		"integration": "postmen",
		"resource": "rates",
		"context": "rates"
	}
}

If there is no "code" and the integration just returns an array of messages, you should use "all",

    ...
    "messages": {
        "all": ["not found!"]
    },
    ...

FAQs

Last updated on 08 Mar 2021

Did you know?

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc