Make errors and their messages more accurate and helpful (#2536)

Also, I normalized them to all be sentences for consistency, and I moved
the reentrancy check from `m.mount` to `m.render` to be a little more
helpful. The router change during mounting is inconsequential and only
to avoid the new modified error, and the change to the update loop is to
send the original error if an error occurred while initializing the
default route. (This is all around more useful anyways.)

And while I was at it, I fixed an obscure bug with sync redraws.
This commit is contained in:
Isiah Meadows 2019-09-30 16:08:04 -04:00 committed by GitHub
parent 475747800a
commit b98ab29efd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
14 changed files with 310 additions and 54 deletions

View file

@ -33,16 +33,16 @@ module.exports = function($window, mountRedraw) {
var SKIP = route.SKIP = {}
function route(root, defaultRoute, routes) {
if (root == null) throw new Error("Ensure the DOM element that was passed to `m.route` is not undefined")
if (!root) throw new TypeError("DOM element being rendered to does not exist.")
// 0 = start
// 1 = init
// 2 = ready
var state = 0
var compiled = Object.keys(routes).map(function(route) {
if (route[0] !== "/") throw new SyntaxError("Routes must start with a `/`")
if (route[0] !== "/") throw new SyntaxError("Routes must start with a '/'.")
if ((/:([^\/\.-]+)(\.{3})?:/).test(route)) {
throw new SyntaxError("Route parameter names must be separated with either `/`, `.`, or `-`")
throw new SyntaxError("Route parameter names must be separated with either '/', '.', or '-'.")
}
return {
route: route,
@ -61,7 +61,7 @@ module.exports = function($window, mountRedraw) {
var defaultData = parsePathname(defaultRoute)
if (!compiled.some(function (i) { return i.check(defaultData) })) {
throw new ReferenceError("Default route doesn't match any known routes")
throw new ReferenceError("Default route doesn't match any known routes.")
}
}
@ -87,8 +87,8 @@ module.exports = function($window, mountRedraw) {
assign(data.params, $window.history.state)
function fail() {
if (path === defaultRoute) throw new Error("Could not resolve default route " + defaultRoute)
function reject(e) {
console.error(e)
setPath(defaultRoute, null, {replace: true})
}
@ -123,13 +123,17 @@ module.exports = function($window, mountRedraw) {
else if (payload.onmatch) {
p.then(function () {
return payload.onmatch(data.params, path, matchedRoute)
}).then(update, fail)
}).then(update, path === defaultRoute ? null : reject)
}
else update("div")
return
}
}
fail()
if (path === defaultRoute) {
throw new Error("Could not resolve default route " + defaultRoute + ".")
}
setPath(defaultRoute, null, {replace: true})
}
}
@ -157,12 +161,11 @@ module.exports = function($window, mountRedraw) {
$window.addEventListener("hashchange", resolveRoute, false)
}
return mountRedraw.mount(root, {
mountRedraw.mount(root, {
onbeforeupdate: function() {
state = state ? 2 : 1
return !(!state || sentinel === currentResolver)
},
oncreate: resolveRoute,
onremove: onremove,
view: function() {
if (!state || sentinel === currentResolver) return
@ -172,6 +175,7 @@ module.exports = function($window, mountRedraw) {
return vnode
},
})
resolveRoute()
}
route.set = function(path, data, options) {
if (lastUpdate != null) {