Skip to content

Commit 5c6c4f5

Browse files
Add a useRawError option to have the raw error object be passed to the error callback
1 parent 2ff5363 commit 5c6c4f5

File tree

3 files changed

+26
-8
lines changed

3 files changed

+26
-8
lines changed

Diff for: README.md

+16-4
Original file line numberDiff line numberDiff line change
@@ -204,21 +204,33 @@ new Vue({
204204

205205
By default, in case of a rejected promise in an async computed property, vue-async-computed will take care of logging the error for you.
206206

207-
If you want to use a custom logging function, the plugin takes an `errorHandler` option, which should be the function you want called with the error information.
207+
If you want to use a custom logging function, the plugin takes an `errorHandler` option, which should be the function you want called with the error information. By default, it will be called with the error's stack trace as an argument, but if you want the raw error itself you can set the
208+
`useRawError` option to `true`.
208209

209210
For example:
210211

211212
````js
212213
Vue.use(AsyncComputed, {
213-
errorHandler (msg) {
214+
errorHandler (stack) {
214215
console.log('Hey, an error!')
215216
console.log('---')
216-
console.log(msg)
217+
console.log(stack)
218+
}
219+
)
220+
221+
// Or with `useRawError`:
222+
Vue.use(AsyncComputed, {
223+
useRawError: true,
224+
errorHandler (err) {
225+
console.log('An error occurred!')
226+
console.log('The error message was: ' + err.msg)
227+
console.log('And the stack trace was:')
228+
console.log(err.stack)
217229
}
218230
)
219231
````
220232

221-
You can pass `false` in order to silently ignore rejected promises.
233+
You can pass `false` as the `errorHandler` in order to silently ignore rejected promises.
222234

223235
## License
224236

Diff for: src/index.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,11 @@ const AsyncComputed = {
5151
? console.error.bind(console, 'Error evaluating async computed property:')
5252
: options.errorHandler
5353

54-
handler(err.stack)
54+
if (options.useRawError) {
55+
handler(err)
56+
} else {
57+
handler(err.stack)
58+
}
5559
})
5660
}, { immediate: true })
5761
})

Diff for: test/index.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ let baseErrorCallback = () => {
99
let testErrorCallback = baseErrorCallback
1010

1111
Vue.use(AsyncComputed, {
12+
useRawError: true,
1213
errorHandler: msg => testErrorCallback(msg)
1314
})
1415

@@ -155,17 +156,18 @@ test("Having only sync computed data still works", t => {
155156
})
156157

157158
test("Handle errors in computed properties", t => {
158-
t.plan(2)
159+
t.plan(3)
159160
const vm = new Vue({
160161
asyncComputed: {
161162
a () {
162-
return Promise.reject("error")
163+
return Promise.reject('error')
163164
}
164165
}
165166
})
166167
t.equal(vm.a, null)
167-
testErrorCallback = msg => {
168+
testErrorCallback = err => {
168169
t.equal(vm.a, null)
170+
t.equal(err, 'error')
169171
testErrorCallback = baseErrorCallback
170172
}
171173
})

0 commit comments

Comments
 (0)