kosa8 0.5.1 → 0.5.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: dec638724a2bb4302bcf2990fdf08c7753d3afc172a6a8fe7441365ae9936286
4
- data.tar.gz: 12c7af648094ab3f26f3335c8bcbb61952587a3db97fc9f421dc22004d1fe030
3
+ metadata.gz: 2e865379c864f1e968325212f3e06cb82056a58357aa06d0f443a651333d7928
4
+ data.tar.gz: 340f70bfbb6ef787f0f506119462d2bf5b785a5eb546f21d645b54d7caf66621
5
5
  SHA512:
6
- metadata.gz: 483b98a99ec7417d9b3ecb80d2be5d7f113fd3794d7bd2dd056c115d6b1ee4d15b48d3cf06011bf4c7528fbd15c13ae75123ada24201e80fe5748c7f9b9b0d01
7
- data.tar.gz: 2807af532b0199813b75eab0d0c6a4108124cbc4e54d69223c7f5cc8f09b05ef8dfeef1de44a28cbe9490f92c62078d81690ffc67eb83f89df15d7151a4152a3
6
+ metadata.gz: d8aa6298bb875c0914621763de073c5326811c7187928df33425f379a31561bc3078fee0366f91d0133dde7f787ac8e0672679f09d4c8f51f0c18388edf506a5
7
+ data.tar.gz: bc7c0768719881ea2833e4ff81323a1b5bd4a7cf3fa5af1d268bb597dd377add42835445d80420354858ee5f7c38ae6bc4a5d620522d0528cd3805a4d86f7993
data/lib/kosa8/cloud.rb CHANGED
@@ -63,6 +63,11 @@ module Kosa8
63
63
  MAX_FORK = 1000
64
64
  # The limit kosa8 Cloud puts on a buffered exec, in seconds (504 exec_timeout past it).
65
65
  EXEC_LIMIT_S = 90
66
+ # Where a person makes and revokes API keys.
67
+ KEYS_URL = "https://app.kosa8.com/app#keys"
68
+ # Added to a 401 from the API: the key is the usual cause.
69
+ AUTH_HINT = "check KOSA8_API_KEY (or api_key:) is a current key; make a new one at #{KEYS_URL}"
70
+
66
71
  EXEC_HINT = "a buffered exec is stopped after #{EXEC_LIMIT_S} s; run long work in the background, " \
67
72
  "writing to /workspace, and poll it"
68
73
 
@@ -88,9 +93,12 @@ module Kosa8
88
93
  def initialize(api_key: nil, base_url: nil, env: ENV, timeout: 120, transport: nil, retry_policy: nil,
89
94
  sleeper: nil)
90
95
  key = (api_key.nil? ? env["KOSA8_API_KEY"].to_s : api_key.to_s).strip
91
- raise Error, "no kosa8 Cloud API key: pass api_key: or set KOSA8_API_KEY" if key.empty?
96
+ if key.empty?
97
+ raise Error, "no kosa8 Cloud API key: set KOSA8_API_KEY or pass api_key:; make one at #{KEYS_URL}"
98
+ end
92
99
  unless key.start_with?(KEY_PREFIX) || key.start_with?(TOKEN_PREFIX)
93
- raise Error, "a kosa8 Cloud API key starts with #{KEY_PREFIX} (a capability token with #{TOKEN_PREFIX})"
100
+ raise Error, "a kosa8 Cloud API key starts with #{KEY_PREFIX} (a capability token with #{TOKEN_PREFIX}); " \
101
+ "check KOSA8_API_KEY or api_key:, or make a key at #{KEYS_URL}"
94
102
  end
95
103
 
96
104
  @key = key
@@ -206,10 +214,12 @@ module Kosa8
206
214
  raise ExecTimeoutError.new("kosa8 Cloud did not answer within #{@timeout} s (#{EXEC_HINT})",
207
215
  method: method, path: path, code: "exec_timeout")
208
216
  end
209
- raise TimeoutError.new("kosa8 Cloud did not answer within #{@timeout} s (#{e.message})",
217
+ raise TimeoutError.new("kosa8 Cloud did not answer #{method} #{path.split("?", 2)[0]} within #{@timeout} s " \
218
+ "(#{e.message}); try again, or give Kosa8::Cloud.new a longer timeout:",
210
219
  method: method, path: path)
211
220
  rescue SystemCallError, IOError, SocketError, OpenSSL::SSL::SSLError => e
212
- raise ConnectionError.new("could not reach kosa8 Cloud at #{@base_url}: #{e.message}",
221
+ raise ConnectionError.new("could not reach kosa8 Cloud at #{@base_url}: #{e.message}; " \
222
+ "check the network connection and KOSA8_CLOUD_URL (or base_url:), then try again",
213
223
  method: method, path: path)
214
224
  end
215
225
  return content.to_s.b if raw && status.to_i < 400
@@ -251,9 +261,40 @@ module Kosa8
251
261
  ra = body["retry_after_s"] # e.g. 409 idempotency_in_progress, 503 no_capacity
252
262
  e.retry_after = ra.to_f if ra.is_a?(Numeric) && ra.positive?
253
263
  end
264
+ e.request_id = header(headers, "x-request-id") || header(headers, "cf-ray")
265
+ e.detail = detail(e, message)
254
266
  e
255
267
  end
256
268
 
269
+ # The context after the sentence of an API error:
270
+ # "[HTTP 503 no_capacity; retry in 10 s; request 8e1a-SJC]", with the
271
+ # place to pay for a 402 and what to check for a 401.
272
+ def self.detail(e, sentence)
273
+ parts = ["HTTP #{e.status}#{e.code ? " #{e.code}" : ""}"]
274
+ if e.retry_after
275
+ n = e.retry_after
276
+ parts << "retry in #{n == n.round ? n.round : n.round(1)} s"
277
+ elsif e.retryable?
278
+ parts << "retryable"
279
+ end
280
+ up = Kosa8.body_str(e.body, "upgrade_url")
281
+ parts << "upgrade: #{up}" if e.status == 402 && up && !sentence.include?(up)
282
+ parts << AUTH_HINT if e.status == 401
283
+ parts << "request #{e.request_id}" if e.request_id
284
+ "[#{parts.join("; ")}]"
285
+ end
286
+
287
+ # A response header's value by case-insensitive name, or nil.
288
+ def self.header(headers, name)
289
+ return nil unless headers.respond_to?(:each)
290
+
291
+ v = nil
292
+ headers.each { |k, val| v = val if k.to_s.casecmp(name).zero? }
293
+ v = v.first if v.is_a?(Array)
294
+ s = v.to_s.strip
295
+ s.empty? ? nil : s
296
+ end
297
+
257
298
  def self.exec_path?(path)
258
299
  path.to_s.split("?", 2)[0].end_with?("/exec")
259
300
  end
data/lib/kosa8/errors.rb CHANGED
@@ -11,6 +11,13 @@ module Kosa8
11
11
  # Seconds the server asked to wait before trying again (Retry-After, or
12
12
  # the body's retry_after_s), when it said.
13
13
  attr_accessor :retry_after
14
+ # The id of the request that failed (the X-Request-Id header, else
15
+ # Cloudflare's cf-ray): quote it when you ask kosa8 support. nil when the
16
+ # answer had neither, and for errors with no answer.
17
+ attr_accessor :request_id
18
+ # Context added after the server's sentence in #message (kosa8 Cloud
19
+ # answers: "[HTTP 503 no_capacity; retry in 10 s; request ...]").
20
+ attr_accessor :detail # :nodoc:
14
21
 
15
22
  def initialize(message = nil, status: nil, daemon_message: nil, remediation: nil, method: nil, path: nil, body: nil,
16
23
  code: nil)
@@ -24,6 +31,21 @@ module Kosa8
24
31
  @code = code
25
32
  end
26
33
 
34
+ # The message: the server's own sentence (or the SDK's), followed for a
35
+ # kosa8 Cloud answer by its status, code, wait and request id, so a log
36
+ # line alone says what happened and what to do. The bare sentence is
37
+ # daemon_message.
38
+ def to_s
39
+ d = @detail
40
+ d.nil? || d.empty? ? super : "#{super} #{d}"
41
+ end
42
+
43
+ # retry_after under the API's own name: seconds to wait before trying
44
+ # again, or nil.
45
+ def retry_after_s
46
+ retry_after
47
+ end
48
+
27
49
  # Whose fault it was: "ok", "exit_nonzero", "timeout", "infra", "quota",
28
50
  # "client", or "error" (not an API answer). See Kosa8.classify_failure.
29
51
  def failure_class
data/lib/kosa8/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Kosa8
4
- VERSION = "0.5.1"
4
+ VERSION = "0.5.2"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kosa8
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.1
4
+ version: 0.5.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - kosa8