From a4fe06370322188b3e0abaad58ff8542d884cbf0 Mon Sep 17 00:00:00 2001 From: nishiki Date: Sun, 15 May 2016 18:30:33 +0200 Subject: [PATCH] remove error_msg in config class --- lib/mpw/config.rb | 34 +++++++++------------------------- lib/mpw/ui/cli.rb | 34 ++++++++++++++-------------------- 2 files changed, 23 insertions(+), 45 deletions(-) diff --git a/lib/mpw/config.rb b/lib/mpw/config.rb index dd797d2..11f1e5c 100644 --- a/lib/mpw/config.rb +++ b/lib/mpw/config.rb @@ -19,7 +19,6 @@ class Config # Constructor # @args: config_file -> the specify config file def initialize(config_file=nil) - @error_msg = nil @config_file = config_file if /darwin/ =~ RUBY_PLATFORM @@ -43,8 +42,7 @@ class Config def setup(key, lang, wallet_dir) if not key =~ /[a-zA-Z0-9.-_]+\@[a-zA-Z0-9]+\.[a-zA-Z]+/ - @error_msg = I18n.t('error.config.key_bad_format') - return false + raise I18n.t('error.config.key_bad_format') end if wallet_dir.empty? @@ -62,10 +60,8 @@ class Config file << config.to_yaml end - return true rescue Exception => e - @error_msg = "#{I18n.t('error.config.write')}\n#{e}" - return false + raise "#{I18n.t('error.config.write')}\n#{e}" end # Setup a new gpg key @@ -76,11 +72,9 @@ class Config # @rtrn: true if the GPG key is create, else false def setup_gpg_key(password, name, length = 4096, expire = 0) if name.nil? or name.empty? - @error_msg = "#{I18n.t('error.config.genkey_gpg.name')}" - return false + raise "#{I18n.t('error.config.genkey_gpg.name')}" elsif password.nil? or password.empty? - @error_msg = "#{I18n.t('error.config.genkey_gpg.password')}" - return false + raise "#{I18n.t('error.config.genkey_gpg.password')}" end param = '' @@ -98,31 +92,24 @@ class Config ctx = GPGME::Ctx.new ctx.genkey(param, nil, nil) - - return true rescue Exception => e - @error_msg = "#{I18n.t('error.config.genkey_gpg.exception')}\n#{e}" - return false + raise "#{I18n.t('error.config.genkey_gpg.exception')}\n#{e}" end # Check the config file # @rtrn: true if the config file is correct def checkconfig - config = YAML::load_file(@config_file) + config = YAML::load_file(@config_file) @key = config['config']['key'] @lang = config['config']['lang'] @wallet_dir = config['config']['wallet_dir'] if @key.empty? or @wallet_dir.empty? - @error_msg = I18n.t('error.config.check') - return false + raise I18n.t('error.config.check') end I18n.locale = @lang.to_sym - - return true rescue Exception => e - @error_msg = "#{I18n.t('error.config.check')}\n#{e}" - return false + raise "#{I18n.t('error.config.check')}\n#{e}" end # Check if private key exist @@ -148,11 +135,8 @@ class Config File.open(@config_file, 'w') do |file| file << config.to_yaml end - - return true rescue Exception => e - @error_msg = "#{I18n.t('error.config.write')}\n#{e}" - return false + raise "#{I18n.t('error.config.write')}\n#{e}" end end diff --git a/lib/mpw/ui/cli.rb b/lib/mpw/ui/cli.rb index 9167c4d..957005b 100644 --- a/lib/mpw/ui/cli.rb +++ b/lib/mpw/ui/cli.rb @@ -34,17 +34,13 @@ class Cli end I18n.locale = language.to_sym - if @config.setup(key, lang, wallet_dir) - puts "#{I18n.t('form.setup_config.valid')}".green - else - puts "#{I18n.t('display.error')} #8: #{@config.error_msg}".red - exit 2 - end + @config.setup(key, lang, wallet_dir) + @config.checkconfig - if not @config.checkconfig - puts "#{I18n.t('display.error')} #9: #{@config.error_msg}".red - exit 2 - end + puts "#{I18n.t('form.setup_config.valid')}".green + rescue Exception => e + puts "#{I18n.t('display.error')} #8: #{e}".red + exit 2 end # Setup a new GPG key @@ -54,8 +50,7 @@ class Cli ask = ask(I18n.t('form.setup_gpg_key.ask')).to_s if not ['Y', 'y', 'O', 'o'].include?(ask) - puts I18n.t('form.setup_gpg_key.no_create') - exit 2 + raise I18n.t('form.setup_gpg_key.no_create') end name = ask(I18n.t('form.setup_gpg_key.name')).to_s @@ -63,8 +58,7 @@ class Cli confirm = ask(I18n.t('form.setup_gpg_key.confirm_password')) {|q| q.echo = false} if password != confirm - puts I18n.t('form.setup_gpg_key.error_password') - exit 2 + raise I18n.t('form.setup_gpg_key.error_password') end length = ask(I18n.t('form.setup_gpg_key.length')).to_s @@ -76,12 +70,12 @@ class Cli puts I18n.t('form.setup_gpg_key.wait') - if @config.setup_gpg_key(password, name, length, expire) - puts "#{I18n.t('form.setup_gpg_key.valid')}".green - else - puts "#{I18n.t('display.error')} #10: #{@config.error_msg}".red - exit 2 - end + @config.setup_gpg_key(password, name, length, expire) + + puts "#{I18n.t('form.setup_gpg_key.valid')}".green + rescue Exception => e + puts "#{I18n.t('display.error')} #8: #{e}".red + exit 2 end # Setup wallet config for sync