Skip to content

Commit d04ed40

Browse files
author
nicola
committed
implementing ldp.stat and handle ENOENT error
1 parent fdb3690 commit d04ed40

2 files changed

Lines changed: 42 additions & 24 deletions

File tree

‎lib/ldp.js‎

Lines changed: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -69,15 +69,25 @@ function LDP(argv) {
6969
return ldp;
7070
}
7171

72-
LDP.prototype.stat = fs.stat;
72+
LDP.prototype.stat = function(file, callback) {
73+
fs.stat(file, function(err, stats) {
74+
if (err) {
75+
return callback({
76+
status: err.code === 'ENOENT' ? 404 : 500,
77+
message: err
78+
});
79+
}
80+
return callback(null, stats);
81+
});
82+
};
7383

7484
LDP.prototype.readFile = function (filename, callback) {
7585
fs.readFile(filename, {
7686
'encoding': 'utf8'
7787
}, function(err, data) {
7888
if (err) {
7989
return callback({
80-
status: 404,
90+
status: err.code === 'ENOENT' ? 404 : 500,
8191
message: "Can't read file: " + err
8292
});
8393
}
@@ -87,14 +97,18 @@ LDP.prototype.readFile = function (filename, callback) {
8797
};
8898

8999
LDP.prototype.readContainerMeta = function (directory, callback) {
100+
var ldp = this;
101+
90102
if (directory[directory.length-1] !== '/') {
91103
directory += '/';
92104
}
93-
fs.readFile(directory + this.suffixMeta, {
94-
'encoding': 'utf8'
95-
}, function(err, data) {
105+
106+
ldp.readFile(directory + ldp.suffixMeta, function(err, data) {
96107
if (err) {
97-
return callback({ status: 404, message:'Metafile not found'});
108+
return callback({
109+
status: err.status,
110+
message: 'Can\'t read metafile'
111+
});
98112
}
99113
return callback(null, data);
100114
});
@@ -118,7 +132,7 @@ LDP.prototype.listContainer = function (filename, uri, containerData, callback)
118132
fs.readdir(filename, function (err, files) {
119133
if (err) {
120134
debug.handlers("GET/HEAD -- Error reading files: " + err);
121-
return callback({ status:404, message: err });
135+
return callback({ status: 500, message: err });
122136
}
123137

124138
debug.handlers("Files in directory: " + files);
@@ -127,15 +141,15 @@ LDP.prototype.listContainer = function (filename, uri, containerData, callback)
127141
}
128142

129143
function getMetadataGraph(metaFile, fileBaseUri, callback) {
130-
fs.stat(metaFile, function(err, metaStats) {
144+
ldp.stat(metaFile, function(err, metaStats) {
131145
if (err) {
132-
return callback({ status: 404, message: err });
146+
return callback(err);
133147
}
134148

135149
if (metaStats && metaStats.isFile()) {
136-
fs.readFile(metaFile, {encoding: 'utf8'}, function(err, rawMetadata) {
150+
ldp.readFile(metaFile, function(err, rawMetadata) {
137151
if (err) {
138-
return callback({ status: 404, message: err });
152+
return callback(err);
139153
}
140154

141155
var metadataGraph = $rdf.graph();
@@ -163,7 +177,7 @@ LDP.prototype.listContainer = function (filename, uri, containerData, callback)
163177
}
164178

165179
// Get file stats
166-
fs.stat(container + file, function (err, stats) {
180+
ldp.stat(container + file, function (err, stats) {
167181
if (err) {
168182
// File does not exist, skip
169183
return callback();
@@ -257,7 +271,7 @@ LDP.prototype.listContainer = function (filename, uri, containerData, callback)
257271
async.waterfall([
258272
// add container stats
259273
function (next) {
260-
fs.stat(filename, function(err, containerStats) {
274+
ldp.stat(filename, function(err, containerStats) {
261275
if (!err) {
262276
addStats(resourceGraph, baseUri, containerStats);
263277
}
@@ -321,12 +335,12 @@ LDP.prototype.put = function (filePath, contents, callback) {
321335

322336
LDP.prototype.get = function(filename, uri, includeBody, callback) {
323337
var ldp = this;
324-
fs.stat(filename, function(err, stats) {
338+
ldp.stat(filename, function(err, stats) {
325339
// File does not exist
326340
if (err) {
327341
return callback({
328-
status: 404,
329-
message:"Can't read file: " + err
342+
status: err.status,
343+
message:"Can't read file: " + err.message
330344
});
331345
}
332346

@@ -394,7 +408,10 @@ LDP.prototype.deleteContainerMetadata = function(directory, callback) {
394408
return fs.unlink(directory + this.suffixMeta, function(err, data) {
395409
if (err) {
396410
debug.container("DELETE -- unlink() error: " + err);
397-
return callback({status:404, message: "Can't delete container: " + err });
411+
return callback({
412+
status: err.code === 'ENOENT' ? 404 : 500,
413+
message: "Can't delete container: " + err
414+
});
398415
}
399416
return callback(null, data);
400417
});
@@ -404,7 +421,10 @@ LDP.prototype.deleteResource = function(filename, callback) {
404421
return fs.unlink(filename, function(err, data) {
405422
if (err) {
406423
debug.container("DELETE -- unlink() error: " + err);
407-
return callback({status:404, message: "Can't delete container: " + err });
424+
return callback({
425+
status: err.code === 'ENOENT' ? 404 : 500,
426+
message: "Can't delete container: " + err
427+
});
408428
}
409429
return callback(null, data);
410430
});

‎test/ldp.js‎

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,22 +42,22 @@ describe('LDP', function () {
4242

4343
it ('should return content if metaFile exists', function (done) {
4444
// file can be empty as well
45-
write('', '.meta');
45+
write('This function just reads this, does not parse it', '.meta');
4646
ldp.readContainerMeta(__dirname + '/resources/', function(err, metaFile) {
4747
rm('.meta');
4848
assert.notOk(err);
49-
assert.equal(metaFile, '');
49+
assert.equal(metaFile, 'This function just reads this, does not parse it');
5050
done();
5151
});
5252
});
5353

5454
it ('should work also if trailing `/` is not passed', function (done) {
5555
// file can be empty as well
56-
write('', '.meta');
56+
write('This function just reads this, does not parse it', '.meta');
5757
ldp.readContainerMeta(__dirname + '/resources', function(err, metaFile) {
5858
rm('.meta');
5959
assert.notOk(err);
60-
assert.equal(metaFile, '');
60+
assert.equal(metaFile, 'This function just reads this, does not parse it');
6161
done();
6262
});
6363
});
@@ -100,9 +100,7 @@ describe('LDP', function () {
100100

101101
assert.notEqual(graph.statements.length, 0);
102102
assert.equal(statements.length, 8);
103-
104103
assert.notOk(err);
105-
console.log(data);
106104
done();
107105
});
108106
});

0 commit comments

Comments
 (0)