Skip to content

Commit

Permalink
Hack error processing where possible.
Browse files Browse the repository at this point in the history
  • Loading branch information
Tomohito Nakayama committed Feb 21, 2016
1 parent 649d8cd commit 6be1b50
Showing 1 changed file with 30 additions and 21 deletions.
51 changes: 30 additions & 21 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ function io( filePath, userProcess, jb, chainedProcess){
'w+',
(err,fd) => {

if(err) raiseError(null, 'IOError Failed to create file.') ;
if(err) raiseError( userProcess._plannedExecuter, 'IOError Failed to create file.') ;

//save to file and apply process.
save(
Expand All @@ -233,7 +233,7 @@ function io( filePath, userProcess, jb, chainedProcess){
fs.close(
fd,
function(err){
if(err) raiseError(null, "io:failsed to close file");
if(err) raiseError( userProcess._plannedExecuter, "io:failsed to close file");
afterCloseProcess();
}
);
Expand Down Expand Up @@ -305,11 +305,11 @@ function process(
encoding(jb),
(err, data) => {

if (err) raiseError(null, 'IOError Failed to read file.',err);
if (err) raiseError( userProcess._plannedExecuter, 'IOError Failed to read file.',err);
fs.close(
fd,
function(err){
if(err) raiseError(null, 'IOError Failed to close file',err);
if(err) raiseError( userProcess._plannedExecuter, 'IOError Failed to close file',err);

var json = decode( data, jb);
apply(
Expand Down Expand Up @@ -353,34 +353,43 @@ function filternize( userProcess ){

}

function wrapUserProcess( userProcess, functionWrapper ){

//To keep _plannedExecuter hack working, this function is needed to use when wrapping userProcess.
let wrapped = functionWrapper( userProcess );
wrapped._plannedExecuter = userProcess._plannedExecuter;//hack to emit error from userProcess by executer.
function guardProcess( userProcess ){

return wrapped;
let guarded =
wrapUserProcess(
userProcess,
function( userProcess ){
return function( json, filePath){
try{
return userProcess( json, filePath);

}catch(err){
//walkaround for unclear this/caller problem in javascript.
//I just wanted to use something like "this".
raiseError( userProcess._plannedExecuter, 'User process error', err);

}
};
}
);

return guarded;

}

function guardProcess(userProcess){

let guarded =
function( json, filePath){
try{
return userProcess( json,filePath);
}catch(err){
//walkaround for unclear this/caller problem in javascript.
//I just wanted to use something like "this".
raiseError( userProcess._plannedExecuter, 'User process error', err);
function wrapUserProcess( userProcess, functionWrapper ){

}
};
//To keep _plannedExecuter hack working, this function is needed to use when wrapping userProcess.
let wrapped = functionWrapper( userProcess );
wrapped._plannedExecuter = userProcess._plannedExecuter;//hack to emit error from userProcess by executer.

return guarded;
return wrapped;

}


//apply process function to json.
function apply( process, json, file, closeFile, jb, filePath, postProcess, chainedProcess){

Expand Down

0 comments on commit 6be1b50

Please sign in to comment.