项目作者: benpptung

项目描述 :
process superagent response to get better error logging result
高级语言: JavaScript
项目地址: git://github.com/benpptung/util-superagent-serializer.git
创建时间: 2016-04-03T06:09:38Z
项目社区:https://github.com/benpptung/util-superagent-serializer

开源协议:

下载


process superagent response to get better error logging result

Example

````
const request = require(‘superagent’);
const serial = require(‘util-superagent-serializer’);

superagent.get(restful_url)
.end((err, res)=>{

  1. // superagent will detect general http level error for you(404, 403, 500...)
  2. if (err) return console.error(err);
  3. // Golden Rule: in javascript, you should trust your own input
  4. // But, you should NEVER trust 3rd party input. `res` is 3rd party input
  5. if (!isSomething(res)) return console.error(NotSomethingErr(res));
  6. // Now, you can trust the res, and continue your codes
  7. ....

});

function isSomething(res) {
// check if the res is something you want here
}

function NotSomethingErr(res) {

var err = new TypeError(‘we got something unknown’);

// serialize the res, and log the error, so we know what happened.
err.original = serial(res);
return err;
}