0
Fork 0
mirror of https://github.com/verdaccio/verdaccio.git synced 2024-12-16 21:56:25 -05:00

handing JSON.parse errors

This commit is contained in:
Alex Kocharin 2013-10-19 00:46:13 +04:00
parent 674f944942
commit 012892600b
2 changed files with 39 additions and 23 deletions

View file

@ -129,8 +129,15 @@ Storage.prototype.read = function(name, cb) {
Storage.prototype.read_json = function(name, cb) {
read(this.path + '/' + name, function(err, res) {
if (err) return cb(err);
cb(null, JSON.parse(res.toString('utf8')));
});
var args = []
try {
args = [null, JSON.parse(res.toString('utf8'))]
} catch(err) {
args = [err]
}
cb.apply(null, args)
})
}
Storage.prototype.create = function(name, value, cb) {

View file

@ -1,22 +1,24 @@
var fs = require('fs');
var semver = require('semver');
var Path = require('path');
var fs_storage = require('./local-fs');
var UError = require('./error').UserError;
var utils = require('./utils');
var mystreams = require('./streams');
var info_file = 'package.json';
var fs = require('fs')
, semver = require('semver')
, Path = require('path')
, fs_storage = require('./local-fs')
, UError = require('./error').UserError
, utils = require('./utils')
, mystreams = require('./streams')
, Logger = require('./logger')
, info_file = 'package.json'
//
// Implements Storage interface
// (same for storage.js, local-storage.js, up-storage.js)
//
function Storage(config) {
if (!(this instanceof Storage)) return new Storage(config);
this.config = config;
var path = Path.resolve(Path.dirname(this.config.self_path), this.config.storage);
this.storage = new fs_storage(path);
return this;
if (!(this instanceof Storage)) return new Storage(config)
this.config = config
var path = Path.resolve(Path.dirname(this.config.self_path), this.config.storage)
this.storage = new fs_storage(path)
this.logger = Logger.logger.child({sub: 'fs'})
return this
}
// returns the minimal package file
@ -238,15 +240,22 @@ Storage.prototype.get_tarball = function(name, filename, callback) {
}
Storage.prototype.get_package = function(name, callback) {
this.storage.read_json(name + '/' + info_file, function(err, result) {
if (err && err.code === 'ENOENT') {
return callback(new UError({
status: 404,
msg: 'no such package available'
}));
var self = this
, file = name + '/' + info_file
self.storage.read_json(file, function(err, result) {
if (err) {
if (err.code === 'ENOENT') {
return callback(new UError({
status: 404,
msg: 'no such package available'
}))
} else {
self.logger.error({err: err, file: file}, 'error reading file @{file}: @{!err.message}')
}
}
callback.apply(null, arguments);
});
callback.apply(null, arguments)
})
}
module.exports = Storage;