diff options
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/hcl/app.rb | 13 | ||||
| -rw-r--r-- | lib/hcl/commands.rb | 26 | ||||
| -rw-r--r-- | lib/hcl/utility.rb | 5 | 
3 files changed, 23 insertions, 21 deletions
| diff --git a/lib/hcl/app.rb b/lib/hcl/app.rb index 9c176af..f164ff5 100644 --- a/lib/hcl/app.rb +++ b/lib/hcl/app.rb @@ -55,22 +55,25 @@ module HCl          else            puts show          end +      rescue CommandError => e +        $stderr.puts e +        exit 1        rescue RuntimeError => e -        STDERR.puts "Error: #{e}" +        $stderr.puts "Error: #{e}"          exit 1        rescue SocketError => e -        STDERR.puts "Connection failed. (#{e.message})" +        $stderr.puts "Connection failed. (#{e.message})"          exit 1        rescue TimesheetResource::ThrottleFailure => e -        STDERR.puts "Too many requests, retrying in #{e.retry_after+5} seconds..." +        $stderr.puts "Too many requests, retrying in #{e.retry_after+5} seconds..."          sleep e.retry_after+5          run        rescue TimesheetResource::AuthFailure => e -        STDERR.puts "Unable to authenticate: #{e}" +        $stderr.puts "Unable to authenticate: #{e}"          request_config          run        rescue TimesheetResource::Failure => e -        STDERR.puts "API failure: #{e}" +        $stderr.puts "API failure: #{e}"          exit 1        end      end diff --git a/lib/hcl/commands.rb b/lib/hcl/commands.rb index 9bd5271..da3b1f0 100644 --- a/lib/hcl/commands.rb +++ b/lib/hcl/commands.rb @@ -3,6 +3,8 @@ require 'highline'  module HCl    module Commands +    class Error < StandardError; end +      def tasks project_code=nil        tasks = Task.all        if tasks.empty? # cache tasks @@ -11,8 +13,7 @@ module HCl        end        tasks.select! {|t| t.project.code == project_code } if project_code        if tasks.empty? -        puts "No matching tasks." -        exit 1 +        fail "No matching tasks."        end        tasks.map { |task| "#{task.project.id} #{task.id}\t#{task}" }.join("\n")      end @@ -37,12 +38,10 @@ module HCl          if entry.cancel            "Deleted entry #{entry}."          else -          puts "Failed to delete #{entry}!" -          exit 1 +          fail "Failed to delete #{entry}!"          end        else -        puts 'Nothing to cancel.' -        exit 1 +        fail 'Nothing to cancel.'        end      end      alias_method :oops, :cancel @@ -64,8 +63,7 @@ module HCl          set "task.#{task_name}", *value          "Added alias @#{task_name} for #{task}."        else -        puts "Unrecognized project and task ID: #{value.inspect}" -        exit 1 +        fail "Unrecognized project and task ID: #{value.inspect}"        end      end @@ -81,8 +79,7 @@ module HCl        starting_time = get_starting_time args        task = get_task args        if task.nil? -        puts "Unknown task alias, try one of the following: ", aliases.join(', ') -        exit 1 +        fail "Unknown task alias, try one of the following: ", aliases.join(', ')        end        timer = task.start \          :starting_time => starting_time, @@ -102,8 +99,7 @@ module HCl          entry.toggle          "Stopped #{entry} (at #{current_time})"        else -        puts "No running timers found." -        exit 1 +        fail "No running timers found."        end      end @@ -117,8 +113,7 @@ module HCl            "Added note to #{entry}."          end        else -        puts "No running timers found." -        exit 1 +        fail "No running timers found."        end      end @@ -147,8 +142,7 @@ module HCl        if entry          entry.toggle        else -        puts "No matching timer found." -        exit 1 +        fail "No matching timer found."        end      end diff --git a/lib/hcl/utility.rb b/lib/hcl/utility.rb index 8026069..72b4842 100644 --- a/lib/hcl/utility.rb +++ b/lib/hcl/utility.rb @@ -1,5 +1,10 @@  module HCl +  class CommandError < StandardError; end    module Utility +    def fail message +      raise CommandError, message +    end +      def get_task_ids ident, args        if @settings.key? "task.#{ident}"          @settings["task.#{ident}"].split(/\s+/) | 
