lhj-tools 0.1.25 → 0.1.26

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,226 @@
1
+ require 'logger'
2
+
3
+ module Lhj
4
+ module JenkinsApi
5
+ # This module contains classes that define exceptions for various catories.
6
+ #
7
+ module Exceptions
8
+ # This is the base class for Exceptions that is inherited from
9
+ # RuntimeError.
10
+ #
11
+ class ApiException < RuntimeError
12
+ def initialize(logger, message = "", log_level = Logger::ERROR)
13
+ logger.add(log_level) { "#{self.class}: #{message}" }
14
+ super(message)
15
+ end
16
+ end
17
+
18
+ # This exception class handles cases where parameters are expected
19
+ # but not provided.
20
+ #
21
+ class NothingSubmitted < ApiException
22
+ def initialize(logger, message = "", log_level = Logger::ERROR)
23
+ message = "Nothing is submitted." if message.empty?
24
+ super(logger, message)
25
+ end
26
+ end
27
+
28
+ # This exception class handles cases where a job not able to be created
29
+ # because it already exists.
30
+ #
31
+ class JobAlreadyExists < ApiException
32
+ def initialize(logger, message = "", log_level = Logger::ERROR)
33
+ super(logger, message)
34
+ end
35
+ end
36
+ # Support for backward compatibility
37
+ JobAlreadyExistsWithName = JobAlreadyExists
38
+
39
+ # This exception class handles cases where a view not able to be created
40
+ # because it already exists
41
+ #
42
+ class ViewAlreadyExists < ApiException
43
+ def initialize(logger, message = "", log_level = Logger::ERROR)
44
+ super(logger, message)
45
+ end
46
+ end
47
+
48
+ # This exception class handles cases where a node not able to be created
49
+ # because it already exists
50
+ #
51
+ class NodeAlreadyExists < ApiException
52
+ def initialize(logger, message = "", log_level = Logger::ERROR)
53
+ super(logger, message)
54
+ end
55
+ end
56
+
57
+ # This exception class handles cases where invalid credentials are provided
58
+ # to connect to the Jenkins.
59
+ #
60
+ class Unauthorized < ApiException
61
+ def initialize(logger, message = "", log_level = Logger::ERROR)
62
+ message = "Invalid credentials are provided." if message.empty?
63
+ super(logger, message, Logger::FATAL)
64
+ end
65
+ end
66
+ # Support for backward compatibility
67
+ UnauthorizedException = Unauthorized
68
+
69
+ # This exception class handles cases where invalid credentials are provided
70
+ # to connect to the Jenkins.
71
+ # While it is apparently used to indicate expiry of a Crumb, this is not
72
+ # the only cause of a forbidden error... maybe the user just isn't allowed
73
+ # to access the given url. We should treat forbidden as a specific "you
74
+ # are not welcome here"
75
+ #
76
+ class Forbidden < ApiException
77
+ def initialize(logger, message = "", log_level = Logger::ERROR)
78
+ msg = "Access denied. Please ensure that Jenkins is set up to allow" +
79
+ " access to this operation. #{message}"
80
+ super(logger, msg)
81
+ end
82
+ end
83
+ # Support for backward compatibility
84
+ ForbiddenException = Forbidden
85
+
86
+ # This exception should be thrown specifically when the caller has had
87
+ # a ForbiddenException and has been able to determine that a (valid)
88
+ # crumb was used, and the attempt still failed.
89
+ # This may require an interim attempt to re-acquire the crumb in order
90
+ # to confirm it has not expired.
91
+ #
92
+ # @example A condition where this exception would be raised
93
+ # def operation
94
+ # retried = false
95
+ # begin
96
+ # make_attempt
97
+ # rescue Forbidden => e
98
+ # refresh_crumbs(true)
99
+ # if @crumbs_enabled
100
+ # if !retried
101
+ # retried = true
102
+ # retry
103
+ # else
104
+ # raise ForbiddenWithCrumb.new(@logger, e.message)
105
+ # end
106
+ # else
107
+ # raise
108
+ # end
109
+ # end
110
+ # end
111
+ #
112
+ # @note the 'refresh_crumbs' method will update crumb enablement and the
113
+ # stored crumb if called with 'true'
114
+ #
115
+ class ForbiddenWithCrumb < Forbidden
116
+ def initialize(logger, message = '', log_level = Logger::ERROR)
117
+ msg = "A crumb was used in attempt to access operation. #{message}"
118
+ super(logger, msg)
119
+ end
120
+ end
121
+
122
+ # This exception class handles cases where a requested page is not found on
123
+ # the Jenkins API.
124
+ #
125
+ class NotFound < ApiException
126
+ def initialize(logger, message = "", log_level = Logger::ERROR)
127
+ msg = message.empty? ? "Requested component is not found on the" +
128
+ " Jenkins CI server." : message
129
+ super(logger, msg)
130
+ end
131
+ end
132
+ # Support for backward compatibility
133
+ NotFoundException = NotFound
134
+
135
+ # This exception class handles cases when Crumb Issues did not issue a
136
+ # crumb upon request.
137
+ #
138
+ class CrumbNotFound < NotFound
139
+ def initialize(logger, message = "", log_level = Logger::ERROR)
140
+ message = "No crumb available on the server." if message.empty?
141
+ super(logger, message)
142
+ end
143
+ end
144
+ # Support for backward compatibility
145
+ CrumbNotFoundException = CrumbNotFound
146
+
147
+ # This exception class handles cases where the requested job does not exist
148
+ # in Jenkins.
149
+ #
150
+ class JobNotFound < NotFound
151
+ def initialize(logger, message = "", log_level = Logger::ERROR)
152
+ msg = message.empty? ? "The specified job is not found" : message
153
+ super(logger, msg)
154
+ end
155
+ end
156
+
157
+ # This exception class handles cases where the requested view does not exist
158
+ # in Jenkins.
159
+ #
160
+ class ViewNotFound < NotFound
161
+ def initialize(logger, message = "", log_level = Logger::ERROR)
162
+ msg = message.empty? ? "The specified view is not found" : message
163
+ super(logger, msg)
164
+ end
165
+ end
166
+
167
+ # This exception class handles cases where the requested node does not exist
168
+ # in Jenkins.
169
+ #
170
+ class NodeNotFound < NotFound
171
+ def initialize(logger, message = "", log_level = Logger::ERROR)
172
+ msg = msg.empty? ? "The specified node is not found" : message
173
+ super(logger, msg)
174
+ end
175
+ end
176
+
177
+ # This exception class handles cases where the Jenkins API returns with a
178
+ # 500 Internal Server Error.
179
+ #
180
+ class InternalServerError < ApiException
181
+ def initialize(logger, message = "", log_level = Logger::ERROR)
182
+ message = "Internal Server Error. Perhaps the in-memory configuration" +
183
+ " Jenkins is different from the disk configuration. Please try to" +
184
+ " reload the configuration" if message.nil? || message.empty?
185
+ super(logger, message)
186
+ end
187
+ end
188
+ # Support for backward compatibility
189
+ InternalServerErrorException = InternalServerError
190
+
191
+ # This exception class handles cases where the Jenkins is getting restarted
192
+ # or reloaded where the response code returned is 503
193
+ #
194
+ class ServiceUnavailable < ApiException
195
+ def initialize(logger, message = "", log_level = Logger::ERROR)
196
+ message = "Jenkins is being reloaded or restarted. Please wait till" +
197
+ " Jenkins is completely back online. This can be" +
198
+ " programatically achieved by System#wait_for_ready" if message.empty?
199
+ super(logger, message)
200
+ end
201
+ end
202
+ # Support for backward compatibility
203
+ ServiceUnavailableException = ServiceUnavailable
204
+
205
+ # Exception occurred while running java CLI commands
206
+ #
207
+ class CLIError < ApiException
208
+ def initialize(logger, message = "", log_level = Logger::ERROR)
209
+ message = "Unable to execute the command." if message.empty?
210
+ super(logger, message)
211
+ end
212
+ end
213
+ # Support for backward compatibility
214
+ CLIException = CLIError
215
+
216
+ # Exception when a particular plugin is not found
217
+ #
218
+ class PluginNotFound < NotFound
219
+ def initialize(logger, message = "", log_level = Logger::ERROR)
220
+ message = "The specified plugin is not found" if message.empty?
221
+ super(logger, message)
222
+ end
223
+ end
224
+ end
225
+ end
226
+ end