# @jetti/jetti.problem

> Error handling for Jetti

Latest version **0.1.12** (published 2021-03-08) · MIT license · 0 weekly downloads

## Install

```sh
npm install @jetti/jetti.problem
pnpm add @jetti/jetti.problem
yarn add @jetti/jetti.problem
bun add @jetti/jetti.problem
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.12 |
| Published | 2021-03-08 |
| First published | 2020-12-08 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 14 |
| Unpacked size | 48.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Olle Lauri Boström |
| Maintainers | jetti |

## Links

- npm: https://www.npmjs.com/package/@jetti/jetti.problem
- Repository: https://github.com/ollelauribostrom/helloworld-es6
- Homepage: https://github.com/ollelauribostrom/helloworld-es6#readme
- Issues: https://github.com/ollelauribostrom/helloworld-es6/issues
- npm.io page: https://npm.io/package/@jetti/jetti.problem

## Dependencies (14)

- [dotenv](https://npm.io/package/dotenv.md) ^8.2.0
- [lodash](https://npm.io/package/lodash.md) ^4.17.20
- [jsonfile](https://npm.io/package/jsonfile.md) ^6.1.0
- [read-yaml](https://npm.io/package/read-yaml.md) ^1.1.0
- [contentful](https://npm.io/package/contentful.md) ^8.1.1
- [lower-case](https://npm.io/package/lower-case.md) ^2.0.2
- [api-problem](https://npm.io/package/api-problem.md) ^6.1.4
- [caller-callsite](https://npm.io/package/caller-callsite.md) ^4.1.0
- [decamelize-keys](https://npm.io/package/decamelize-keys.md) ^1.1.0
- [strip-extension](https://npm.io/package/strip-extension.md) ^1.1.0
- [underscore.string](https://npm.io/package/underscore.string.md) ^3.3.5
- [@jetti/jetti.logger](https://npm.io/package/@jetti/jetti.logger.md) ^1.0.0
- [underscore.inflections](https://npm.io/package/underscore.inflections.md) ^0.2.1
- [@babel/plugin-proposal-decorators](https://npm.io/package/@babel/plugin-proposal-decorators.md) 7.4.4

## Recent versions

- 0.1.12 (latest) — 2021-03-08
- 0.1.11 — 2021-02-16
- 0.1.10 — 2021-02-15
- 0.1.9 — 2021-02-07
- 0.1.8 — 2021-02-05
- 0.1.7 — 2021-02-05
- 0.1.6 — 2021-02-04
- 0.1.5 — 2021-02-02
- 0.1.4 — 2021-02-02
- 0.1.3 — 2021-01-28
- 0.1.2 — 2021-01-26
- 0.1.1 — 2021-01-25
- 0.0.12 — 2021-01-24
- 0.0.10 — 2021-01-15
- 0.0.9 — 2021-01-06
- … 5 more at https://npm.io/package/@jetti/jetti.problem/versions

## README

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:

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

For errors related to Sequelize models:

```js
    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:

```js
    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.

```js
    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.

```js
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.

```json
{
	"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",

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

---
_Source: https://npm.io/package/@jetti/jetti.problem · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
