Skip to content

Commit

Permalink
feat: improved error message format
Browse files Browse the repository at this point in the history
  • Loading branch information
Justineo committed Feb 7, 2024
1 parent 5e26044 commit 013dd3a
Show file tree
Hide file tree
Showing 33 changed files with 73 additions and 57 deletions.
2 changes: 1 addition & 1 deletion packages/veui/src/components/Cascader/Cascader.vue
Original file line number Diff line number Diff line change
Expand Up @@ -628,7 +628,7 @@ export default {
{ alias: 'options', includeSelf: true }
)
if (!parents) {
throw new Error(`[veui] Unknown option`)
throw new Error('[veui-cascader] Unknown option.')
}
option = parents[parents.length - 1]
parents = parents.slice(0, -1)
Expand Down
2 changes: 1 addition & 1 deletion packages/veui/src/components/Dialog.vue
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ export default {
},
resetPosition () {
if (!this.resetDrag) {
throw new Error('The dialog is not ready for drag.')
throw new Error('[veui-dialog] The dialog is not ready for drag.')
}
this.resetDrag()
Expand Down
2 changes: 1 addition & 1 deletion packages/veui/src/components/Form/_useValidator.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ function normalizeTriggers (triggers, length) {
triggers = fill(Array(length), triggers)
} else if (!Array.isArray(triggers)) {
throw new Error(
'[veui-form] The triggers of validators must be an array or a string.'
'[veui-form] The trigger of a validator must be an array or a string.'
)
}
return triggers.map((item) => {
Expand Down
2 changes: 1 addition & 1 deletion packages/veui/src/components/Menu/_mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export default {
item.path = to
} else {
throw new Error(
'[veui-menu] Non-string `to` cannot be resolved without Vue Router.'
'[veui-nav] Non-string `to` cannot be resolved without Vue Router.'
)
}

Expand Down
2 changes: 1 addition & 1 deletion packages/veui/src/components/Tabs/Tabs.vue
Original file line number Diff line number Diff line change
Expand Up @@ -633,7 +633,7 @@ function pickFields (tab) {
function normalizeItems (items) {
return (items || []).map((item) => {
if (!item.name) {
throw new Error('[veui-tabs] The name of tab is required!')
throw new Error('[veui-tabs] Tab name is required.')
}
return {
...item,
Expand Down
2 changes: 1 addition & 1 deletion packages/veui/src/components/TimePicker/TimePicker.vue
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ export default {
checkPropValidity () {
if (this.value && !this.util.isAvailable(this.realValue, true)) {
// TODO 需要恢复到之前的状态吗
throw new Error('value prop is invalid!')
throw new Error('[veui-time-picker] `value` prop is invalid.')
}
},
scrollSelectedTime (viewport, duration) {
Expand Down
2 changes: 1 addition & 1 deletion packages/veui/src/components/TimePicker/_TimePickerUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export default class TimePickerUtil {
!datasource.length ||
datasource.some((i) => !isArray(i))
) {
throw new Error('datasource array required')
throw new Error('[veui-time-picker] Time options are not valid.')
}

this.datasource = map(datasource, (i) => i.sort((a, b) => (a > b ? 1 : -1)))
Expand Down
2 changes: 1 addition & 1 deletion packages/veui/src/components/Uploader/Uploader.vue
Original file line number Diff line number Diff line change
Expand Up @@ -635,7 +635,7 @@ export default {
.map(({ message }) => message)
.join(this.t('separator'))
file.preview = errors.some(({ preview }) => !!preview)
throw new Error('validate failed') // skip to next catch block
throw new Error('[veui] Validation failed.') // skip to next catch block
})
.then(() => {
// validate success, start to upload
Expand Down
4 changes: 2 additions & 2 deletions packages/veui/src/components/Uploader/_helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ export function getUploadRequest (options) {
return getCustomUploadRequest(options)
}
throw new Error(
'`action` is required for `xhr` or `iframe` mode and `upload` is requried for `custom` mode'
'[veui-uploader] `action` is required for `xhr` or `iframe` mode and `upload` is requried for `custom` mode.'
)
}

Expand Down Expand Up @@ -531,7 +531,7 @@ function getIframeUploadRequest (options) {
form
)
} else {
throw new Error('no matched iframe mode')
throw new Error('[veui-uploader] `iframe-mode` is invalid.')
}

// TODO: timeout ?
Expand Down
6 changes: 4 additions & 2 deletions packages/veui/src/directives/drag.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ const OPTIONS_SCHEMA = {

export function registerHandler (name, Handler, isNativeDrag) {
if (!(Handler.prototype instanceof BaseHandler)) {
throw new Error('The handler class must derive from `BaseHandler`.')
throw new Error(
'[v-drag] The handler class must derive from `BaseHandler`.'
)
}
HANDLERS[name] = { Handler, isNativeDrag }
}
Expand Down Expand Up @@ -119,7 +121,7 @@ function refresh (el, binding, vnode) {
handler = new Handler(options, contextComponent, vnode)
} else {
throw new Error(
`[v-drag] The handler type "${options.type}" is not registered.`
'[v-drag] The handler type `${options.type}` is not registered.'
)
}

Expand Down
8 changes: 4 additions & 4 deletions packages/veui/src/directives/drag/BaseHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,25 @@ export default class BaseHandler {
}

drag () {
throw new Error('`drag` method must be implemented.')
throw new Error('[v-drag] `drag` method must be implemented.')
}

end () {
this.isDragging = false
}

destroy () {
throw new Error('`destroy` method must be implemented.')
throw new Error('[v-drag] `destroy` method must be implemented.')
}

setOptions (options) {
if (this.isDragging) {
throw new Error('Do not set `options` while dragging.')
throw new Error('[v-drag] Do not set `options` while dragging.')
}
this.options = options
}

reset () {
throw new Error('`reset` method must be implemented.')
throw new Error('[v-drag] `reset` method must be implemented.')
}
}
2 changes: 1 addition & 1 deletion packages/veui/src/managers/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export class ConfigManager {
}

if (typeof key !== 'string') {
throw new Error('`Config key must be a string value.')
throw new Error('[veui-config] Config key must be a string value.')
}

let k = ns ? `${ns}.${key}` : key
Expand Down
2 changes: 1 addition & 1 deletion packages/veui/src/managers/focus.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class FocusContext {
constructor (root, env, { source, trap = false, preferred }) {
if (!root) {
throw new Error(
'Root must be specified to create a FocusContext instance.'
'[veui-focus] Root must be specified to create a `FocusContext` instance.'
)
}

Expand Down
2 changes: 1 addition & 1 deletion packages/veui/src/managers/overlay.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ function getNode (node) {
node = nodeIndex[node]

if (!node) {
throw new Error(`The given node id doesn't exist!`)
throw new Error("[veui-overlay] The given node id doesn't exist.")
}
}

Expand Down
4 changes: 3 additions & 1 deletion packages/veui/src/managers/rule.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ export class Rule {
const { name, value: ruleValue, message } = rule
let validator = this.ruleValidators[name] || rule
if (!isFunction(validator.validate)) {
throw new Error('[veui] `validate` function is required for rules')
throw new Error(
'[veui-rule] `validate` function is required for rules.'
)
}

if (!validator.validate(val, ruleValue, contextData)) {
Expand Down
4 changes: 3 additions & 1 deletion packages/veui/src/managers/simple-dialog.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ export default class SimpleDialog {
}

_show () {
throw new Error("SimpleDialog's [_show] method must be implemented.")
throw new Error(
"[veui-simple-dialog] SimpleDialog's `_show` method must be implemented."
)
}

show (content, title, options = {}) {
Expand Down
10 changes: 5 additions & 5 deletions packages/veui/src/managers/ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ function addPropValue (component, prop, value) {

if (!ui || !ui[prop]) {
throw new Error(
`[veui] Unknown ui prop "${prop}" of component "${component}".`
`[veui-ui] Unknown ui prop \`${prop}\` of component \`${component}\`.`
)
}

const { values } = ui[prop]
if (!values) {
throw new Error(
`[veui] ui prop "${prop}" of component "${component}" is not a enum prop.`
`[veui-ui] ui prop \`${prop}\` of component \`${component}\` is not a enum prop.`
)
}

Expand All @@ -41,20 +41,20 @@ function setPropDefault (component, prop, value) {

if (!ui || !ui[prop]) {
throw new Error(
`[veui] Unknown ui prop "${prop}" of component "${component}".`
`[veui-ui] Unknown ui prop \`${prop}\` of component \`${component}\`.`
)
}

const { values } = ui[prop]
if (!values) {
throw new Error(
`[veui] ui prop "${prop}" of component "${component}" is not a enum prop. Only enum props can have default values.`
`[veui-ui] ui prop \`${prop}\` of component \`${component}\` is not a enum prop. Only enum props can have default values.`
)
}

if (values.indexOf(value) === -1) {
throw new Error(
`[veui] ui prop "${prop}" of component "${component}" does not have value "${value}".`
`[veui-ui] ui prop \`${prop}\` of component \`${component}\` does not have value \`${value}\`.`
)
}

Expand Down
8 changes: 4 additions & 4 deletions packages/veui/src/mixins/controllable.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ let options = {
return
}
throw new Error(
`[controllable] Unkown prop key: '${prop}' on committing.`
`[veui-controllable] Unkown prop key: \`${prop}\` on committing.`
)
}
}
}

const errorMsg =
'[controllable] prop config must be either a string, an object or an object array.'
'[veui-controllable] Prop config must be either a string, an object or an object array.'

/**
* 自动将对应的 prop 转换成受控的
Expand Down Expand Up @@ -75,15 +75,15 @@ export default function useControllable (props) {
result.computed[getRealName(def)] = {
get () {
if (get === false) {
throw new Error("[controllable] Can't access disabled getter!")
throw new Error("[veui-controllable] Can't access disabled getter.")
}
return computedGetter(this, def)
},
set (value) {
// 一般可以用来禁用 assignment,而强制使用 vm.commit !
// 对于 v-model/.sync,这个 set 还是有点用处
if (set === false) {
throw new Error("[controllable] Can't access disabled setter!")
throw new Error("[veui-controllable] Can't access disabled setter.")
}
return computedSetter(this, value, def)
}
Expand Down
6 changes: 3 additions & 3 deletions packages/veui/src/mixins/coupled.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ export function useChild (type, parentType, fields, { direct = false } = {}) {
},
coupledProxy () {
if (!Array.isArray(fields)) {
throw new Error('[veui-coupled-child] `fields` must be an array.')
throw new Error('[veui-coupled] `fields` must be an array.')
}

return fields.reduce(
Expand All @@ -164,12 +164,12 @@ export function useChild (type, parentType, fields, { direct = false } = {}) {
: fn
} else {
throw new Error(
'[veui-coupled-child] `fields` mapper must either be a string or a function'
'[veui-coupled] `fields` mapper must either be a string or a function.'
)
}
} else {
throw new Error(
'[veui-coupled-child] `fields` item must either be a string, or a tuple of length 2 or 3.'
'[veui-coupled] `fields` item must either be a string, or a tuple of length 2 or 3.'
)
}

Expand Down
2 changes: 1 addition & 1 deletion packages/veui/src/mixins/input.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export function useInput () {
},
computed: {
realName () {
return (this.field && this.field.getName()) || this.name
return this.name || (this.field && this.field.getName())
},
realDisabled () {
return Boolean(
Expand Down
2 changes: 1 addition & 1 deletion packages/veui/src/mixins/key-select.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ function toggleSelector (elem, selector, force) {
}
} else {
throw new Error(
'focusSelector only accepts a simple class selector (.foo) or an attribute existence selector ([foo]).'
'[veui-key-select] `focusSelector` only accepts a simple class selector (`.foo`) or an attribute existence selector (`[foo]`).'
)
}
}
Expand Down
4 changes: 3 additions & 1 deletion packages/veui/src/mixins/overlay.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ export default {
},
relocate () {
if (!this.$refs.overlay) {
throw new Error('Can not find [this.$refs.overlay] to relocate')
throw new Error(
'[veui-overlay] Can not find `this.$refs.overlay` to relocate.'
)
}
this.$refs.overlay.relocate()
}
Expand Down
4 changes: 2 additions & 2 deletions packages/veui/src/mixins/searchable.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ function search (datasource, keyword, options, result = []) {
let isBool = !isArray && typeof offsets === 'boolean'
if (!isArray && !isBool) {
throw new Error(
'The return value of the `match` function must either be a boolean or an array.'
'[veui-searchable] The return value of the `match` function must either be a boolean or an array.'
)
}
itemWrap.matched = toBoolean(offsets)
Expand All @@ -112,7 +112,7 @@ function search (datasource, keyword, options, result = []) {
let filtered = filterFn(item, keyword, { ancestors, offsets })
if (typeof filtered !== 'boolean') {
throw new Error(
'The return value of the `filter` function must be a boolean.'
'[veui-searchable] The return value of the `filter` function must be a boolean.'
)
}
itemWrap.filtered = filtered
Expand Down
4 changes: 3 additions & 1 deletion packages/veui/src/utils/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,9 @@ export function getIndexOfType (current, parentType) {
: current.$parent

if (parentType && !parent) {
throw new Error(`No ancestor typed as [${parentType}] found.`)
throw new Error(
`[veui-context] No ancestor typed as \`${parentType}\` found.`
)
}

let parentVnodes = parent.$slots.default
Expand Down
4 changes: 2 additions & 2 deletions packages/veui/src/utils/date.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ export function startOf (base, startOf, { weekStartsOn } = {}) {
const impl = START_OF_FN_MAP[startOf]

if (!impl) {
throw new Error(`[veui] Invalid unit for \`startOf\`: ${startOf}`)
throw new Error(`[veui-date] Invalid unit for \`startOf\`: ${startOf}.`)
}

if (startOf === 'week') {
Expand All @@ -152,7 +152,7 @@ export function add (base, offset) {
const impl = ADD_FN_MAP[key] || ADD_FN_MAP[`${key}s`]

if (!impl) {
throw new Error(`[veui] Invalid unit for \`add\`: ${key}`)
throw new Error(`[veui-date] Invalid unit for \`add\`: ${key}.`)
}

if (offset[key] !== 0) {
Expand Down
8 changes: 5 additions & 3 deletions packages/veui/src/utils/dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -583,7 +583,7 @@ export function scrollToAlign (viewport, target, options) {
options = {}
} else {
throw new Error(
'the third argument of `scrollToAlign` must be a object or an array or a number'
'[veui-dom] The third argument of `scrollToAlign` must be a object or an array or a number.'
)
}

Expand Down Expand Up @@ -630,7 +630,7 @@ export function scrollTo (viewport, options) {
options = {}
}
if (!isNumber(left) || !isNumber(top)) {
throw new Error('left and top must be numbers')
throw new Error('[veui-dom] `left` and `top` must be numbers.')
}

let distanceLeft = left - scrollLeft
Expand Down Expand Up @@ -951,7 +951,9 @@ function getStableOffset (el, context) {
*/
export function getScrollOffset (el, context) {
if (!context.contains(el)) {
throw new Error('The context element must contain the starting element.')
throw new Error(
'[veui-dom] The context element must contain the starting element.'
)
}

let current = el
Expand Down
Loading

0 comments on commit 013dd3a

Please sign in to comment.