waibu 2.3.2 → 2.3.4
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/lib/handle-error.js +7 -12
- package/lib/handle-not-found.js +41 -5
- package/lib/handle-redirect.js +4 -4
- package/lib/handle-xml-body.js +1 -1
- package/lib/web-app.js +2 -2
- package/package.json +1 -1
- package/wiki/CHANGES.md +6 -0
package/lib/handle-error.js
CHANGED
|
@@ -1,24 +1,19 @@
|
|
|
1
1
|
import { redirect } from './handle-redirect.js'
|
|
2
|
-
import { notFound, writeHtml } from './handle-not-found.js'
|
|
2
|
+
import { notFound, writeHtml, interceptor } from './handle-not-found.js'
|
|
3
3
|
|
|
4
|
-
async function error (req, reply
|
|
5
|
-
const { get } = this.app.lib._
|
|
6
|
-
const webApp = get(req, 'routeOptions.config.webApp')
|
|
4
|
+
async function error (err, req, reply) {
|
|
7
5
|
this.log.error(err)
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
const errorHandler = get(plugin, 'waibuFactory.errorHandler')
|
|
11
|
-
if (errorHandler) return await errorHandler.call(plugin, err, req, reply)
|
|
12
|
-
}
|
|
6
|
+
const resp = await interceptor.call(this, 'errorHandler', err, req, reply)
|
|
7
|
+
if (resp) return resp
|
|
13
8
|
return writeHtml.call(this, req, reply, `${this.ns}:/extend/bajoTemplate/template/500.html`, { text: this.app.log.getErrorMessage(err) })
|
|
14
9
|
}
|
|
15
10
|
|
|
16
11
|
async function handleError () {
|
|
17
12
|
const me = this
|
|
18
13
|
this.instance.setErrorHandler(async function (err, req, reply) {
|
|
19
|
-
if (err.message === '_notFound' || err.statusCode === 404) return await notFound.call(me, req, reply
|
|
20
|
-
if (err.message === '_redirect' && err.redirect) return redirect.call(me,
|
|
21
|
-
return await error.call(me, req, reply
|
|
14
|
+
if (err.message === '_notFound' || err.statusCode === 404) return await notFound.call(me, err, req, reply)
|
|
15
|
+
if (err.message === '_redirect' && err.redirect) return redirect.call(me, err, req, reply)
|
|
16
|
+
return await error.call(me, err, req, reply)
|
|
22
17
|
})
|
|
23
18
|
}
|
|
24
19
|
|
package/lib/handle-not-found.js
CHANGED
|
@@ -10,15 +10,51 @@ export function writeHtml (req, reply, tpl, payload) {
|
|
|
10
10
|
return compiled(payload)
|
|
11
11
|
}
|
|
12
12
|
|
|
13
|
-
export async function
|
|
13
|
+
export async function interceptor (err, name, req, reply) {
|
|
14
14
|
const { get } = this.app.lib._
|
|
15
15
|
const webApp = get(req, 'routeOptions.config.webApp')
|
|
16
|
-
reply.code(404)
|
|
17
16
|
if (webApp) {
|
|
18
17
|
const plugin = this.app[webApp]
|
|
19
|
-
const
|
|
20
|
-
if (
|
|
18
|
+
const handler = get(plugin, `waibuFactory.${name}`)
|
|
19
|
+
if (handler) return await handler.call(plugin, err, req, reply)
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function redirSvc (req) {
|
|
24
|
+
const { trim, find, get } = this.app.lib._
|
|
25
|
+
const { outmatch } = this.app.lib
|
|
26
|
+
let match = false
|
|
27
|
+
let [prefix, ...args] = trim(req.url, '/').split('/')
|
|
28
|
+
args = '/' + args.join('/')
|
|
29
|
+
let plugin = find(this.app.getAllNs(), p => {
|
|
30
|
+
return get(this, `app.${p}.config.waibu.prefix`) === prefix
|
|
31
|
+
})
|
|
32
|
+
if (!plugin) {
|
|
33
|
+
plugin = 'main'
|
|
34
|
+
args = `/${prefix}`
|
|
21
35
|
}
|
|
36
|
+
const items = get(this, `app.${plugin}.config.waibuMpa.redirect`, {})
|
|
37
|
+
for (const k in items) {
|
|
38
|
+
const isMatch = outmatch(k)
|
|
39
|
+
if (isMatch(args)) {
|
|
40
|
+
match = items[k]
|
|
41
|
+
break
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
return match
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export async function notFound (err, req, reply) {
|
|
48
|
+
const { getMethod } = this.app.bajo
|
|
49
|
+
let redirectTo = await redirSvc.call(this, req, reply)
|
|
50
|
+
if (redirectTo !== false) {
|
|
51
|
+
const fn = getMethod(redirectTo, false)
|
|
52
|
+
if (fn) redirectTo = await fn(req)
|
|
53
|
+
if (redirectTo) return reply.redirectTo(redirectTo)
|
|
54
|
+
}
|
|
55
|
+
reply.code(404)
|
|
56
|
+
const resp = await interceptor.call(this, 'notFoundHandler', err, req, reply)
|
|
57
|
+
if (resp) return resp
|
|
22
58
|
const text = req.t('notFound%s%s', req.t('route'), req.url)
|
|
23
59
|
return writeHtml.call(this, req, reply, `${this.ns}:/extend/bajoTemplate/template/400.html`, { text })
|
|
24
60
|
}
|
|
@@ -26,7 +62,7 @@ export async function notFound (req, reply, err = {}) {
|
|
|
26
62
|
async function handleNotFound () {
|
|
27
63
|
const me = this
|
|
28
64
|
me.instance.setNotFoundHandler(async function (req, reply) {
|
|
29
|
-
return await notFound.call(me, req, reply)
|
|
65
|
+
return await notFound.call(me, null, req, reply)
|
|
30
66
|
})
|
|
31
67
|
}
|
|
32
68
|
|
package/lib/handle-redirect.js
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
import path from 'path'
|
|
2
2
|
|
|
3
|
-
export function redirect (
|
|
4
|
-
if (
|
|
5
|
-
else reply.redirect(this.routePath(
|
|
3
|
+
export function redirect (err = {}, req, reply) {
|
|
4
|
+
if (err.redirect.startsWith('http') || path.isAbsolute(err.redirect)) reply.redirect(err.redirect)
|
|
5
|
+
else reply.redirect(this.routePath(err.redirect, err.options))
|
|
6
6
|
return reply
|
|
7
7
|
}
|
|
8
8
|
|
|
9
9
|
async function handleRedirect (options) {
|
|
10
10
|
const me = this
|
|
11
11
|
this.instance.decorateReply('redirectTo', function (url, options = {}) {
|
|
12
|
-
return redirect.call(me, this, url, options)
|
|
12
|
+
return redirect.call(me, { redirect: url, options }, null, this, url, options)
|
|
13
13
|
})
|
|
14
14
|
}
|
|
15
15
|
|
package/lib/handle-xml-body.js
CHANGED
package/lib/web-app.js
CHANGED
|
@@ -24,10 +24,10 @@ export async function collect (glob = 'boot.js', baseNs) {
|
|
|
24
24
|
|
|
25
25
|
async function webApp () {
|
|
26
26
|
const { runHook } = this.app.bajo
|
|
27
|
-
|
|
27
|
+
this.webApps = await collect.call(this)
|
|
28
28
|
await runHook(`${this.ns}:beforeAppBoot`)
|
|
29
29
|
// build routes
|
|
30
|
-
for (const m of
|
|
30
|
+
for (const m of this.webApps) {
|
|
31
31
|
const disabled = this.app[m.ns].config.disabled
|
|
32
32
|
if (Array.isArray(disabled) && disabled.length === 1 && ['*', 'all'].includes(disabled[0])) {
|
|
33
33
|
this.log.warn('allRoutesConfigDisabled%s', m.ns)
|
package/package.json
CHANGED