Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var HttpError = function(code, message){
- Error.call(this, message);
- err.code = code;
- }
- HttpError.prototype = Object.create(Error.prototype);
- HttpError.prototype.code = 500;
- HttpError.prototype.applyToRes = function(res){
- res.status(this.code).send(this.message);
- return res;
- }
- metaEngine.getAsync( id ).then( function( meta ){
- console.log( 'success!' )
- res.set( 'Content-Type', meta.mimeType )
- res.set( 'Content-Length', file.length )
- return storageEngine.getAsync( id )
- }, function(){
- console.log( 'failure!' )
- throw new HttpError(404, "Not found");
- } ).then( function( file ){
- console.log( 'still running' )
- if( file.pipe ){
- file.pipe( res )
- return
- } else if( typeof file === 'string' || Buffer.isBuffer( file ) ){
- res.send( file )
- } else {
- throw new HttpError(500, 'Invalid file returned. Please return ReadableStream, string, or Buffer' )
- }
- })
- .catch(HttpError, function(err){
- err.applyToRes(res);
- })
- .catch(function(err){
- new HttpError(500, err.message).applyToRes(res);
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement