Gozala / reducers

Library for higher-order manipulation of collections
MIT License
181 stars 8 forks source link

error handling #32

Closed Raynos closed 11 years ago

Raynos commented 11 years ago

It's weird to have errors swallowed and not go anywhere.

callback-reduce

For example I use callback-reduce

And i try to fold the result, and nothing happens.

I have to manually remember to capture everything to see whether an error exists.

This only applies for reducibles with one value where

callback(source, function (err, result) { ... }) may be useful (i.e. reverse callback-reduce).

toArray

var reduce = require("reducible/reduce")
var end = require("reducible/end")
var isError = require("reducible/is-error")

module.exports = toArray

function toArray(source, callback) {
    reduce(source, function(value, result) {
        // If source is `end`-ed deliver accumulated `state`.
        if (value === end) {
            return callback(null, result)
        }
        // If is source has an error, deliver that.
        else if (isError(value)) {
            return callback(value)
        }

        result.push(value)

        return result
    }, [])
}

When using this toArray function with a callback that throws the callback gets called with the thrown error which is really weird.

toArray(source, function (err) {
    err.foo
    // throws, next time called with the thrown error
})

This seems to be a case of reduce somehow try catching my reducing function and passing errors I throw back to me which is super weird. Don't do that.

Raynos commented 11 years ago

We may also want a better error handling thing for reducibles of multiple values.

Maybe fold should return EventEmitter instance and emit errors on that like a stream does.

Raynos commented 11 years ago

[Example of toArray being weird][1]

Gozala commented 11 years ago

@Raynos could you please submit test cases for few things that required work arounds like: https://github.com/Gozala/callback-reduce/blob/master/passback.js#L16-L18 https://github.com/Gozala/callback-reduce/blob/master/passback.js#L47-L55

Gozala commented 11 years ago

@Raynos could you lpese submit test cases for workarounds so this could be closed ?

Gozala commented 11 years ago

Fixed by Gozala/reducible#2