Security News
NIST Misses 2024 Deadline to Clear NVD Backlog
NIST has failed to meet its self-imposed deadline of clearing the NVD's backlog by the end of the fiscal year. Meanwhile, CVE's awaiting analysis have increased by 33% since June.
koa-error-mapper
Advanced tools
koa-error-mapper
is a middleware that handles application errors by mapping them to a custom format.
Install the package via yarn
:
❯ yarn add koa-error-mapper
or via npm
:
❯ npm install koa-error-mapper --save
Mappers are responsible for handling errors and normalizing the response. A generic fallback mapper is included in case no custom mapper is available.
The only interface for a mapper is a map()
function. A mapper should return undefined
if it is not capable or responsible for mapping a specific error. It must return an object with status
and body
. If not returned on the object, the value on the response will be undefined
for precaution. It may also contain a headers
property.
module.exports = {
map(e) {
if (!(e instanceof CustomError)) {
return;
}
// Add your own custom logic here.
return { status: e.code, body: { message: e.message }, headers: { Foo: 'Bar' }};
}
};
Now use the error mapper and register CustomMapper
:
'use strict';
const CustomError = require('path/to/my/custom/error');
const Koa = require('koa');
const app = new Koa();
const customMapper = require('path/to/my/custom/mapper');
app.use(errorMapper([customMapper]);
app.get('/', async () => {
throw new CustomError(401, 'Ah-ah!');
});
app.listen(3000);
Result:
GET /
HTTP/1.1 401 Unauthorized
Foo: Bar
{ "message": "Ah-ah!" }
❯ yarn test
❯ npm version [<new version> | major | minor | patch] -m "Release %s"
MIT
v4.1.0 (2020-11-06)
FAQs
An error mapper for Koa applications
The npm package koa-error-mapper receives a total of 1,530 weekly downloads. As such, koa-error-mapper popularity was classified as popular.
We found that koa-error-mapper demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 9 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
NIST has failed to meet its self-imposed deadline of clearing the NVD's backlog by the end of the fiscal year. Meanwhile, CVE's awaiting analysis have increased by 33% since June.
Security News
Cloudflare has launched a setup wizard allowing users to easily create and manage a security.txt file for vulnerability disclosure on their websites.
Security News
The Socket Research team breaks down a malicious npm package targeting the legitimate DOMPurify library. It uses obfuscated code to hide that it is exfiltrating browser and crypto wallet data.