Skip to content

Commit

Permalink
feat: AuthError and NotFoundError respond with same template
Browse files Browse the repository at this point in the history
  • Loading branch information
andreidmt committed Feb 18, 2020
1 parent 03ab7a7 commit f5d62f9
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 33 deletions.
2 changes: 1 addition & 1 deletion src/errors/authorization.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { BaseError } from "./base"

export class AuthorizationError extends BaseError {
constructor(message = "Not allowed to access resource", details = {}) {
constructor(message, details = {}) {
super(message, details)

this.name = "AuthorizationError"
Expand Down
27 changes: 9 additions & 18 deletions src/middleware/req-route-exists.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,18 @@
const debug = require("debug")("Blocks:URLParamsMiddleware")

import { NotFoundError } from "../errors/not-found"
const debug = require("debug")("Blocks:RouteExistsMiddleware")

module.exports = ({ Router }) => (req, res, next) => {
const { route, params } = Router.find({
method: req.method,
pathname: req.ctx.pathname,
})
try {
const { route, params } = Router.find({
method: req.method,
pathname: req.ctx.pathname,
})

if (route) {
req.ctx.params = params
req.ctx.route = route

next()
} else {
next(
new NotFoundError(
`Endpoint ${req.method}:${req.ctx.pathname} not found`,
{
method: req.method,
pathname: req.ctx.pathname,
}
)
)
} catch (error) {
// NotFoundError
next(error)
}
}
43 changes: 29 additions & 14 deletions src/plugins/router.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
const debug = require("debug")("Blocks:RouterPlugin")

import { pathToRegexp } from "path-to-regexp"
import { count, push, reduce, find, merge, pick, is } from "@mutantlove/m"
import {
count,
push,
reduce,
find,
merge,
pick,
is,
isEmpty,
} from "@mutantlove/m"

import { InputValidationError } from "../errors/input"
import { AuthorizationError } from "../errors/authorization"
import { NotFoundError } from "../errors/not-found"

export default {
create: () => {
Expand Down Expand Up @@ -41,20 +51,22 @@ export default {
return isPathMatch && isMethodMatch
})(routes)

let params = {}

if (route) {
const paramsList = route.pathRegExp.exec(pathname)

params = reduce(
(acc, element, index) => ({
...acc,
[element.name]: paramsList[index + 1],
}),
{}
)(route.pathParamsKeys)
if (isEmpty(route)) {
throw new NotFoundError(`Endpoint ${method}:${pathname} not found`, {
method,
path: pathname,
})
}

const paramsList = route.pathRegExp.exec(pathname)
const params = reduce(
(acc, element, index) => ({
...acc,
[element.name]: paramsList[index + 1],
}),
{}
)(route.pathParamsKeys)

return {
route,
params,
Expand Down Expand Up @@ -119,7 +131,10 @@ export default {
Promise.resolve(route.isAllowed(req))
.then(isAllowed => {
if (!isAllowed) {
throw new AuthorizationError()
throw new AuthorizationError("Not allowed to access resource", {
method: route.method,
path: route.path,
})
}

return null
Expand Down

0 comments on commit f5d62f9

Please sign in to comment.