@@ -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
7484LDP . 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
8999LDP . 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
322336LDP . 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 } ) ;
0 commit comments