Skip to content

Commit

Permalink
fix(errors): clean up organization of errors to separate file (#37)
Browse files Browse the repository at this point in the history
Co-Authored-By: Liran Tal <[email protected]>
  • Loading branch information
captaincaius and lirantal committed Jun 19, 2019
1 parent 2b298cb commit 4e07023
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 12 deletions.
11 changes: 11 additions & 0 deletions errors/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
'use strict'
class RouteVersionUnmatchedError extends Error {
constructor (message) {
super(message)
this.name = this.constructor.name
}
}

module.exports = {
RouteVersionUnmatchedError
}
12 changes: 1 addition & 11 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,9 @@
'use strict'

const semver = require('semver')

class RouteVersionUnmatchedError extends Error {
constructor (message) {
super(message)
this.name = this.constructor.name
}
}
const { RouteVersionUnmatchedError } = require('./errors')

class versionRouter {
static get RouteVersionUnmatchedError () {
return RouteVersionUnmatchedError
}

static route (versionsMap = new Map(), options = new Map()) {
return (req, res, next) => {
for (let [versionKey, versionRouter] of versionsMap) {
Expand Down
3 changes: 2 additions & 1 deletion test/sanity.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const test = require('ava')
const versionRouter = require('../index')
const { RouteVersionUnmatchedError } = require('../errors')

test('given a versioned router, match the route', t => {
const v1 = '1.0'
Expand Down Expand Up @@ -64,7 +65,7 @@ test('given a versioned router, dont match the requestVersion and error out if n
const result = middleware(req, resIn, nextHandler)
t.falsy(resIn)
t.truthy(result instanceof Error)
t.truthy(result instanceof versionRouter.RouteVersionUnmatchedError)
t.truthy(result instanceof RouteVersionUnmatchedError)
t.truthy(result.name === 'RouteVersionUnmatchedError')
t.truthy(result.message === `${requestedVersion} doesn't match any versions`)
})
Expand Down

0 comments on commit 4e07023

Please sign in to comment.