From e2839d136c8941a43b8ab83e4b097633bb5ab1c2 Mon Sep 17 00:00:00 2001 From: nishiki Date: Thu, 16 Jan 2014 21:24:08 +0100 Subject: [PATCH] clean error message with the server --- i18n/en.yml | 6 ++++++ i18n/fr.yml | 10 ++++++++-- lib/Server.rb | 49 ++++++++++++++++++++++--------------------------- lib/Sync.rb | 47 ++++++++++++++++++++++------------------------- 4 files changed, 58 insertions(+), 54 deletions(-) diff --git a/i18n/en.yml b/i18n/en.yml index 7805f04..22f2f7b 100644 --- a/i18n/en.yml +++ b/i18n/en.yml @@ -19,6 +19,7 @@ en: name_empty: "You must define a name!" sync: connection: "Connection fail!" + communication: "A communication problem with the server is appeared!" no_data: "Nothing data!" not_authorized: "You haven't the access to remote file!" unknown: "An unknown error is occured!" @@ -117,6 +118,11 @@ en: connect: "Connection to:" nothing: "Nothing result!" server: + error: + client: + unknown: "An unknown error is appeared!" + no_authorized: "You haven't access to remote file!" + no_data: "Nothing data has been sent!" option: usage: "Usage" config: "Specifie the configuration file" diff --git a/i18n/fr.yml b/i18n/fr.yml index 9005238..b14aa99 100644 --- a/i18n/fr.yml +++ b/i18n/fr.yml @@ -18,8 +18,9 @@ fr: update: name_empty: "Vous devez définir un nom!" sync: - connection: "La connexion n'a pu être établie" - no_data: "Aucune data!" + connection: "La connexion n'a pu être établie!" + communication: "Un problème de communication avec le serveur est apparu!" + no_data: "Aucune donnée!" not_authorized: "Vous n'avez pas les autorisations d'accès au fichier distant!" unknown: "Une erreur inconnue est survenue!" cli: @@ -117,6 +118,11 @@ fr: connect: "Connexion à:" nothing: "Aucun résultat!" server: + error: + client: + unknown: "Une erreur inconnue est apparue!" + no_authorized: "Vous n'avez pas les accès au fichier distant!" + no_data: "Aucune donnée n'a été transmise!" option: usage: "Utilisation" config: "Spécifie le fichier de configuration" diff --git a/lib/Server.rb b/lib/Server.rb index 5b76de7..267105f 100644 --- a/lib/Server.rb +++ b/lib/Server.rb @@ -43,7 +43,10 @@ class Server when 'close' closeConnection(client) else - client.puts 'Unknown command' + send_msg = {:action => 'unknown', + :gpg_key => msg['gpg_key'], + :error => 'server.error.client.unknown'} + client.puts send_msg closeConnection(client) end end @@ -72,20 +75,18 @@ class Server if isAuthorized?(msg['password'], salt, hash) send_msg = {:action => 'get', :gpg_key => msg['gpg_key'], - :msg => 'done', - :data => data} + :data => data, + :error => nil} else send_msg = {:action => 'get', :gpg_key => msg['gpg_key'], - :msg => 'fail', - :error => 'not_authorized'} + :error => 'server.error.client.no_authorized'} end else - send_msg = {:action => 'get', - :gpg_key => msg['gpg_key'], - :data => '', - :msg => 'fail', - :error => 'file_not_exist'} + send_msg = {:action => 'get', + :gpg_key => msg['gpg_key'], + :data => '', + :error => nil} end return send_msg.to_json @@ -101,8 +102,7 @@ class Server if data.nil? || data.empty? send_msg = {:action => 'update', :gpg_key => msg['gpg_key'], - :msg => 'fail', - :error => 'no_data'} + :error => 'server.error.client.no_data'} return send_msg.to_json end @@ -133,20 +133,18 @@ class Server file << config.to_yaml end - send_msg = {:action => 'update', - :gpg_key => msg['gpg_key'], - :msg => 'done'} + send_msg = {:action => 'update', + :gpg_key => msg['gpg_key'], + :error => nil} rescue Exception => e send_msg = {:action => 'update', :gpg_key => msg['gpg_key'], - :msg => 'fail', - :error => 'server_error'} + :error => 'server.error.client.unknown'} end else send_msg = {:action => 'update', :gpg_key => msg['gpg_key'], - :msg => 'fail', - :error => 'not_autorized'} + :error => 'server.error.client.no_authorized'} end return send_msg.to_json @@ -167,8 +165,7 @@ class Server if !File.exist?(file_gpg) send_msg = {:action => 'delete', :gpg_key => msg['gpg_key'], - :msg => 'delete_fail', - :error => 'file_not_exist'} + :error => nil} return send_msg.to_json end @@ -183,18 +180,16 @@ class Server send_msg = {:action => 'delete', :gpg_key => msg['gpg_key'], - :msg => 'delete_done'} + :error => nil} rescue Exception => e send_msg = {:action => 'delete', :gpg_key => msg['gpg_key'], - :msg => 'delete_fail', - :error => e} + :error => 'server.error.client.unknown'} end else send_msg = {:action => 'delete', :gpg_key => msg['gpg_key'], - :msg => 'delete_fail', - :error => 'not_autorized'} + :error => 'server.error.client.no_authorized'} end return send_msg.to_json @@ -285,7 +280,7 @@ class Server file << config.to_yaml end rescue Exception => e - puts "#{I18n.t('server.formsetup.not_valid')}\n#{e}" + puts "#{I18n.t('server.form.setup.not_valid')}\n#{e}" return false end diff --git a/lib/Sync.rb b/lib/Sync.rb index 0c3ed31..a02a08d 100644 --- a/lib/Sync.rb +++ b/lib/Sync.rb @@ -55,16 +55,18 @@ class Sync return nil end - send_msg = {:action => 'get', - :gpg_key => @gpg_key, - :password => @password, - :suffix => @suffix} + send_msg = {:action => 'get', + :gpg_key => @gpg_key, + :password => @password, + :suffix => @suffix} @socket.puts send_msg.to_json msg = JSON.parse(@socket.gets) - case msg['error'] - when nil, 'file_not_exist' + if !msg['error'] + @error_msg = I18n.t('error.sync.communication') + return nil + elsif msg['error'].nil? tmp_file = "/tmp/mpw-#{MPW.generatePassword()}.gpg" File.open(tmp_file, 'w') do |file| file << msg['data'] @@ -76,15 +78,12 @@ class Sync end File.unlink(tmp_file) - return @mpw.search() - when 'not_authorized' - @error_msg = "#{I18n.t('error.sync.not_authorized')}\n#{e}" else - @error_msg = "#{I18n.t('error.sync.unknown')}\n#{e}" + @error_msg = I18n.t(msg['error']) + return nil end - return nil end # Update the remote data @@ -95,29 +94,27 @@ class Sync return true end - send_msg = {:action => 'update', - :gpg_key => @gpg_key, - :password => @password, - :suffix => @suffix, - :data => data} + send_msg = {:action => 'update', + :gpg_key => @gpg_key, + :password => @password, + :suffix => @suffix, + :data => data} @socket.puts send_msg.to_json msg = JSON.parse(@socket.gets) - case msg['error'] - when nil + if !msg['error'] + @error_msg = I18n.t('error.sync.communication') + return false + elsif msg['error'].nil? return true - when 'not_authorized' - @error_msg = "#{I18n.t('error.sync.not_authorized')}\n#{e}" - when 'no_data' - @error_msg = "#{I18n.t('error.sync.no_data')}\n#{e}" else - @error_msg = "#{I18n.t('error.sync.unknown')}\n#{e}" + @error_msg = I18n.t(msg['error']) + return false end - - return false end + # TODO def delete() end