Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Option to convert 4xx and 5xx to errors #82

Closed
seanrucker opened this issue Apr 23, 2015 · 1 comment · Fixed by #164
Closed

Option to convert 4xx and 5xx to errors #82

seanrucker opened this issue Apr 23, 2015 · 1 comment · Fixed by #164
Assignees
Milestone

Comments

@seanrucker
Copy link

@seanrucker seanrucker commented Apr 23, 2015

I'm using Wreck with async to perform a series of network calls. In each function I have to write boilerplate code to check the response.statusCode and convert it to a Boom error. If Wreck could do that automatically it would allow me to rewrite something like this:

async.series([
  function(callback) {
    Wreck.get('https://google.com/first', function(err, response, payload) {
      if (err) {
        return callback(err)
      }

      if (response.statusCode >= 400) {
        err = Boom.create(response.statusCode, payload.message)
        return callback(err)
      }

      // do something with first payload
      callback()
    })
  },
  function(callback) {
    Wreck.get('https://google.com/second', function(err, response, payload) {
      if (err) {
        return callback(err)
      }

      if (response.statusCode >= 400) {
        err = Boom.create(response.statusCode, payload.message)
        return callback(err)
      }

      // do something with second payload
      callback()
    })
  },
  function(callback) {
    Wreck.get('https://google.com/third', function(err, response, payload) {
      if (err) {
        return callback(err)
      }

      if (response.statusCode >= 400) {
        err = Boom.create(response.statusCode, payload.message)
        return callback(err)
      }

      // do something  with third payload
      callback()
    })
  }
], function(err) {
  // done
})

to this:

var opts = { autoConvertErrors: true }
async.waterfall([
  function(callback) {
    Wreck.get('https://google.com/first', opts, callback)
  },
  function(response, payload, callback) {
    // do something with first payload
    Wreck.get('https://google.com/second', opts, callback)
  },
  function(response, payload, callback) {
    // do something with second payload
    Wreck.get('https://google.com/third', opts, callback)
  },
  function(response, payload, callback) {
    // do something with third payload
    callback()
  }
], function(err) {
  // done
})
@geek geek added the request label Sep 25, 2015
@nlf

This comment has been minimized.

Copy link
Member

@nlf nlf commented Mar 21, 2017

@geek plan to implement this? if not we should close it. fwiw, it seems like a reasonable idea to me

@geek geek added breaking changes feature and removed request labels Mar 21, 2017
@geek geek added this to the 11.0.0 milestone Mar 21, 2017
@geek geek self-assigned this Mar 21, 2017
@geek geek closed this in #158 Mar 21, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants
You can’t perform that action at this time.