Skip to content

Commit

Permalink
Renamed to process-warning (#45)
Browse files Browse the repository at this point in the history
Signed-off-by: Matteo Collina <hello@matteocollina.com>
  • Loading branch information
mcollina authored Dec 28, 2021
1 parent 648c6a6 commit 91a9886
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 21 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2020 Fastify
Copyright (c) Fastify

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
27 changes: 15 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,24 +1,27 @@
# fastify-warning
# process-warning

![CI](https://github.com/fastify/fastify-warning/workflows/CI/badge.svg)
[![NPM version](https://img.shields.io/npm/v/fastify-warning.svg?style=flat)](https://www.npmjs.com/package/fastify-warning)
[![Known Vulnerabilities](https://snyk.io/test/github/fastify/fastify-warning/badge.svg)](https://snyk.io/test/github/fastify/fastify-warning)
![CI](https://github.com/fastify/process-warning/workflows/CI/badge.svg)
[![NPM version](https://img.shields.io/npm/v/process-warning.svg?style=flat)](https://www.npmjs.com/package/process-warning)
[![Known Vulnerabilities](https://snyk.io/test/github/fastify/process-warning/badge.svg)](https://snyk.io/test/github/fastify/process-warning)
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](https://standardjs.com/)

A small utility, used by Fastify itself, for generating consistent warning objects across your codebase and plugins.
A small utility for generating consistent warning objects across your codebase.
It also exposes a utility for emitting those warnings, guaranteeing that they are issued only once.

This module is used by the [Fastify](https://fastify.io) framework and it was called `fastify-warning` prior to version 1.0.0.

### Install
t
```
npm i fastify-warning
npm i process-warning
```

### Usage

The module exports a builder function that returns a utility for creating warnings and emitting them.

```js
const warning = require('fastify-warning')()
const warning = require('process-warning')()
```

#### Methods
Expand All @@ -27,8 +30,8 @@ const warning = require('fastify-warning')()
warning.create(name, code, message)
```

- `name` (`string`, required) - The error name, you can access it later with `error.name`. For consistency, we recommend prefixing plugin error names with `FastifWarning{YourPluginName}`
- `code` (`string`, required) - The warning code, you can access it later with `error.code`. For consistency, we recommend prefixing plugin error codes with `FST_{YourPluginName}_`. NOTE: codes should be all uppercase.
- `name` (`string`, required) - The error name, you can access it later with `error.name`. For consistency, we recommend prefixing module error names with `{YourModule}Warning`
- `code` (`string`, required) - The warning code, you can access it later with `error.code`. For consistency, we recommend prefixing plugin error codes with `{ThreeLetterModuleName}_`, e.g. `FST_`. NOTE: codes should be all uppercase.
- `message` (`string`, required) - The warning message. You can also use interpolated strings for formatting the message.

The utility also contains an `emit` function that you can use for emitting the warnings you have previously created by passing their respective code. A warning is guaranteed to be emitted only once.
Expand All @@ -41,21 +44,21 @@ warning.emit(code [, a [, b [, c]]])
- `[, a [, b [, c]]]` (`any`, optional) - Parameters for string interpolation.

```js
const warning = require('fastify-warning')()
const warning = require('process-warning')()
warning.create('FastifyWarning', 'FST_ERROR_CODE', 'message')
warning.emit('FST_ERROR_CODE')
```

How to use an interpolated string:
```js
const warning = require('fastify-warning')()
const warning = require('process-warning')()
warning.create('FastifyWarning', 'FST_ERROR_CODE', 'Hello %s')
warning.emit('FST_ERROR_CODE', 'world')
```

The module also exports an `warning.emitted` [Map](https://developer.mozilla.org/it/docs/Web/JavaScript/Reference/Global_Objects/Map), which contains all the warnings already emitted. Useful for testing.
```js
const warning = require('fastify-warning')()
const warning = require('process-warning')()
warning.create('FastifyWarning', 'FST_ERROR_CODE', 'Hello %s')
console.log(warning.emitted.get('FST_ERROR_CODE')) // false
warning.emit('FST_ERROR_CODE', 'world')
Expand Down
6 changes: 3 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ function build () {
const emitted = new Map()

function create (name, code, message) {
if (!name) throw new Error('Fastify warning name must not be empty')
if (!code) throw new Error('Fastify warning code must not be empty')
if (!message) throw new Error('Fastify warning message must not be empty')
if (!name) throw new Error('Warning name must not be empty')
if (!code) throw new Error('Warning code must not be empty')
if (!message) throw new Error('Warning message must not be empty')

code = code.toUpperCase()

Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "fastify-warning",
"name": "process-warning",
"version": "0.2.0",
"description": "A small utility for creating warnings and emitting them.",
"main": "index.js",
Expand All @@ -9,7 +9,7 @@
},
"repository": {
"type": "git",
"url": "git+https://github.com/fastify/fastify-warning.git"
"url": "git+https://github.com/fastify/processs-warning.git"
},
"keywords": [
"fastify",
Expand Down
6 changes: 3 additions & 3 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ test('Should throw when error code has no fastify name', t => {
try {
create()
} catch (err) {
t.is(err.message, 'Fastify warning name must not be empty')
t.is(err.message, 'Warning name must not be empty')
}
})

Expand All @@ -55,7 +55,7 @@ test('Should throw when error has no code', t => {
try {
create('name')
} catch (err) {
t.is(err.message, 'Fastify warning code must not be empty')
t.is(err.message, 'Warning code must not be empty')
}
})

Expand All @@ -64,7 +64,7 @@ test('Should throw when error has no message', t => {
try {
create('name', 'code')
} catch (err) {
t.is(err.message, 'Fastify warning message must not be empty')
t.is(err.message, 'Warning message must not be empty')
}
})

Expand Down

0 comments on commit 91a9886

Please sign in to comment.