OSDN Git Service

use try-catch syntax for error check
authorhimetani_cafe <fumifumi@yasunaga-lab.bio.kyutech.ac.jp>
Thu, 21 Jan 2016 14:42:42 +0000 (23:42 +0900)
committerhimetani_cafe <fumifumi@yasunaga-lab.bio.kyutech.ac.jp>
Thu, 21 Jan 2016 14:42:42 +0000 (23:42 +0900)
 Please enter the commit message for your changes. Lines starting
 with '' will be ignored, and an empty message aborts the commit.
 On branch master
 Changes to be committed:
modified:   Eos.js

 Changes not staged for commit:
modified:   ../../test/mocha/Eos.test.js

server/class/Eos.js

index 63342a5..d87d1b7 100644 (file)
@@ -31,153 +31,159 @@ function Eos() {
                 comment: ''// string
             };
 
-            /**
-             * Check of command name
-             */
-            if(typeof command !== 'string') {
-                result.comment = 'Command parameter need to be string';
-                return result;
-            }
-
-            const hasCommand = Object.keys(commandReference).indexOf(command) > -1;
-            if(!hasCommand) {
-                result.comment = 'Command name is invalid';
-                return result;
-            }
-            
-            /**
-             * Check of options
-             */
+            try {
+
+                /**
+                 * Check of command name
+                 */
+                if(typeof command !== 'string') {
+                    errorMsg = 'Command parameter need to be string';
+                    throw new Error(errorMsg);
+                }
 
-            if(!(Array.isArray(options))) {
-                result.comment = 'Options need to be Array';
-                return result;
-            }
+                const hasCommand = Object.keys(commandReference).indexOf(command) > -1;
+                if(!hasCommand) {
+                    errorMsg = 'Command name is invalid';
+                    throw new Error(errorMsg);
+                }
+                
+                /**
+                 * Check of options
+                 */
+
+                if(!(Array.isArray(options))) {
+                    errorMsg = 'Options need to be Array';
+                    throw new Error(errorMsg);
+                }
 
-            
-            if(options.length === 0) {
-                result.comment = 'At least one option is required.';
-                return result;
-            }
+                
+                if(options.length === 0) {
+                    errorMsg = 'At least one option is required.';
+                    throw new Error(errorMsg);
+                }
 
-            // translate options to key-value and check whether options include correct member
-            const optionsObj = {};
-            var hasCorrectMember = true;
-            var isArgumentsArray = true;
+                // translate options to key-value and check whether options include correct member
+                const optionsObj = {};
+                var hasCorrectMember = true;
+                var isArgumentsArray = true;
 
-            options.forEach(function(o) {
-                if(!(o.name) && !(o.arguments)) {
-                    hasCorrectMember = false;
-                } else {
-                    if(Array.isArray(o.arguments)) {
-                        optionsObj[o.name] = o.arguments;
+                options.forEach(function(o) {
+                    if(!(o.name) && !(o.arguments)) {
+                        hasCorrectMember = false;
                     } else {
-                        isArgumentsArray = false;
+                        if(Array.isArray(o.arguments)) {
+                            optionsObj[o.name] = o.arguments;
+                        } else {
+                            isArgumentsArray = false;
+                        }
                     }
+                });
+
+                // check each object has proberties "name" and "argumets"
+                if(!hasCorrectMember) {
+                    errorMsg = 'Options need to include Object which have properties "name" and "arguments"';
+                    throw new Error(errorMsg);
                 }
-            });
 
-            // check each object has proberties "name" and "argumets"
-            if(!hasCorrectMember) {
-                result.comment = 'Options need to include Object which have properties "name" and "arguments"';
-                return result;
-            }
+                // check each "argumets" properties is Array
+                if(!isArgumentsArray) {
+                    errorMsg = 'Each "arguments" properties needs to be Array';
+                    throw new Error(errorMsg);
+                }
 
-            // check each "argumets" properties is Array
-            if(!isArgumentsArray) {
-                result.comment = 'Each "arguments" properties needs to be Array';
-                return result;
-            }
+                // Read OptionControlFile info of command
+                const ocf = require(__dirname + '/../../user-specific-files/OptionControlFile/commands/' + command);
+                var ocfObj = {};
 
-            // Read OptionControlFile info of command
-            const ocf = require(__dirname + '/../../user-specific-files/OptionControlFile/commands/' + command);
-            var ocfObj = {};
+                // translate ocf info to key-value
+                var notIncludingRequiredOptions = [];
+                ocf.forEach(function(o) {
+                    if(o.optionProperties && Object.keys(optionsObj).indexOf(o.option) < 0) {
+                        notIncludingRequiredOptions.push(o.option);
+                    }
+                    ocfObj[o.option] = o;
+                });
 
-            // translate ocf info to key-value
-            var notIncludingRequiredOptions = [];
-            ocf.forEach(function(o) {
-                if(o.optionProperties && Object.keys(optionsObj).indexOf(o.option) < 0) {
-                    notIncludingRequiredOptions.push(o.option);
+                // check whether all required option exist 
+                if(notIncludingRequiredOptions.length > 0) {
+                    errorMsg = 'Option ' + notIncludingRequiredOptions.toString() + ' are required';
+                    throw new Error(errorMsg);
                 }
-                ocfObj[o.option] = o;
-            });
 
-            // check whether all required option exist 
-            if(notIncludingRequiredOptions.length > 0) {
-                result.comment = 'Option ' + notIncludingRequiredOptions.toString() + ' are required';
-                return result;
-            }
+                var invalidArgumentsNumber= [];
+                var invalidArgumentType = [];
+                var invalidOutputFileName = [];
 
-            var invalidArgumentsNumber= [];
-            var invalidArgumentType = [];
-            var invalidOutputFileName = [];
+                // output file Regexp
+                const outRegExp = /out|append/;
 
-            // output file Regexp
-            const outRegExp = /out|append/;
-
-            options.forEach(function(o) {
-                // option number
-                const expectNum = ocfObj[o.name].optionNumber;
-                const actualNum = o.arguments.length;
-                if(expectNum !== actualNum) {
-                    invalidArgumentsNumber.push({name: o.name, expect: expectNum, actual: actualNum});
-                }
+                options.forEach(function(o) {
+                    // option number
+                    const expectNum = ocfObj[o.name].optionNumber;
+                    const actualNum = o.arguments.length;
+                    if(expectNum !== actualNum) {
+                        invalidArgumentsNumber.push({name: o.name, expect: expectNum, actual: actualNum});
+                    }
 
 
-                // argType and outFile name
-                o.arguments.forEach(function(arg,i) {
-                    // argType
-                    var formType = ocfObj[o.name].arg[i].formType
-                    if(formType === 'select') { // This argument is filename
-                        var exist = workspace.indexOf(arg) > -1;
-                        if(!exist) {
-                            invalidArgumentType.push({name: o.name, file: arg});
-                        }
-                    } else {
-                        const expectType = formType === 'text' ? 'string' : 'number';
-                        const actualType = typeof arg;
-                        if(expectType !== actualType) {
-                            invalidArgumentType.push({name: o.name, expect: expectType, actual: actualType});
+                    // argType and outFile name
+                    o.arguments.forEach(function(arg,i) {
+                        // argType
+                        var formType = ocfObj[o.name].arg[i].formType
+                        if(formType === 'select') { // This argument is filename
+                            var exist = workspace.indexOf(arg) > -1;
+                            if(!exist) {
+                                invalidArgumentType.push({name: o.name, file: arg});
+                            }
+                        } else {
+                            const expectType = formType === 'text' ? 'string' : 'number';
+                            const actualType = typeof arg;
+                            if(expectType !== actualType) {
+                                invalidArgumentType.push({name: o.name, expect: expectType, actual: actualType});
+                            }
                         }
-                    }
 
-                    // outFile name
-                    if(outRegExp.test(ocfObj[o.name].arg[i].argType)) {
-                        if(workspace.indexOf(o.arguments[i]) > -1) {
-                            invalidOutputFileName.push({name: o.name, file: arg});
+                        // outFile name
+                        if(outRegExp.test(ocfObj[o.name].arg[i].argType)) {
+                            if(workspace.indexOf(o.arguments[i]) > -1) {
+                                invalidOutputFileName.push({name: o.name, file: arg});
+                            }
                         }
-                    }
+                    });
                 });
-            });
 
-            // check arguments number value
-            if(invalidArgumentsNumber.length > 0) {
-                result.comment = '"arguments" properties is invalid number.\n';
-                invalidArgumentsNumber.forEach(function(i) {
-                    result.comment += '     ' + i.name + ' expect to  ' + i.expect + ', but actual ' + i.actual + '.\n';
-                });
-                return result;
-            }
+                // check arguments number value
+                if(invalidArgumentsNumber.length > 0) {
+                    errorMsg = '"arguments" properties is invalid number.\n';
+                    invalidArgumentsNumber.forEach(function(i) {
+                        errorMsg += '     ' + i.name + ' expect to  ' + i.expect + ', but actual ' + i.actual + '.\n';
+                    });
+                    throw new Error(errorMsg);
+                }
 
-            // check arguments type
-            if(invalidArgumentType.length > 0) {
-                result.comment = '"arguments" type is invalid.\n';
-                invalidArgumentType.forEach(function(i) {
-                    if(i.file) {
-                        result.comment += '     ' + i.name + ' ' + i.file + ' does not exist.\n';
-                    } else {
-                        result.comment += '     ' + i.name + ' expect to ' + i.expect + ', but actual ' + i.actual + '.\n';
-                    }
-                });
-                return result;
-            }
+                // check arguments type
+                if(invalidArgumentType.length > 0) {
+                    errorMsg = '"arguments" type is invalid.\n';
+                    invalidArgumentType.forEach(function(i) {
+                        if(i.file) {
+                            errorMsg += '     ' + i.name + ' ' + i.file + ' does not exist.\n';
+                        } else {
+                            errorMsg += '     ' + i.name + ' expect to ' + i.expect + ', but actual ' + i.actual + '.\n';
+                        }
+                    });
+                    throw new Error(errorMsg);
+                }
 
-            // check outFile name
-            if(invalidOutputFileName.length > 0) {
-                result.comment = 'output file name is invalid.\n';
-                invalidOutputFileName.forEach(function(i) {
-                    result.comment += '     ' + i.name + ' ' + i.file + ' has already existed.\n';
-                });
+                // check outFile name
+                if(invalidOutputFileName.length > 0) {
+                    errorMsg = 'output file name is invalid.\n';
+                    invalidOutputFileName.forEach(function(i) {
+                        errorMsg += '     ' + i.name + ' ' + i.file + ' has already existed.\n';
+                    });
+                    throw new Error(errorMsg);
+                }
+            } catch(e) {
+                result.message = e.message;
                 return result;
             }
 
@@ -217,7 +223,7 @@ function Eos() {
              * End in Success
              */
             result.hasDone = true;
-            result.comment = command + ' has done.';
+            result.message = command + ' has done.';
             return result;
         }
     }