summaryrefslogtreecommitdiff
path: root/lib/chef/chef_fs/file_system/exceptions.rb
blob: 2a1baba8f52f417ffce5b96fb0054d48ff7ef7ec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#
# Author:: Thom May (<thom@chef.io>)
# Copyright:: Copyright 2012-2016, Chef Software Inc.
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

class Chef
  module ChefFS
    module FileSystem
      class FileSystemError < StandardError
        # @param entry The entry which had an issue.
        # @param cause The wrapped exception (if any).
        # @param reason A string describing why this exception happened.
        def initialize(entry, cause = nil, reason = nil)
          super(reason)
          @entry = entry
          @cause = cause
          @reason = reason
        end

        # The entry which had an issue.
        attr_reader :entry

        # The wrapped exception (if any).
        attr_reader :cause

        # A string describing why this exception happened.
        attr_reader :reason
      end

      class MustDeleteRecursivelyError < FileSystemError; end

      class NotFoundError < FileSystemError; end

      class OperationFailedError < FileSystemError
        def initialize(operation, entry, cause = nil, reason = nil)
          super(entry, cause, reason)
          @operation = operation
        end

        def message
          if cause && cause.is_a?(Net::HTTPExceptions) && cause.response.code == "400"
            "#{super} cause: #{cause.response.body}"
          else
            super
          end
        end

        attr_reader :operation
      end

      class OperationNotAllowedError < FileSystemError
        def initialize(operation, entry, cause = nil, reason = nil)
          reason ||=
            case operation
            when :delete
              "cannot be deleted"
            when :write
              "cannot be updated"
            when :create_child
              "cannot have a child created under it"
            when :read
              "cannot be read"
            end
          super(entry, cause, reason)
          @operation = operation
        end

        attr_reader :operation
        attr_reader :entry
      end

      class AlreadyExistsError < OperationFailedError; end

      class CookbookFrozenError < AlreadyExistsError; end

      class RubyFileError < OperationNotAllowedError
        def reason
          result = super
          result + " (can't safely update ruby files)"
        end
      end

      class DefaultEnvironmentCannotBeModifiedError < OperationNotAllowedError
        def reason
          result = super
          result + " (default environment cannot be modified)"
        end
      end

    end
  end
end