# express-gateway-plugin-errors

> An express gateway plugin and policy for app error formatting and management

Latest version **1.0.2** (published 2019-10-10) · MIT license · 0 weekly downloads

## Install

```sh
npm install express-gateway-plugin-errors
pnpm add express-gateway-plugin-errors
yarn add express-gateway-plugin-errors
bun add express-gateway-plugin-errors
```

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.2 |
| Published | 2019-10-10 |
| First published | 2019-09-25 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 15.7 KB |
| Known vulnerabilities | 0 (+25 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 0 |
| Author | josemf |
| Maintainers | josemf |
| Keywords | express-gateway, policy, errors, json, formatting, reporting |

## Links

- npm: https://www.npmjs.com/package/express-gateway-plugin-errors
- Repository: https://github.com/josemf/express-gateway-plugin-errors
- Homepage: https://github.com/josemf/express-gateway-plugin-errors#readme
- Issues: https://github.com/josemf/express-gateway-plugin-errors/issues
- npm.io page: https://npm.io/package/express-gateway-plugin-errors

## Dependencies (2)

- [axios](https://npm.io/package/axios.md) ^0.19.0
- [lodash](https://npm.io/package/lodash.md) ^4.17.15

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 1.0.2 (latest) — 2019-10-10
- 1.0.1 — 2019-10-08
- 1.0.0 — 2019-09-25

## README

# express-gateway-plugin-errors
An express gateway plugin and policy for error formatting and handling.

## Why

This plugin serves three different app development requirements that occur very often in API development and composition:

* It's required every response from the gateway is in JSON format.
* We need to format errors in a unified way, even if we're composing from different sets of API backends, that report errors in different formats.
* Error messages should be enforced by the backend—in this scenario the app is dumb and just notifies whatever error message the API gateway sends.


## Installation

Type from your shell environment:

```bash
eg plugin install express-gateway-plugin-errors
```

A PR is pending approval in `express-gateway` that is required to enable all features for this plugin. So until it's approved you can install a custom `express-gateway` from my repo fork: https://github.com/josemf/express-gateway.

## Quick start

1. Make sure the plugin is listed in [system.config.yml file](https://www.express-gateway.io/docs/configuration/system.config.yml/).
This is done automatically for you if you used the command above.

2. Add error handling configuration to [gateway.config.yml file](https://www.express-gateway.io/docs/configuration/gateway.config.yml/).

```yaml
- errors:
  - action:
    template: '{ "status": "$status", "message": "$message" }'
    defaults:
      status: NOK
      message: Internal Server Error
    messages:
      Internal Server Error: Oops... Please try again
      Validation Error: Some fields have validation errors
```

3. Enable `selfHandleResponse` in **proxy** configuration to pass through backend responses—makes it possible handle error responses.

```yaml
- proxy:
  - action:
    serviceEndpoint: your-service-endpoint
    changeOrigin: true
    selfHandleResponse: true
```

### Configuration Parameters

By default if no parameter have been declared, the plugin will make sure every response from the backend is in JSON. By itself this might be useful if the app was built in a way that expects every response to be JSON.

Express gateway outputs most of its internal errors in `text/plain`. For example if we setup `key-auth` authorization and the app sends an invalid key token, `express-gateway` will respond with:

```
Unauthorized
```

Just by enabling the policy in some policy list will make `express-gateway` to respond with:

```json
{
  "status": "error",
  "message": "Unauthorized"
}
```

#### template:

This makes possible to customize the response JSON structure for errors. Example:

```yaml
- errors:
  - action:
    template: '{ "success": "$status", "message": "$message", "error": "$code", "exception": "$trace", "file": "$file", "line": "$line" }'
```

In this example it is expected proxied backend to return something errors in the following structure:

```json
{
  "status": false,
  "message": "Something went wrong...",
  "code": "internal_server_error",
  "trace": [ ... ],
  "file": "some_file_path",
  "line": 89
}
```

Having the template rule applied would result in this response:

```json
{
  "success": false,
  "message": "Something went wrong...",
  "error": "internal_server_error",
  "exception": [ ... ],
  "file": "some_file_path",
  "line": 89
}
```

#### defaults:

Some times it's possible the proxied backend is not sending some field in the template structure. For those cases we setup defaults:

```yaml
defaults:
  success: false
  error: 500_error
  message: Oopsie
```

#### messageField:

We always need a JSON `message` field. For example if the proxied backend sends `text/plain` we should know the field name to fill with its response content.

```yaml
messageField: message
```

#### debugFields:

Some response fields can be showned or filtered out depending on env `LOG_LEVEL=debug`:

```yaml
debugFields:
  - exception
  - file
  - line
```

#### restrictErrors:

Some times we don't control the proxied service backend, and we should not trust error messages it sends:

```yaml
restrictErrors: true
```

Only listed messages in the `messages:` option dictionary will be sent through, if it's not found there a default message in `defaults:` will be sent.

#### messages:

This will map error messages received from the proxied backend to the ones we want to send to our app:

```yaml
 messages:
   Internal Server Error: Oops... Please try again
   Validation Error: Some fields have validation errors
   Unauthorized: Please login again...
```

#### filters:

This is a more sophisticated way to map messages between error responses and what we want to send to apps, using regular expressions:

```yaml
filters:
  -
    regex: ^Found an error in (\s+) module
    error: "module_error"
    message: "Module $1 error"
```

It will do regex capture and substitution with `$N` format.

## What is missing

There are a few other things that depending on specific requirement needs I might have on the future or the demand this plugin might attract that I'm willing to pursue:

* Error reporting to external proxied service endpoint.
* Converting errors to other formats.

Ofcourse any contribution is appreciated!

---
_Source: https://npm.io/package/express-gateway-plugin-errors · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
