mirror of
https://github.com/nuxt/nuxt.git
synced 2025-03-09 03:03:18 +00:00
improve error handling
This commit is contained in:
parent
771945c789
commit
cef64cdbad
@ -1,12 +1,24 @@
|
||||
const { resolve, relative, sep } = require('path')
|
||||
const _ = require('lodash')
|
||||
const PrettyError = require('pretty-error')
|
||||
const Chalk = require('chalk')
|
||||
|
||||
const pe = new PrettyError()
|
||||
|
||||
exports.logError = function (_error) {
|
||||
exports.printWarn = function (msg, from) {
|
||||
/* eslint-disable no-console */
|
||||
console.log(pe.render(_error))
|
||||
const fromStr = from ? Chalk.yellow(` ${from}\n\n`) : ' '
|
||||
|
||||
console.error('\n' + Chalk.bgYellow.black(' WARN ') + fromStr + msg + '\n')
|
||||
}
|
||||
|
||||
exports.printError = function (_error, from) {
|
||||
/* eslint-disable no-console */
|
||||
const errStr = pe.render(_error)
|
||||
const fromStr = from ? Chalk.red(` ${from}\n`) : ' '
|
||||
|
||||
console.error('\n' + Chalk.bgRed.black(' ERROR ') + fromStr)
|
||||
console.error(errStr + '\n')
|
||||
}
|
||||
|
||||
exports.encodeHtml = function encodeHtml(str) {
|
||||
|
@ -2,7 +2,13 @@ const path = require('path')
|
||||
const fs = require('fs')
|
||||
const { uniq } = require('lodash')
|
||||
const hash = require('hash-sum')
|
||||
const { chainFn, sequence } = require('../common/utils')
|
||||
const {
|
||||
chainFn,
|
||||
sequence,
|
||||
depricated,
|
||||
printError,
|
||||
printWarn
|
||||
} = require('../common/utils')
|
||||
|
||||
module.exports = class ModuleContainer {
|
||||
constructor(nuxt) {
|
||||
@ -33,7 +39,7 @@ module.exports = class ModuleContainer {
|
||||
|
||||
addTemplate(template) {
|
||||
/* istanbul ignore if */
|
||||
if (!template || typeof template !== 'string') {
|
||||
if (!template) {
|
||||
throw new Error('Invalid template:' + template)
|
||||
}
|
||||
|
||||
@ -100,16 +106,12 @@ module.exports = class ModuleContainer {
|
||||
// Type 1: String
|
||||
if (typeof moduleOpts === 'string') {
|
||||
src = moduleOpts
|
||||
}
|
||||
|
||||
// Type 2: Babel style array
|
||||
if (Array.isArray(moduleOpts)) {
|
||||
} else if (Array.isArray(moduleOpts)) {
|
||||
// Type 2: Babel style array
|
||||
src = moduleOpts[0]
|
||||
options = moduleOpts[1]
|
||||
}
|
||||
|
||||
// Type 3: Pure object
|
||||
if (typeof moduleOpts === 'object') {
|
||||
} else if (typeof moduleOpts === 'object') {
|
||||
// Type 3: Pure object
|
||||
src = moduleOpts.src
|
||||
options = moduleOpts.options
|
||||
handler = moduleOpts.handler
|
||||
@ -119,8 +121,8 @@ module.exports = class ModuleContainer {
|
||||
if (!handler) {
|
||||
try {
|
||||
handler = require(this.nuxt.resolvePath(src))
|
||||
} catch (e) {
|
||||
console.error(e) // eslint-disable-line no-console
|
||||
} catch (err) {
|
||||
printError(err)
|
||||
throw new Error('Error while resolving module: ' + src)
|
||||
}
|
||||
}
|
||||
@ -145,12 +147,11 @@ module.exports = class ModuleContainer {
|
||||
return new Promise((resolve, reject) => {
|
||||
// Prepare callback
|
||||
const cb = err => {
|
||||
// eslint-disable-next-line no-console
|
||||
console.warn(
|
||||
'[Depricated] Consider using async/await for module handlers. ' +
|
||||
'Supporting callbacks will be removed in next releases: ' +
|
||||
src
|
||||
printWarn(
|
||||
'Supporting callbacks is depricated and will be removed in next releases. Consider using async/await.',
|
||||
src
|
||||
)
|
||||
|
||||
/* istanbul ignore if */
|
||||
if (err) {
|
||||
return reject(err)
|
||||
@ -163,7 +164,7 @@ module.exports = class ModuleContainer {
|
||||
|
||||
// If module send back a promise
|
||||
if (result && result.then) {
|
||||
return result
|
||||
return resolve(result)
|
||||
}
|
||||
|
||||
// If not expecting a callback but returns no promise (=synchronous)
|
||||
|
@ -4,12 +4,11 @@ const Module = require('module')
|
||||
const { isPlainObject } = require('lodash')
|
||||
const chalk = require('chalk')
|
||||
const { Options } = require('../common')
|
||||
const { sequence } = require('../common/utils')
|
||||
const { sequence, printError, printWarn } = require('../common/utils')
|
||||
const { join, resolve } = require('path')
|
||||
const { version } = require('../../package.json')
|
||||
const ModuleContainer = require('./module')
|
||||
const Renderer = require('./renderer')
|
||||
const { logError } = require('../common/utils')
|
||||
|
||||
const debug = Debug('nuxt:')
|
||||
debug.color = 5
|
||||
@ -19,7 +18,6 @@ module.exports = class Nuxt {
|
||||
this.options = Options.from(options)
|
||||
|
||||
this.initialized = false
|
||||
this.errorHandler = this.errorHandler.bind(this)
|
||||
|
||||
// Hooks
|
||||
this._hooks = {}
|
||||
@ -36,7 +34,10 @@ module.exports = class Nuxt {
|
||||
this.renderer
|
||||
)
|
||||
|
||||
this._ready = this.ready().catch(this.errorHandler)
|
||||
this._ready = this.ready().catch(err => {
|
||||
this.callHook('error', err)
|
||||
printError(err)
|
||||
})
|
||||
}
|
||||
|
||||
static get version() {
|
||||
@ -70,21 +71,23 @@ module.exports = class Nuxt {
|
||||
}
|
||||
|
||||
plugin(name, fn) {
|
||||
// eslint-disable-next-line no-console
|
||||
console.error(
|
||||
`[Deprecated] nuxt.plugin('${name}',..) is deprecated. Please use new hooks system.`
|
||||
)
|
||||
|
||||
// A tiny backward compatibility util
|
||||
const hookMap = {
|
||||
const hook = {
|
||||
ready: 'ready',
|
||||
close: 'close',
|
||||
listen: 'listen',
|
||||
built: 'build:done'
|
||||
}
|
||||
}[name]
|
||||
|
||||
if (hookMap[name]) {
|
||||
this.hook(hookMap[name], fn)
|
||||
if (hook) {
|
||||
this.hook(hook, fn)
|
||||
printWarn(
|
||||
`nuxt.plugin('${name}',..) is deprecated. Use new hooks system.`
|
||||
)
|
||||
} else {
|
||||
throw new Error(
|
||||
`nuxt.plugin('${name}',..) is not supported. Use new hooks system.`
|
||||
)
|
||||
}
|
||||
|
||||
// Always return nuxt class which has plugin() for two level hooks
|
||||
@ -107,8 +110,7 @@ module.exports = class Nuxt {
|
||||
try {
|
||||
await sequence(this._hooks[name], fn => fn(...args))
|
||||
} catch (err) {
|
||||
console.error(`> Error on hook "${name}":`) // eslint-disable-line no-console
|
||||
console.error(err) // eslint-disable-line no-console
|
||||
printError(err, name)
|
||||
}
|
||||
}
|
||||
|
||||
@ -167,25 +169,6 @@ module.exports = class Nuxt {
|
||||
})
|
||||
}
|
||||
|
||||
errorHandler(_error) {
|
||||
// Call error hook
|
||||
this.callHook('error', _error)
|
||||
|
||||
// Silent when errorHandler is set to false
|
||||
if (this.options.errorHandler === false) {
|
||||
return
|
||||
}
|
||||
|
||||
// Custom errorHandler
|
||||
if (typeof this.options.errorHandler === 'function') {
|
||||
this.options.errorHandler(_error)
|
||||
return
|
||||
}
|
||||
|
||||
// Default handler
|
||||
logError(_error)
|
||||
}
|
||||
|
||||
resolvePath(path) {
|
||||
// Try to resolve using NPM resolve path first
|
||||
try {
|
||||
|
Loading…
Reference in New Issue
Block a user