The result of a PUT
operation against a database, is sometimes ok even if it doesn't come back as a HTTP code 200
.
In this specific case I want to treat 400
as another okay error, but struggle to come up with an elegant way of doing it.
# stream from a promise any non 200 is fail
putter = Bacon.fromPromise @_exec 'PUT', '/some/resource', {}
errors = putter.errors().mapError(errBody) # now errors are normal values
badErrors = errors.filter(statusIsnt(400)).flatMap (body) ->
return new Bacon.Error body # and now they are errors again
okErrors = errors.filter(statusIs(400)).flatMap (body) -> {}
noError = putter.mapError().filter (v) -> v? # attempt to get rid of errors
Bacon.mergeAll noError, okErrors, badErrors # combine to get result stream
I come from a promises background, and I find the above somewhat clumsy, which leads me to conclude I'm missing something. Compare:
@_exec 'PUT', '/some/resource', {}
.fail (err) ->
if err.body.status == 400 # ok
return {}
else
throw err