@srfnstack/spliffy 0.9.0 → 0.9.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@srfnstack/spliffy",
3
- "version": "0.9.0",
3
+ "version": "0.9.1",
4
4
  "author": "snowbldr",
5
5
  "private": false,
6
6
  "homepage": "https://github.com/narcolepticsnowman/spliffy",
@@ -37,9 +37,7 @@ export const cloneMiddleware = (middleware) => {
37
37
  * @param middleware
38
38
  */
39
39
  export const validateMiddleware = (middleware) => {
40
- if (Array.isArray(middleware)) {
41
- validateMiddlewareArray(middleware)
42
- } else if (typeof middleware === 'object') {
40
+ if (!Array.isArray(middleware) && typeof middleware === 'object') {
43
41
  for (const method in middleware) {
44
42
  // ensure methods are always available as uppercase
45
43
  const upMethod = method.toUpperCase()
@@ -47,7 +45,7 @@ export const validateMiddleware = (middleware) => {
47
45
  validateMiddlewareArray(middleware[upMethod])
48
46
  }
49
47
  } else {
50
- throw new Error('Invalid middleware definition: ' + middleware)
48
+ validateMiddlewareArray(middleware)
51
49
  }
52
50
  }
53
51
 
package/src/routes.mjs CHANGED
@@ -51,6 +51,14 @@ const importModules = async (config, dirPath, files) => Promise.all(
51
51
  .map(f => path.join(dirPath, f.name))
52
52
  .map(mwPath => import(`file://${mwPath}`)
53
53
  .then(module => ({ module, mwPath }))
54
+ .catch(e => {
55
+ // Hack to workaround https://github.com/nodejs/modules/issues/471
56
+ if (e instanceof SyntaxError) {
57
+ const newError = new SyntaxError(`${e.message}. In file: ${mwPath}`)
58
+ newError.stack = e.stack
59
+ throw newError
60
+ }
61
+ })
54
62
  ))
55
63
 
56
64
  const findRoutesInDir = async (name, filePath, urlPath, inheritedMiddleware, pathParameters, config) => {
@@ -106,10 +114,25 @@ const buildJSHandlerRoute = async (name, filePath, urlPath, inheritedMiddleware,
106
114
  filePath,
107
115
  handlers: {}
108
116
  }
109
- const module = await import(`file://${filePath}`)
117
+ let module
118
+ try {
119
+ module = await import(`file://${filePath}`)
120
+ } catch (e) {
121
+ // Hack to workaround https://github.com/nodejs/modules/issues/471
122
+ if (e instanceof SyntaxError) {
123
+ const newError = new SyntaxError(`${e.message}. In file: ${filePath}`)
124
+ newError.stack = e.stack
125
+ throw newError
126
+ }
127
+ }
110
128
  const handlers = module.default
111
-
112
- route.middleware = mergeMiddleware(handlers.middleware || [], inheritedMiddleware)
129
+ try {
130
+ route.middleware = mergeMiddleware(handlers.middleware || [], inheritedMiddleware)
131
+ } catch (e) {
132
+ const err = new Error(`Failed to load middleware for route: ${filePath}`)
133
+ err.stack += `\nCaused By: ${e.stack}`
134
+ throw err
135
+ }
113
136
  for (const method of Object.keys(handlers).filter(k => !ignoreHandlerFields[k])) {
114
137
  if (HTTP_METHODS.indexOf(method) === -1) {
115
138
  throw new Error(`Method: ${method} in file ${filePath} is not a valid http method. It must be one of: ${HTTP_METHODS}. Method names must be all uppercase.`)