@kaliber/build 0.0.152-beta.1 → 0.0.152-beta.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. package/lib/serve.js +30 -14
  2. package/package.json +1 -1
package/lib/serve.js CHANGED
@@ -66,15 +66,8 @@ app.use((err, req, res, next) => {
66
66
  if (err.status && err.status >= 400 && err.status < 500)
67
67
  return res.status(err.status).send()
68
68
 
69
- console.error(err)
70
- if (reportError) reportError(err, req)
71
-
72
- const response = res.status(500)
73
- if (isProduction) {
74
- findFile(req.path, internalServerError)
75
- .then(file => file ? response.sendFile(file) : next())
76
- .catch(next)
77
- } else response.send(`<pre><title style='display: block;'>${err.stack || err.toString()}</title><pre>`)
69
+ reportServerError(err, req)
70
+ serveInternalServerError(err, { req, res, next })
78
71
  })
79
72
 
80
73
  app.listen(port, () => console.log(`Server listening at port ${port}`))
@@ -84,7 +77,7 @@ async function resolveFile(req, res, next) {
84
77
  const { path } = req
85
78
  /** @type {Array<[string, (file:any) => any]>} */
86
79
  const combinations = [
87
- [indexWithRouting, file => serveIndexWithRouting(req, res, file)],
80
+ [indexWithRouting, file => serveIndexWithRouting(file, { req, res, next })],
88
81
  [notFound, file => res.status(404).sendFile(file)],
89
82
  [index, file => res.status(200).sendFile(file)],
90
83
  ]
@@ -138,7 +131,7 @@ function possibleDirectories(path) {
138
131
  return possibleDirectories
139
132
  }
140
133
 
141
- function serveIndexWithRouting(req, res, file) {
134
+ function serveIndexWithRouting(file, { req, res, next }) {
142
135
  const envRequire = isProduction ? require : require('import-fresh')
143
136
 
144
137
  const routeTemplate = envRequire(file)
@@ -153,10 +146,19 @@ function serveIndexWithRouting(req, res, file) {
153
146
  const html = renderTemplate(template, location, { data })
154
147
  res.status(status).set(headers).send(html)
155
148
  })
149
+ .catch(error => {
150
+ reportServerError(error, req)
151
+ serveInternalServerError(error, { req, res, next })
152
+ })
156
153
  else {
157
- const { data, status, headers } = dataOrPromise
158
- const html = renderTemplate(template, location, { data })
159
- res.status(status).set(headers).send(html)
154
+ try {
155
+ const { data, status, headers } = dataOrPromise
156
+ const html = renderTemplate(template, location, { data })
157
+ res.status(status).set(headers).send(html)
158
+ } catch (error) {
159
+ reportServerError(error, req)
160
+ serveInternalServerError(error, { req, res, next })
161
+ }
160
162
  }
161
163
  }
162
164
 
@@ -181,3 +183,17 @@ function renderTemplate(template, location, { data }) {
181
183
  const html = template({ location, data })
182
184
  return html
183
185
  }
186
+
187
+ function serveInternalServerError(error, { res, req, next }) {
188
+ const response = res.status(500)
189
+ if (isProduction) {
190
+ findFile(req.path, internalServerError)
191
+ .then(file => file ? response.sendFile(file) : next())
192
+ .catch(next)
193
+ } else response.send(`<pre><title style='display: block;'>${error.stack || error.toString()}</title><pre>`)
194
+ }
195
+
196
+ function reportServerError(error, req) {
197
+ console.error(error)
198
+ if (reportError) reportError(error, req)
199
+ }
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "0.0.152-beta.1",
2
+ "version": "0.0.152-beta.3",
3
3
  "name": "@kaliber/build",
4
4
  "description": "Zero configuration, opinionated webpack / react build setup",
5
5
  "scripts": {