Project

General

Profile

Download (1.52 KB) Statistics
| Branch: | Tag: | Revision:

hammer-cli-csv / lib / hammer_cli_csv / exception_handler.rb @ 561a8ac9

1
# Copyright 2013-2014 Red Hat, Inc.
2
#
3
# This software is licensed to you under the GNU General Public
4
# License as published by the Free Software Foundation; either version
5
# 2 of the License (GPLv2) or (at your option) any later version.
6
# There is NO WARRANTY for this software, express or implied,
7
# including the implied warranties of MERCHANTABILITY,
8
# NON-INFRINGEMENT, or FITNESS FOR A PARTICULAR PURPOSE. You should
9
# have received a copy of GPLv2 along with this software; if not, see
10
# http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
11

    
12
require 'hammer_cli/exception_handler'
13

    
14
module HammerCLICsv
15
  class ExceptionHandler < HammerCLI::ExceptionHandler
16
    def mappings
17
      super + [
18
        [Exception, :handle_csv_exception],
19
        [RestClient::Forbidden, :handle_forbidden],
20
        [RestClient::UnprocessableEntity, :handle_unprocessable_entity],
21
        [ArgumentError, :handle_argument_error]
22
      ]
23
    end
24

    
25
    protected
26

    
27
    def handle_csv_exception(e)
28
      $stderr.puts e.message
29
      log_full_error e
30
      HammerCLI::EX_DATAERR
31
    end
32

    
33
    def handle_unprocessable_entity(e)
34
      response = JSON.parse(e.response)
35
      response = response[response.keys[0]]
36

    
37
      print_error response['full_messages']
38
      HammerCLI::EX_DATAERR
39
    end
40

    
41
    def handle_argument_error(e)
42
      print_error e.message
43
      log_full_error e
44
      HammerCLI::EX_USAGE
45
    end
46

    
47
    def handle_forbidden(e)
48
      print_error 'Forbidden - server refused to process the request'
49
      log_full_error e
50
      HammerCLI::EX_NOPERM
51
    end
52
  end
53
end