Skip to content

Commit

Permalink
Add errorHandler fix #6
Browse files Browse the repository at this point in the history
  • Loading branch information
diegohaz committed Apr 30, 2016
1 parent 90a9edc commit 657d533
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 13 deletions.
30 changes: 22 additions & 8 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
/** @module menquery */
import _ from 'lodash'
import MenqueryParam from './menquery-param'
import MenquerySchema from './menquery-schema'
import Param from './menquery-param'
import Schema from './menquery-schema'

export {MenqueryParam as Param}
export {MenquerySchema as Schema}
export {Param, Schema}

export let handlers = {
parsers: {},
Expand Down Expand Up @@ -69,13 +68,13 @@ export function validator (name, fn) {
*/
export function middleware (schema, options) {
return function (req, res, next) {
let _schema = schema instanceof MenquerySchema
let _schema = schema instanceof Schema
? _.clone(schema)
: new MenquerySchema(schema, options)
: new Schema(schema, options)

_schema.validate(req.query, (err) => {
if (err) {
res.status(400)
req.menquery = {error: err}
return next(err)
}

Expand All @@ -86,4 +85,19 @@ export function middleware (schema, options) {
}
}

export default {handler, parser, formatter, validator, middleware}
/**
* Error handler middleware.
* @memberof menquery
* @return {Function} The middleware.
*/
export function errorHandler () {
return function (err, req, res, next) {
if (req.menquery && req.menquery.error) {
res.status(400).json(req.menquery.error)
} else {
next(err)
}
}
}

export default {Schema, Param, handlers, handler, parser, formatter, validator, middleware, errorHandler}
2 changes: 1 addition & 1 deletion src/menquery-schema.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import _ from 'lodash'
import * as menquery from './'
import menquery from './'
import MenqueryParam from './menquery-param'

/**
Expand Down
8 changes: 4 additions & 4 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import request from 'supertest'
import express from 'express'
import mongoose from 'mongoose'
import test from 'tape'
import * as menquery from '../src'
import menquery from '../src'
import './menquery-param'
import './menquery-schema'

Expand All @@ -20,15 +20,15 @@ let Test = mongoose.model('Test', schema)

let route = (...args) => {
let app = express()
app.get('/tests', menquery.middleware(...args), (err, req, res, next) => {
return err ? res.json(err) : next()
}, (req, res) => {
app.get('/tests', menquery.middleware(...args), (req, res) => {
Test.find(req.menquery.query, req.menquery.select, req.menquery.cursor).then((items) => {
res.status(200).json(items)
}).catch((err) => {
res.status(500).send(err)
})
})

app.use(menquery.errorHandler())
return app
}

Expand Down

0 comments on commit 657d533

Please sign in to comment.