All Projects → koajs → json-error

koajs / json-error

Licence: MIT license
Error handler for pure-JSON apps

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to json-error

koa-better-error-handler
A better error-handler for Lad and Koa. Makes `ctx.throw` awesome (best used with koa-404-handler)
Stars: ✭ 51 (-39.29%)
Mutual labels:  koa, error-handler
restria
Entria's REST API boilerplate
Stars: ✭ 25 (-70.24%)
Mutual labels:  koa
react-graphql
react-graphql 快速开发方案
Stars: ✭ 15 (-82.14%)
Mutual labels:  koa
DiscordBot-Template
A boilerplate / template for discord.js bots with 100% coverage of Discord API, command handler, error handler based on https://discordjs.guide/
Stars: ✭ 129 (+53.57%)
Mutual labels:  error-handler
express-to-koa
Use express middlewares in Koa2, the one that really works.
Stars: ✭ 18 (-78.57%)
Mutual labels:  koa
koa-server
🗄️ GraphQL Back-end Server with Relay, Koa, MongoDB and Mongoose
Stars: ✭ 31 (-63.1%)
Mutual labels:  koa
oss-upload-nodejs
服务器签名后直接上传文件到阿里云 OSS
Stars: ✭ 61 (-27.38%)
Mutual labels:  koa
node-input-validator
Validation library for node.js
Stars: ✭ 74 (-11.9%)
Mutual labels:  koa
Movie-Paradise
A responsive movie preview web app
Stars: ✭ 19 (-77.38%)
Mutual labels:  koa
react-koa-universal
a boilerplate react graphql apollo css-in-js buzzword koa ssr pwa wasm throwaway app 🚮
Stars: ✭ 12 (-85.71%)
Mutual labels:  koa
node-fs
node-fs
Stars: ✭ 55 (-34.52%)
Mutual labels:  koa
laravel-error-handler
Laravel 5.2/5.3 package for better exception handling.
Stars: ✭ 15 (-82.14%)
Mutual labels:  error-handler
coolliyong.github.io
javascript基础
Stars: ✭ 57 (-32.14%)
Mutual labels:  koa
touchbase.ai
E2E-encryption-powered Personal CRM - smartly engage more in meaningful relationships
Stars: ✭ 12 (-85.71%)
Mutual labels:  koa
error
Makes handling and debugging PHP errors suck less
Stars: ✭ 17 (-79.76%)
Mutual labels:  error-handler
douban-book-api
第三方豆瓣读书 api 接口
Stars: ✭ 44 (-47.62%)
Mutual labels:  koa
nodejs-graphql-template
Template project for Node.js® GraphQL APIs
Stars: ✭ 13 (-84.52%)
Mutual labels:  koa
inversify-koa-utils
inversify-koa-utils is a module based on inversify-express-utils. This module has utilities for koa 2 applications development using decorators and IoC Dependency Injection (with inversify)
Stars: ✭ 27 (-67.86%)
Mutual labels:  koa
store-server
vue-store项目后端。基于Node.js(Koa)实现的电商后端项目。
Stars: ✭ 199 (+136.9%)
Mutual labels:  koa
koa2-rest-scaffold
Koa2 RESTful API 脚手架。
Stars: ✭ 27 (-67.86%)
Mutual labels:  koa

Koa JSON Error

NPM version Build status Test coverage Dependency Status License Downloads

Error handler for pure Koa >=2.0.0 JSON apps where showing the stack trace is cool!

npm install --save koa-json-error

Versions >=3.0.0 support Koa ^2.0.0. For earlier versions of Koa, please use previous releases.

Requirements

  • node >=6.0.0
  • koa >=2.2.0

Starting from 3.2.0, this package supports node >=6.0.0 to match Koa requirements.

API

'use strict';
const koa = require('koa');
const error = require('koa-json-error')

let app = new Koa();
app.use(error())

If you don't really feel that showing the stack trace is that cool, you can customize the way errors are shown on responses. There's a basic and more advanced, granular approach to this.

Basic usage

You can provide a single formatter function as an argument on middleware initialization. It receives the original raised error and it is expected to return a formatted response.

Here's a simple example:

'use strict';
const koa = require('koa');
const error = require('koa-json-error')

function formatError(err) {
    return {
        // Copy some attributes from
        // the original error
        status: err.status,
        message: err.message,

        // ...or add some custom ones
        success: false,
        reason: 'Unexpected'
    }
}

let app = new Koa();
app.use(error(formatError));

This basic configuration is essentially the same (and serves as a shorthand for) the following:

'use strict';
let app = new Koa();
app.use(error({
    preFormat: null,
    format: formatError
}));

See section below.

Advanced usage

You can also customize errors on responses through a series of three formatter functions, specified in an options object. They receive the raw error object and return a formatted response. This gives you fine-grained control over the final output and allows for different formats on various environments.

You may pass in the options object as argument to the middleware. These are the available settings.

options.preFormat (Function)

Perform some task before calling options.format. Must be a function with the original err as its only argument.

Defaults to:

(err) => Object.assign({}, err)

Which sets all enumerable properties of err onto the formatted object.

options.format (Function)

Runs inmediatly after options.preFormat. It receives two arguments: the original err and the output of options.preFormat. It should return a newly formatted error.

Defaults to adding the following non-enumerable properties to the output:

const DEFAULT_PROPERTIES = [
  'name',
  'message',
  'stack',
  'type'
];

It also defines a status property like so:

obj.status = err.status || err.statusCode || 500;

options.postFormat (Function)

Runs inmediatly after options.format. It receives two arguments: the original err and the output of options.format. It should return a newly formatted error.

The default is a no-op (final output is defined by options.format).

This option is useful when you want to preserve the default functionality and extend it in some way.

For example,

'use strict';
const _ = require('lodash');
const koa = require('koa');
const error = require('koa-json-error')

let options = {
    // Avoid showing the stacktrace in 'production' env
    postFormat: (e, obj) => process.env.NODE_ENV === 'production' ? _.omit(obj, 'stack') : obj
};
let app = new Koa();
app.use(error(options));

Modifying the error inside the *format functions will mutate the original object. Be aware of that if any other Koa middleware runs after this one.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].