@middy/util
Advanced tools
Comparing version 2.3.0 to 2.4.0
53
index.js
@@ -210,2 +210,52 @@ "use strict"; | ||
return response; | ||
}; // smaller version of `http-errors` | ||
const statuses = require('./codes.json'); | ||
const { | ||
inherits | ||
} = require('util'); | ||
const createErrorRegexp = /[^a-zA-Z]/g; | ||
const createError = (code, message, properties = {}) => { | ||
const name = statuses[code].replace(createErrorRegexp, ''); | ||
const className = name.substr(-5) !== 'Error' ? name + 'Error' : name; | ||
function HttpError(message) { | ||
// create the error object | ||
const msg = message !== null && message !== void 0 ? message : statuses[code]; | ||
const err = new Error(msg); // capture a stack trace to the construction point | ||
Error.captureStackTrace(err, HttpError); // adjust the [[Prototype]] | ||
Object.setPrototypeOf(err, HttpError.prototype); // redefine the error message | ||
Object.defineProperty(err, 'message', { | ||
enumerable: true, | ||
configurable: true, | ||
value: msg, | ||
writable: true | ||
}); // redefine the error name | ||
Object.defineProperty(err, 'name', { | ||
enumerable: false, | ||
configurable: true, | ||
value: className, | ||
writable: true | ||
}); | ||
return err; | ||
} | ||
inherits(HttpError, Error); | ||
const desc = Object.getOwnPropertyDescriptor(HttpError, 'name'); | ||
desc.value = className; | ||
Object.defineProperty(HttpError, 'name', desc); | ||
Object.assign(HttpError.prototype, { | ||
status: code, | ||
statusCode: code, | ||
expose: code < 500 | ||
}, properties); | ||
return new HttpError(message); | ||
}; | ||
@@ -224,3 +274,4 @@ | ||
jsonSafeParse, | ||
normalizeHttpResponse | ||
normalizeHttpResponse, | ||
createError | ||
}; |
{ | ||
"name": "@middy/util", | ||
"version": "2.3.0", | ||
"version": "2.4.0", | ||
"description": "🛵 The stylish Node.js middleware engine for AWS Lambda (util package)", | ||
@@ -46,3 +46,3 @@ "type": "commonjs", | ||
"devDependencies": { | ||
"@middy/core": "^2.3.0", | ||
"@middy/core": "^2.4.0", | ||
"@types/aws-lambda": "^8.10.76", | ||
@@ -54,3 +54,3 @@ "@types/node": "^15.9.0", | ||
"homepage": "https://github.com/middyjs/middy#readme", | ||
"gitHead": "bfde6fec04cea012747c547d0cfd44bb2ea4e1ef" | ||
"gitHead": "d20d67c6ec1c8dc1f58b1bedbcbb71f70d302fb5" | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
13084
243
136
3
4
1
32