Skip to content

process superagent response to get better error logging result

Notifications You must be signed in to change notification settings

benpptung/util-superagent-serializer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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)=>{
    
    // superagent will detect general http level error for you(404, 403, 500...)
    if (err) return console.error(err);
    
    // Golden Rule: in javascript, you should trust your own input
    // But, you should NEVER trust 3rd party input. `res` is 3rd party input
    
    if (!isSomething(res)) return console.error(NotSomethingErr(res));
    
    // Now, you can trust the res, and continue your codes
    ....
  });
  

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;
}

About

process superagent response to get better error logging result

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published