jenkins_api_client2 1.9.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,246 @@
1
+ #
2
+ # Copyright (c) 2012-2013 Kannan Manickam <arangamani.kannan@gmail.com>
3
+ #
4
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ # of this software and associated documentation files (the "Software"), to deal
6
+ # in the Software without restriction, including without limitation the rights
7
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ # copies of the Software, and to permit persons to whom the Software is
9
+ # furnished to do so, subject to the following conditions:
10
+ #
11
+ # The above copyright notice and this permission notice shall be included in
12
+ # all copies or substantial portions of the Software.
13
+ #
14
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20
+ # THE SOFTWARE.
21
+ #
22
+
23
+ require 'logger'
24
+
25
+ module JenkinsApi
26
+ # This module contains classes that define exceptions for various catories.
27
+ #
28
+ module Exceptions
29
+ # This is the base class for Exceptions that is inherited from
30
+ # RuntimeError.
31
+ #
32
+ class ApiException < RuntimeError
33
+ def initialize(logger, message = "", log_level = Logger::ERROR)
34
+ logger.add(log_level) { "#{self.class}: #{message}" }
35
+ super(message)
36
+ end
37
+ end
38
+
39
+ # This exception class handles cases where parameters are expected
40
+ # but not provided.
41
+ #
42
+ class NothingSubmitted < ApiException
43
+ def initialize(logger, message = "", log_level = Logger::ERROR)
44
+ message = "Nothing is submitted." if message.empty?
45
+ super(logger, message)
46
+ end
47
+ end
48
+
49
+ # This exception class handles cases where a job not able to be created
50
+ # because it already exists.
51
+ #
52
+ class JobAlreadyExists < ApiException
53
+ def initialize(logger, message = "", log_level = Logger::ERROR)
54
+ super(logger, message)
55
+ end
56
+ end
57
+ # Support for backward compatibility
58
+ JobAlreadyExistsWithName = JobAlreadyExists
59
+
60
+ # This exception class handles cases where a view not able to be created
61
+ # because it already exists
62
+ #
63
+ class ViewAlreadyExists < ApiException
64
+ def initialize(logger, message = "", log_level = Logger::ERROR)
65
+ super(logger, message)
66
+ end
67
+ end
68
+
69
+ # This exception class handles cases where a node not able to be created
70
+ # because it already exists
71
+ #
72
+ class NodeAlreadyExists < ApiException
73
+ def initialize(logger, message = "", log_level = Logger::ERROR)
74
+ super(logger, message)
75
+ end
76
+ end
77
+
78
+ # This exception class handles cases where invalid credentials are provided
79
+ # to connect to the Jenkins.
80
+ #
81
+ class Unauthorized < ApiException
82
+ def initialize(logger, message = "", log_level = Logger::ERROR)
83
+ message = "Invalid credentials are provided." if message.empty?
84
+ super(logger, message, Logger::FATAL)
85
+ end
86
+ end
87
+ # Support for backward compatibility
88
+ UnauthorizedException = Unauthorized
89
+
90
+ # This exception class handles cases where invalid credentials are provided
91
+ # to connect to the Jenkins.
92
+ # While it is apparently used to indicate expiry of a Crumb, this is not
93
+ # the only cause of a forbidden error... maybe the user just isn't allowed
94
+ # to access the given url. We should treat forbidden as a specific "you
95
+ # are not welcome here"
96
+ #
97
+ class Forbidden < ApiException
98
+ def initialize(logger, message = "", log_level = Logger::ERROR)
99
+ msg = "Access denied. Please ensure that Jenkins is set up to allow" +
100
+ " access to this operation. #{message}"
101
+ super(logger, msg)
102
+ end
103
+ end
104
+ # Support for backward compatibility
105
+ ForbiddenException = Forbidden
106
+
107
+ # This exception should be thrown specifically when the caller has had
108
+ # a ForbiddenException and has been able to determine that a (valid)
109
+ # crumb was used, and the attempt still failed.
110
+ # This may require an interim attempt to re-acquire the crumb in order
111
+ # to confirm it has not expired.
112
+ #
113
+ # @example A condition where this exception would be raised
114
+ # def operation
115
+ # retried = false
116
+ # begin
117
+ # make_attempt
118
+ # rescue Forbidden => e
119
+ # refresh_crumbs(true)
120
+ # if @crumbs_enabled
121
+ # if !retried
122
+ # retried = true
123
+ # retry
124
+ # else
125
+ # raise ForbiddenWithCrumb.new(@logger, e.message)
126
+ # end
127
+ # else
128
+ # raise
129
+ # end
130
+ # end
131
+ # end
132
+ #
133
+ # @note the 'refresh_crumbs' method will update crumb enablement and the
134
+ # stored crumb if called with 'true'
135
+ #
136
+ class ForbiddenWithCrumb < Forbidden
137
+ def initialize(logger, message = '', log_level = Logger::ERROR)
138
+ msg = "A crumb was used in attempt to access operation. #{message}"
139
+ super(logger, msg)
140
+ end
141
+ end
142
+
143
+ # This exception class handles cases where a requested page is not found on
144
+ # the Jenkins API.
145
+ #
146
+ class NotFound < ApiException
147
+ def initialize(logger, message = "", log_level = Logger::ERROR)
148
+ msg = message.empty? ? "Requested component is not found on the" +
149
+ " Jenkins CI server." : message
150
+ super(logger, msg)
151
+ end
152
+ end
153
+ # Support for backward compatibility
154
+ NotFoundException = NotFound
155
+
156
+ # This exception class handles cases when Crumb Issues did not issue a
157
+ # crumb upon request.
158
+ #
159
+ class CrumbNotFound < NotFound
160
+ def initialize(logger, message = "", log_level = Logger::ERROR)
161
+ message = "No crumb available on the server." if message.empty?
162
+ super(logger, message)
163
+ end
164
+ end
165
+ # Support for backward compatibility
166
+ CrumbNotFoundException = CrumbNotFound
167
+
168
+ # This exception class handles cases where the requested job does not exist
169
+ # in Jenkins.
170
+ #
171
+ class JobNotFound < NotFound
172
+ def initialize(logger, message = "", log_level = Logger::ERROR)
173
+ msg = message.empty? ? "The specified job is not found" : message
174
+ super(logger, msg)
175
+ end
176
+ end
177
+
178
+ # This exception class handles cases where the requested view does not exist
179
+ # in Jenkins.
180
+ #
181
+ class ViewNotFound < NotFound
182
+ def initialize(logger, message = "", log_level = Logger::ERROR)
183
+ msg = message.empty? ? "The specified view is not found" : message
184
+ super(logger, msg)
185
+ end
186
+ end
187
+
188
+ # This exception class handles cases where the requested node does not exist
189
+ # in Jenkins.
190
+ #
191
+ class NodeNotFound < NotFound
192
+ def initialize(logger, message = "", log_level = Logger::ERROR)
193
+ msg = msg.empty? ? "The specified node is not found" : message
194
+ super(logger, msg)
195
+ end
196
+ end
197
+
198
+ # This exception class handles cases where the Jenkins API returns with a
199
+ # 500 Internal Server Error.
200
+ #
201
+ class InternalServerError < ApiException
202
+ def initialize(logger, message = "", log_level = Logger::ERROR)
203
+ message = "Internal Server Error. Perhaps the in-memory configuration" +
204
+ " Jenkins is different from the disk configuration. Please try to" +
205
+ " reload the configuration" if message.nil? || message.empty?
206
+ super(logger, message)
207
+ end
208
+ end
209
+ # Support for backward compatibility
210
+ InternalServerErrorException = InternalServerError
211
+
212
+ # This exception class handles cases where the Jenkins is getting restarted
213
+ # or reloaded where the response code returned is 503
214
+ #
215
+ class ServiceUnavailable < ApiException
216
+ def initialize(logger, message = "", log_level = Logger::ERROR)
217
+ message = "Jenkins is being reloaded or restarted. Please wait till" +
218
+ " Jenkins is completely back online. This can be" +
219
+ " programatically achieved by System#wait_for_ready" if message.empty?
220
+ super(logger, message)
221
+ end
222
+ end
223
+ # Support for backward compatibility
224
+ ServiceUnavailableException = ServiceUnavailable
225
+
226
+ # Exception occurred while running java CLI commands
227
+ #
228
+ class CLIError < ApiException
229
+ def initialize(logger, message = "", log_level = Logger::ERROR)
230
+ message = "Unable to execute the command." if message.empty?
231
+ super(logger, message)
232
+ end
233
+ end
234
+ # Support for backward compatibility
235
+ CLIException = CLIError
236
+
237
+ # Exception when a particular plugin is not found
238
+ #
239
+ class PluginNotFound < NotFound
240
+ def initialize(logger, message = "", log_level = Logger::ERROR)
241
+ message = "The specified plugin is not found" if message.empty?
242
+ super(logger, message)
243
+ end
244
+ end
245
+ end
246
+ end