Advertisement
Guest User

Untitled

a guest
Nov 24th, 2014
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var HttpError = function(code, message){
  2.   Error.call(this, message);
  3.   err.code = code;
  4. }
  5. HttpError.prototype = Object.create(Error.prototype);
  6. HttpError.prototype.code = 500;
  7. HttpError.prototype.applyToRes = function(res){
  8.    res.status(this.code).send(this.message);
  9.    return res;
  10. }
  11.  
  12.   metaEngine.getAsync( id ).then( function( meta ){
  13.     console.log( 'success!' )
  14.     res.set( 'Content-Type', meta.mimeType )
  15.     res.set( 'Content-Length', file.length )
  16.     return storageEngine.getAsync( id )
  17.   }, function(){
  18.     console.log( 'failure!' )
  19.     throw new HttpError(404, "Not found");
  20.   } ).then( function( file ){
  21.     console.log( 'still running' )
  22.     if( file.pipe ){
  23.       file.pipe( res )
  24.       return
  25.     } else if( typeof file === 'string' || Buffer.isBuffer( file ) ){
  26.       res.send( file )
  27.     } else {
  28.       throw new HttpError(500, 'Invalid file returned. Please return ReadableStream, string, or Buffer' )
  29.     }
  30.   })
  31.   .catch(HttpError, function(err){
  32.     err.applyToRes(res);
  33.   })
  34.   .catch(function(err){
  35.     new HttpError(500, err.message).applyToRes(res);
  36.   });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement