# platform-error

> - PlatformError is a Node.js library that allows you to easily throw HTTP errors that can be serialized and sent in server responses. - This library is useful for creating error objects that can be sent as JSON responses to client-side applications. - U

Latest version **1.0.9** (published 2023-08-06) · ISC license · 0 weekly downloads

## Install

```sh
npm install platform-error
pnpm add platform-error
yarn add platform-error
bun add platform-error
```

## Health

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

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.9 |
| Published | 2023-08-06 |
| First published | 2023-03-18 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 98.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Maintainers | biswasray |
| Keywords | node, error, custom error |

## Links

- npm: https://www.npmjs.com/package/platform-error
- Repository: https://github.com/biswasray/platform-error
- Homepage: https://github.com/biswasray/platform-error#readme
- Issues: https://github.com/biswasray/platform-error/issues
- npm.io page: https://npm.io/package/platform-error

## Alternatives

- [@sentry/react-native](https://npm.io/package/@sentry/react-native.md) — 2.6M weekly downloads
- [@ardatan/aggregate-error](https://npm.io/package/@ardatan/aggregate-error.md) — 708.1K weekly downloads
- [custom-error-generator](https://npm.io/package/custom-error-generator.md) — 2.0K weekly downloads
- [@technik-sde/prosemirror-recreate-transform](https://npm.io/package/@technik-sde/prosemirror-recreate-transform.md) — 1.5K weekly downloads
- [@suchipi/error-utils](https://npm.io/package/@suchipi/error-utils.md) — 78 weekly downloads

## Recent versions

- 1.0.9 (latest) — 2023-08-06
- 1.0.8 — 2023-08-06
- 1.0.7 — 2023-05-08
- 1.0.6 — 2023-04-18
- 1.0.5 — 2023-04-18
- 1.0.4 — 2023-04-18
- 1.0.3 — 2023-03-21
- 1.0.2 — 2023-03-20
- 1.0.1 — 2023-03-20
- 1.0.0 — 2023-03-18

## README

# NodeJs Server Error Handler

- PlatformError is a Node.js library that allows you to easily throw HTTP errors that can be serialized and sent in server responses.
- This library is useful for creating error objects that can be sent as JSON responses to client-side applications.
- User can also throw Custom error with custom messages.

## Installation

To install , use NPM:

```bash
npm install platform-error
```

## Usage

To create a platform error, import the PlatformError class and create an error object to throw.

```javascript
import PlatformError from "platform-error";

throw new PlatformError("Bad Request", { messages: "Query limit must be numeric" });
```

To throw error as server response user need `express-async-errors`

### Following these steps for server response

- Install "express-async-errors" , and import on server file

```bash
npm install express-async-errors --save
```

```javascript
import "express-async-errors";
```

- Import `ExpressErrorHandler` middleware and use it at last of routing.

```javascript
import { ExpressErrorHandler } from "platform-error";

const app = express();
...
...
...
app.use(ExpressErrorHandler());
// If you want log error before response
app.use(ExpressErrorHandler(true));
// If you have custom logger function
app.use(ExpressErrorHandler(logger));

app.listen(process.env.PORT,()=> {
    console.log(`Server running on ${process.env.PORT}....`)
});

```

- Create and throw error

```javascript
app.post('/user/:id', (request: Request,response: Response)=> {
    ...
    ...
    throw new PlatformError("Unauthorized", { messages: "You are not authorized to access this resource" });
});

app.post('/user/signin', (request: Request,response: Response)=> {
    ...
    ...
    throw new PlatformError("Not Found", { messages: "Username does not exist", resource: "User" });
});

app.post('/user/signup', (request: Request,response: Response)=> {
    ...
    ...
    throw new PlatformError("Bad Request", { messages: ["Invalid Email","Password must contain alphanumerics"] });
});
```

- User can throw custom error.

```javascript
app.get('/user', (request: Request,response: Response)=> {
    ...
    ...
    throw new CustomError(500, ["Something is wrong!"]);
});
```

## License

All code in this repository is released under the terms of the ISC license.

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