Skip to content

Commit f091f62

Browse files
committed
feat: Refactored and renamed as fastify-http-errors-enhanced.
1 parent bcdaf40 commit f091f62

18 files changed

+190
-257
lines changed

CHANGELOG.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
### 2020-11-22 / 2.0.0
2+
3+
- feat: Renamed to `fastify-http-errors-enhanced`.
4+
- feat: Dropped all error related depedencies in favor of `http-errors-enhanced`.
5+
- feat: Removed `addAdditionalProperties` and `serializeError` which can now be found in `http-errors-enhanced`.
6+
17
### 2020-09-28 / 1.0.0
28

39
- feat: Updated to fastify 3.x.x.
@@ -49,4 +55,3 @@
4955

5056
- chore: Rebuild.
5157
- feat: Extract errors to another module.
52-

README.md

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
1-
# fastify-errors-properties
1+
# fastify-http-errors-enhanced
22

3-
[![Package Version](https://img.shields.io/npm/v/fastify-errors-properties.svg)](https://npm.im/fastify-errors-properties)
4-
[![Dependency Status](https://img.shields.io/david/ShogunPanda/fastify-errors-properties)](https://david-dm.org/ShogunPanda/fastify-errors-properties)
5-
[![Build](https://github.com/ShogunPanda/fastify-errors-properties/workflows/CI/badge.svg)](https://github.com/ShogunPanda/fastify-errors-properties/actions?query=workflow%3ACI)
6-
[![Code Coverage](https://img.shields.io/codecov/c/gh/ShogunPanda/fastify-errors-properties?token=d0ae1643f35c4c4f9714a357f796d05d)](https://codecov.io/gh/ShogunPanda/fastify-errors-properties)
3+
[![Package Version](https://img.shields.io/npm/v/fastify-http-errors-enhanced.svg)](https://npm.im/fastify-http-errors-enhanced)
4+
[![Dependency Status](https://img.shields.io/david/ShogunPanda/fastify-http-errors-enhanced)](https://david-dm.org/ShogunPanda/fastify-http-errors-enhanced)
5+
[![Build](https://github.com/ShogunPanda/fastify-http-errors-enhanced/workflows/CI/badge.svg)](https://github.com/ShogunPanda/fastify-http-errors-enhanced/actions?query=workflow%3ACI)
6+
[![Code Coverage](https://img.shields.io/codecov/c/gh/ShogunPanda/fastify-http-errors-enhanced?token=d0ae1643f35c4c4f9714a357f796d05d)](https://codecov.io/gh/ShogunPanda/fastify-http-errors-enhanced)
77

8-
A error handling plugin for Fastify that enables additional properties in errors.
8+
A error handling plugin for Fastify that uses enhanced HTTP errors.
99

10-
http://sw.cowtech.it/fastify-errors-properties
10+
http://sw.cowtech.it/fastify-http-errors-enhanced
1111

1212
## Installation
1313

1414
Just run:
1515

1616
```bash
17-
npm install fastify-errors-properties --save
17+
npm install fastify-http-errors-enhanced --save
1818
```
1919

2020
## Usage
@@ -46,10 +46,10 @@ To clarify, take this server as a example:
4646
const server = require('fastify')()
4747
const createError = require('http-errors')
4848

49-
server.register(require('fastify-errors-properties'))
49+
server.register(require('fastify-http-errors-enhanced'))
5050

5151
server.get('/invalid', {
52-
handler: async function(request, reply) {
52+
handler: async function (request, reply) {
5353
throw createError(404, 'You are not supposed to reach this.', { header: { 'X-Req-Id': request.id, id: 123 } })
5454
}
5555
})
@@ -98,10 +98,10 @@ To clarify, take this server as a example:
9898
const server = require('fastify')()
9999
const createError = require('http-errors')
100100

101-
server.register(require('fastify-errors-properties'), { hideUnhandledErrors: false })
101+
server.register(require('fastify-http-errors-enhanced'), { hideUnhandledErrors: false })
102102

103103
server.get('/invalid', {
104-
handler: function(request, reply) {
104+
handler: function (request, reply) {
105105
const error = new Error('This was not supposed to happen.')
106106
error.id = 123
107107
throw error
@@ -159,7 +159,7 @@ Example of a response validation error:
159159
}
160160
```
161161

162-
## Contributing to fastify-errors-properties
162+
## Contributing to fastify-http-errors-enhanced
163163

164164
- Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
165165
- Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.

lib/handlers.js

Lines changed: 13 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,11 @@
11
"use strict";
2-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3-
if (k2 === undefined) k2 = k;
4-
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5-
}) : (function(o, m, k, k2) {
6-
if (k2 === undefined) k2 = k;
7-
o[k2] = m[k];
8-
}));
9-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
10-
Object.defineProperty(o, "default", { enumerable: true, value: v });
11-
}) : function(o, v) {
12-
o["default"] = v;
13-
});
14-
var __importStar = (this && this.__importStar) || function (mod) {
15-
if (mod && mod.__esModule) return mod;
16-
var result = {};
17-
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
18-
__setModuleDefault(result, mod);
19-
return result;
20-
};
21-
var __importDefault = (this && this.__importDefault) || function (mod) {
22-
return (mod && mod.__esModule) ? mod : { "default": mod };
23-
};
242
Object.defineProperty(exports, "__esModule", { value: true });
253
exports.handleErrors = exports.handleValidationError = exports.handleNotFoundError = void 0;
26-
const http_errors_1 = __importStar(require("http-errors"));
27-
const http_status_codes_1 = __importDefault(require("http-status-codes"));
28-
const statuses_1 = __importDefault(require("statuses"));
29-
const properties_1 = require("./properties");
4+
const http_errors_enhanced_1 = require("http-errors-enhanced");
305
const utils_1 = require("./utils");
316
const validation_1 = require("./validation");
327
function handleNotFoundError(request, reply) {
33-
handleErrors(new http_errors_1.NotFound('Not found.'), request, reply);
8+
handleErrors(new http_errors_enhanced_1.NotFoundError('Not found.'), request, reply);
349
}
3510
exports.handleNotFoundError = handleNotFoundError;
3611
function handleValidationError(error, request) {
@@ -40,7 +15,7 @@ function handleValidationError(error, request) {
4015
and fastify throws on first failing section.
4116
*/
4217
const section = error.message.match(/^\w+/)[0];
43-
return http_errors_1.default(http_status_codes_1.default.BAD_REQUEST, 'One or more validations failed trying to process your request.', {
18+
return new http_errors_enhanced_1.BadRequestError('One or more validations failed trying to process your request.', {
4419
failedValidations: validation_1.convertValidationErrors(section, Reflect.get(request, section), error.validation)
4520
});
4621
}
@@ -56,38 +31,37 @@ function handleErrors(error, request, reply) {
5631
}
5732
else if ((_b = request.errorProperties) === null || _b === void 0 ? void 0 : _b.hideUnhandledErrors) {
5833
// It is requested to hide the error, just log it and then create a generic one
59-
request.log.error({ error: properties_1.serializeError(error) });
60-
error = new http_errors_1.InternalServerError('An error occurred trying to process your request.');
34+
request.log.error({ error: http_errors_enhanced_1.serializeError(error) });
35+
error = new http_errors_enhanced_1.InternalServerError('An error occurred trying to process your request.');
6136
}
6237
else {
63-
// Wrap in a http-error, making the stack explicitily available
64-
error = Object.assign(new http_errors_1.InternalServerError(error.message), properties_1.serializeError(error));
38+
// Wrap in a HttpError, making the stack explicitily available
39+
error = new http_errors_enhanced_1.InternalServerError(http_errors_enhanced_1.serializeError(error));
6540
Object.defineProperty(error, 'stack', { enumerable: true });
6641
}
6742
}
6843
else if (code === 'INVALID_CONTENT_TYPE' || code === 'FST_ERR_CTP_INVALID_MEDIA_TYPE') {
69-
error = http_errors_1.default(http_status_codes_1.default.UNSUPPORTED_MEDIA_TYPE, utils_1.upperFirst(validation_1.validationMessagesFormatters.contentType()));
44+
error = new http_errors_enhanced_1.UnsupportedMediaTypeError(utils_1.upperFirst(validation_1.validationMessagesFormatters.contentType()));
7045
}
7146
else if (code === 'FST_ERR_CTP_EMPTY_JSON_BODY') {
72-
error = http_errors_1.default(http_status_codes_1.default.BAD_REQUEST, utils_1.upperFirst(validation_1.validationMessagesFormatters.jsonEmpty()));
47+
error = new http_errors_enhanced_1.BadRequestError(utils_1.upperFirst(validation_1.validationMessagesFormatters.jsonEmpty()));
7348
}
7449
else if (code === 'MALFORMED_JSON' || error.message === 'Invalid JSON' || error.stack.includes('at JSON.parse')) {
75-
error = http_errors_1.default(http_status_codes_1.default.BAD_REQUEST, utils_1.upperFirst(validation_1.validationMessagesFormatters.json()));
50+
error = new http_errors_enhanced_1.BadRequestError(utils_1.upperFirst(validation_1.validationMessagesFormatters.json()));
7651
}
7752
// Get the status code
7853
let { statusCode, headers } = error;
7954
// Code outside HTTP range
8055
if (statusCode < 100 || statusCode > 599) {
81-
statusCode = http_status_codes_1.default.INTERNAL_SERVER_ERROR;
56+
statusCode = http_errors_enhanced_1.INTERNAL_SERVER_ERROR;
8257
}
8358
// Create the body
8459
const body = {
8560
statusCode,
86-
code: error.code,
87-
error: statuses_1.default(statusCode.toString()),
61+
error: http_errors_enhanced_1.messagesByCodes[statusCode],
8862
message: error.message
8963
};
90-
properties_1.addAdditionalProperties(body, error);
64+
http_errors_enhanced_1.addAdditionalProperties(body, error);
9165
// Send the error back
9266
// eslint-disable-next-line @typescript-eslint/no-floating-promises
9367
reply

lib/index.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,13 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
1313
return (mod && mod.__esModule) ? mod : { "default": mod };
1414
};
1515
Object.defineProperty(exports, "__esModule", { value: true });
16-
exports.plugin = exports.validationMessagesFormatters = exports.niceJoin = exports.convertValidationErrors = exports.serializeError = exports.addAdditionalProperties = void 0;
16+
exports.plugin = exports.validationMessagesFormatters = exports.niceJoin = exports.convertValidationErrors = void 0;
1717
const ajv_1 = __importDefault(require("ajv"));
1818
const fastify_plugin_1 = __importDefault(require("fastify-plugin"));
1919
const handlers_1 = require("./handlers");
2020
const validation_1 = require("./validation");
2121
__exportStar(require("./handlers"), exports);
2222
__exportStar(require("./interfaces"), exports);
23-
var properties_1 = require("./properties");
24-
Object.defineProperty(exports, "addAdditionalProperties", { enumerable: true, get: function () { return properties_1.addAdditionalProperties; } });
25-
Object.defineProperty(exports, "serializeError", { enumerable: true, get: function () { return properties_1.serializeError; } });
2623
var validation_2 = require("./validation");
2724
Object.defineProperty(exports, "convertValidationErrors", { enumerable: true, get: function () { return validation_2.convertValidationErrors; } });
2825
Object.defineProperty(exports, "niceJoin", { enumerable: true, get: function () { return validation_2.niceJoin; } });
@@ -48,7 +45,7 @@ exports.plugin = fastify_plugin_1.default(function (instance, options, done) {
4845
instance.addHook('onRoute', validation_1.addResponseValidation);
4946
}
5047
done();
51-
}, { name: 'fastify-errors-properties' });
48+
}, { name: 'fastify-http-errors-enhanced' });
5249
exports.default = exports.plugin;
5350
module.exports = exports.plugin;
5451
Object.assign(module.exports, exports);

lib/properties.js

Lines changed: 0 additions & 27 deletions
This file was deleted.

lib/validation.js

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
"use strict";
2-
var __importDefault = (this && this.__importDefault) || function (mod) {
3-
return (mod && mod.__esModule) ? mod : { "default": mod };
4-
};
52
Object.defineProperty(exports, "__esModule", { value: true });
63
exports.addResponseValidation = exports.convertValidationErrors = exports.validationMessagesFormatters = exports.niceJoin = void 0;
7-
const http_errors_1 = __importDefault(require("http-errors"));
8-
const http_status_codes_1 = __importDefault(require("http-status-codes"));
4+
const http_errors_enhanced_1 = require("http-errors-enhanced");
95
const utils_1 = require("./utils");
106
function niceJoin(array, lastSeparator = ' and ', separator = ', ') {
117
switch (array.length) {
@@ -177,18 +173,18 @@ function addResponseValidation(route) {
177173
route.preSerialization = async function (_request, reply, payload) {
178174
const statusCode = reply.raw.statusCode;
179175
// Never validate error 500
180-
if (statusCode === http_status_codes_1.default.INTERNAL_SERVER_ERROR) {
176+
if (statusCode === http_errors_enhanced_1.INTERNAL_SERVER_ERROR) {
181177
return payload;
182178
}
183179
// No validator, it means the HTTP status is not allowed
184180
const validator = validators[statusCode];
185181
if (!validator) {
186-
throw http_errors_1.default(http_status_codes_1.default.INTERNAL_SERVER_ERROR, exports.validationMessagesFormatters.invalidResponseCode(statusCode));
182+
throw new http_errors_enhanced_1.InternalServerError(exports.validationMessagesFormatters.invalidResponseCode(statusCode));
187183
}
188184
// Now validate the payload
189185
const valid = validator(payload);
190186
if (!valid) {
191-
throw http_errors_1.default(http_status_codes_1.default.INTERNAL_SERVER_ERROR, exports.validationMessagesFormatters.invalidResponse(statusCode), {
187+
throw new http_errors_enhanced_1.InternalServerError(exports.validationMessagesFormatters.invalidResponse(statusCode), {
192188
failedValidations: convertValidationErrors('response', payload, validator.errors)
193189
});
194190
}

package.json

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
{
2-
"name": "fastify-errors-properties",
2+
"name": "fastify-http-errors-enhanced",
33
"version": "1.0.0",
4-
"description": "Fastify plugin to support http-errors additional properties.",
5-
"homepage": "https://sw.cowtech.it/fastify-errors-properties",
6-
"repository": "github:ShogunPanda/fastify-errors-properties",
4+
"description": "A error handling plugin for Fastify that uses enhanced HTTP errors.",
5+
"homepage": "https://sw.cowtech.it/fastify-http-errors-enhanced",
6+
"repository": "github:ShogunPanda/fastify-http-errors-enhanced",
77
"keywords": [
88
"fastify",
99
"fastify-plugin",
10-
"http-errors"
10+
"http-errors",
11+
"http-errors-enhanced"
1112
],
1213
"bugs": {
13-
"url": "https://github.com/ShogunPanda/fastify-errors-properties/issues"
14+
"url": "https://github.com/ShogunPanda/fastify-http-errors-enhanced/issues"
1415
},
1516
"author": "Shogun <shogun@cowtech.it>",
1617
"license": "ISC",
@@ -35,22 +36,18 @@
3536
"postpublish": "git push origin && git push origin -f --tags"
3637
},
3738
"dependencies": {
38-
"ajv": "^6.12.5",
39-
"fastify-plugin": "^2.3.4",
40-
"http-errors": "^1.8.0",
41-
"http-status-codes": "^2.1.4",
42-
"statuses": "^2.0.0"
39+
"ajv": "^6.12.6",
40+
"fastify-plugin": "^3.0.0",
41+
"http-errors-enhanced": "^0.3.0"
4342
},
4443
"devDependencies": {
45-
"@cowtech/eslint-config": "^7.10.0",
46-
"@types/http-errors": "^1.8.0",
47-
"@types/node": "^14.11.2",
48-
"@types/statuses": "^2.0.0",
44+
"@cowtech/eslint-config": "^7.10.1",
45+
"@types/node": "^14.14.10",
4946
"@types/tap": "^14.10.1",
50-
"fastify": "^3.4.1",
51-
"prettier": "^2.1.2",
52-
"tap": "^14.10.8",
53-
"typescript": "^4.0.3"
47+
"fastify": "^3.8.0",
48+
"prettier": "^2.2.1",
49+
"tap": "^14.11.0",
50+
"typescript": "^4.1.2"
5451
},
5552
"engines": {
5653
"node": ">=12.15.0"

0 commit comments

Comments
 (0)