entix
v0.1.3
Published
#### Getting Started: index.js ```js global['entix'] = require('entixio')() const bodyparser = require('koa-body') // middleware
Downloads
7
Readme
entix
Getting Started: index.js
global['entix'] = require('entixio')()
const bodyparser = require('koa-body') // middleware
entix.use(bodyparser())
// load more global middleware here ...
entix.startServer()
Adding ErrorHandlers: index.js
global['entix'] = require('entixio')()
const { ValidationError } = require('entixio').joi
const { UniqueViolationError } = require('entixio').objection
const { JsonWebTokenError } = require('jsonwebtoken')
const bodyparser = require('koa-body')
const validationErrorHandler = async ({ctx, error}) => {
if(error instanceof ValidationError){
console.log('jackopt')
ctx.status = 422
const { details, _original } = error
error = { original: _original, details: {}}
details.map(d => error.details[d.context.key] = d.message)
return error
}
}
const databaseErrorHandler = async ({ctx, error}) => {
if(error instanceof UniqueViolationError){
ctx.status = 422
const key = error.columns.pop()
error = { original: ctx.request.body, details: {}}
error.details[key] = `this ${key} is already taken`
return error
}
}
const jwtErrorHandler = async ({ctx, error}) => {
if(error instanceof JsonWebTokenError){
const managed = ['invalid signature', 'jwt expired', 'jwt malformed', 'jwt must be provided', 'jwt token']
if(managed.includes(error.message)){
ctx.status = 401
error = { message: error.message }
return error
}
}
}
entix.use(bodyparser())
entix.loadErrorHandler(validationErrorHandler)
entix.loadErrorHandler(databaseErrorHandler)
entix.loadErrorHandler(jwtErrorHandler)
entix.startServer()